How to make simple Arduino Phone

How to make simple Arduino Phone

Today we will make a programmable mobile phone for beginners to learn about microcontrollers.

A simple way to get started with Arduino.

Objectives:

  1. Programing basic features
  2. Communicate and display LCD screen with Arduino
  3. Communicate and get data from SIM module to make calls and send messages
  4. Use matrix keys
  5. Schematic circuit power and charging for battery
  6. Learn how to design PCB circuit

Supplies

Supplies

Supplies:

  1. Arduino Pro Mini 3V3: Here
  2. SIM800C or A7680C module: Here
  3. Nokia 5110 LCD display: Here
  4. 4x4 matrix keypad: Here
  5. TP4056 charging circuit: Here
  6. MT3608 3V3 voltage regulator circuit: Here
  7. CH340 code loading circuit: Here
  8. Need some resistors, wires and test board,..

Overview:

Power block: utilize a slim 3.7V power battery for mobile applications. A universal charging module like the TP4056 is required to charge and manage the battery voltage. Additionally, a 3V3 power supply is necessary for the entire circuit to function. We employ the MT3608 IC, a type of boost converter IC, to elevate the voltage from the battery to 3V3. It's important to note that the battery voltage will fluctuate from 3.2V when low to 4.2V when fully charged.

MicroController block: Implement the ATMEGA328P 3V3 IC from Arduino Pro Mini or Arduino Nano, which is very user-friendly for beginners starting with programming.

SIM block: Incorporate the SIM800C module to connect to the GSM network for making calls and sending messages. I selected this SIM module primarily because it is very affordable and easy to solder manually. However, in several countries, 2G networks have been phased out, but a solution exists without needing to change the hardware: using SIMCOM's 4G module, the A7680C, which has a pinout that is fully compatible for replacement.

Display block: Utilize the Nokia 5110 LCD display, giving this phone a nostalgic look reminiscent of Nokia phones from the 2000s :v. But that's not the main point! The key aspect is that it is readily available, inexpensive, and simple to program. An OLED screen can also be used for enhanced display quality.

IO block: Employ a 4x4 matrix keypad for data entry and device control.

The buzzer and LED light can function as a flashlight and provide notifications for incoming calls.

I have a concept where we can substitute the LED light with an infrared light, allowing us to input the IR command learned from a television remote or air conditioner. This would enable our phone to function as a remote control. There are countless ideas you can explore with this.

Avoid trying to connect everything at once, as it can become overwhelming. However, it's not overly complicated. Just read the datasheet and library, and follow the instructions step by step!

Step 1: Programming the First Program

Programming the First Program

Like all developers in the world, the first thing they do to program is “Hello World”. We will also have the first thing for every microcontroller is “Blink Led”.

Use the code uploader and open the Arduino IDE to create the first project for Arduino Pro Mini.

Link instructions and example code here:

  1. How to program Arduino Pro Mini using CH340 Module: Link

Step 2: Testing With Module Sim

Testing With Module Sim

Programming the SIM module necessitates a strong understanding of string manipulation, as communication with the module occurs via AT commands, to which the SIM replies with appropriate strings. Your responsibility is to process these strings to achieve the intended results.

The procedure commences with the transmission of a text message to a designated phone number.

Upon receipt of a message from another individual, it is essential to analyze the string to extract the sender's phone number, the date, and the primary content of the message.

Furthermore, we address the functionalities of initiating calls, receiving calls, and ending calls.

Connect Arduino with Module SIM800: SIM_TX to pin 3, SIM_RX to pin 2:

SoftwareSerial mySerial(3, 2); //SIM800 Tx & Rx is connected to Arduino #3 & #2

Link instructions and example code here:

  1. Send Receive SMS & Call with SIM800L GSM Module & Arduino: Link

Step 3: Programming With Nokia LCD5110 Display

Programming With Nokia LCD5110 Display

Utilize libraries and tools to showcase whatever you display on this screen, such as logos, images, and news updates, to customize your phone to your liking.

This part of the project is the most time-consuming. It's crucial to have a thorough understanding of the LCD display to program it according to your preferences.

Connect Arduino with Module LCD5110:

// Initialize LCD object for software SPI
// Adafruit_PCD8544(CLK,DIN,D/C,CE,RST);
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

Link instructions and example code here:

  1. Interface Nokia 5110 Graphic LCD Display with Arduino: Link
  2. Menu on a Nokia 5110 LCD Display with Arduinio Tutorial: Link

Step 4: Testing With Matrix Keypad 4x4

Testing With Matrix Keypad 4x4

Use a 4X4 keypad with 16 buttons and 8 pins on the microcontroller. Programing to receive buttons to perform functions or receive data such as numbers to call, text messages.

Connect 4X4 Keypad to Arduino: C1-14 to A0-A3 and H1-H4 to Pin 9-12.

Link instructions and example code here:

  1. Interfacing Arduino with 4×4 Matrix: Link

Step 5: Complete Version 1.0?

Complete Version 1.0?
Complete Version 1.0?

Add a battery and a charging module for it.

Assembling everything into a mini box, we now have a basic mobile version 1.0.

Done?

No, of course not. We need this project to be professional and easy to develop for any newbie.

Step 6: Start Pcb Design

Start Pcb Design
Start Pcb Design
Start Pcb Design

Start designing everything with professional design software such as Altium Designer, Kicad, Eagle, EasyEDA,...

Here I use Altium Designer Software: Link

Build a schematic from the components, modules used before and edit to suit the requirements. You may need to look at the datasheets from the manufacturer to know how to build a standard circuit.

Import the PCB and start the layout.

Arrange the components and place them in the appropriate positions, proceed to wire. Follow the rules for 2-layer circuits and the PCB manufacturer of your choice.

Place the PCB oder: JLCPCB - Link

The Gerber file I left below can be ready to send to the PCB manufacturer. Send them and they will make pcb for you, can choose the color you like: black, white, blue,...

Choose Base Material FR-4, 2 Layers, PCB Qty 5, PCB Thickness 1.6mm, PCB Color,...

Add items to your cart, proceed to checkout, and then wait.

Download Gerber File: Link here

Below is the schematic diagram I have created.

Step 7: Soldering Components to the PCB

Soldering Components to the PCB
Soldering Components to the PCB
Soldering Components to the PCB
Soldering Components to the PCB

First, ensure that all the components listed in the Excel BOM are prepared, along with the soldering iron, heat blower, and all required tools.

Begin by soldering and testing the circuit sample, where your determination, alongside you soldering expertise, is crucial. We're working with tiny 0603 components, so if the budget allows, you could use a stencil to apply solder paste. After then using the heat table or heat blower with temperature about 240 - 350 °C to solder components. However, I believe it's entirely possible to solder by hand using soldering Iron. It's a time-intensive process, but it's also quite interesting.

Download Bill of Material (BOM): Link

Step 8: Final Check

Final Check
Final Check
Final Check

Examine the solder joints once again. Prior to connecting the power, it is essential to test the power source to ensure it complies with the 3V3 standard, as any deviation could potentially harm the microcontroller.

Remove the 0R resistor from the 3V3 power supply, install the battery, and perform a test to confirm that the TP4056 battery charger functions properly with 5V power from the MicroUSB port. Utilize a multimeter to measure the battery voltage along with other voltages. Subsequently, inspect the MT3608 block, assessing both the input and output to ensure they are at the appropriate 3V3 level; if they are not, recalibration of the Feedback resistor may be necessary. Furthermore, it is vital to measure the circuit to prevent possible short circuits resulting from PCB or soldering issues. After completing these verifications, reconnect the 0R resistor, and the phone should become operational.

Main Code

#include
#include
#include //Lcd Library
#include
#include
#include //Keypad library
// #include
//********************************************************************************************//
SoftwareSerial PHONE(3,2);// Connect with SIM module (Tx, Rx);
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 8); // LCD Nokia 5110 connect pin with Arduino
                                          //SCLK,DIN,DC,SCE,RST
// keyboard diagram
const byte ROWS = 4; 
const byte COLS = 4; 
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
/* A-OK; B-UP; C-DOWN; D-BACK*/
byte rowPins[ROWS] = {12, 11, 10, 9};  //Rows
byte colPins[COLS] = {A3, A2, A1, A0}; //Cols
Keypad key = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); // Init Keypad
//********************************************************************************************//
boolean backlight = true;
unsigned char menu_item = 1;
unsigned char message_item = 1;
unsigned char page = 0;
int count=0;
//********************************************************************************************//
// Variables used
String number="";
String NUMBER=""; // number phone
String msg="";
String instr="";
String str_sms="";
String str_sms_2="";
String str_sms_3="";
String str1="";
String sms="";
String dd="";
String mm="";
String yy="";
String hh="";
String min="";
String time="";
String time_now="";
int ring=0;
int i=0,temp=0;
int sms_flag=0;
char sms_num[3];
int rec_read=0;
int temp1=0;
String ch="1,.?!@abc2def3ghi4jkl5mno6pqrs7tuv8wxyz90*# "; // Code for message
unsigned long real_time;  
unsigned char t=0;
unsigned char contrast=50;
char myKey;
//********************************************************************************************//
#define LIGHT A5 //Connect pin
#define BUZZER 13
#define LED_LCD A4
static const unsigned char PROGMEM tim[] = // Phone wave icon
{ 0x00,0x6C,0xFE,0xFE,0x7C,0x38,0x10,0x00};
// 'lock', 84x48px
const unsigned char LOCK [] PROGMEM = { // Lock icon
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0xf8, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x0f, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x03, 0xc0, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x03, 
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x03, 0x80, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 
0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x03, 0xc0, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 
0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xf0, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x1f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xfc, 0x7f, 0xf0, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x1f, 0xf0, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x1f, 0xf0, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x1f, 0xf8, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x3f, 
0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xfc, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x1f, 0xfc, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 
0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x3f, 0xf0, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 
0xf8, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x1f, 0xf0, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x1f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xf0, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const unsigned char PROGMEM viettel[] = // Network provider icon
{ B00000000, B00000000,
  B00000000, B00000000,
  B00000001, B00000000,
  B00000101, B00000000,
  B00010101, B00000000,
  B01010101, B00000000,
  B01010101, B00000000,
  B01010101, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000 };
static const unsigned char PROGMEM battery_icon[] =  // Battery icon
{ B00000000, B00000000,
  B01111111, B11000000,
  B01000000, B01000000,
  B11010101, B01000000,
  B11010101, B01000000,
  B11010101, B01000000,
  B01000000, B01000000,
  B01111111, B11000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000 };
static const unsigned char PROGMEM message_icon[] =  // Message icon
{ B11111111, B11111000,
  B10100000, B00101000,
  B10010000, B01001000,
  B10001000, B10001000,
  B10000101, B00001000,
  B10000010, B00001000,
  B10000000, B00001000,
  B11111111, B11111000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000,
  B00000000, B00000000 };
void setup() {
  pinMode(LED_LCD,OUTPUT);  //Backlight for LCD
  pinMode(LIGHT,OUTPUT);    //Light
  pinMode(BUZZER,OUTPUT);   //Buzzer
  digitalWrite(LED_LCD,LOW);
  digitalWrite(LIGHT,LOW);
  digitalWrite(BUZZER,LOW);
  // Timer1.initialize(1000000); //Init interrupt
  // Timer1.attachInterrupt(GetTime);
  real_time = millis();
  // Serial.begin(9600);
  //Init LCD
  display.begin();      
  // display.setContrast(50); //Set contrast to 50
  display.setContrast(contrast);
  display.display();
  display.clearDisplay(); 
  display.display(); 
  //Init SIM module
  PHONE.begin(9600);
  gsm_init();
  
  // delay(2000);
}
void loop() { //Main program
  if( (millis() - real_time) >10000 ){ // get time from network
    real_time = millis();
    GetTime();
  }
  serialEvent(); // Check message and calling
  myKey = key.getKey();//Check button keypad
  drawMenu(); // Main Home
  
  // Handling incoming call and message events
  if(ring == 1){ // Incoming call detected
    while(ring==1)
    {
     
      page_7();
      ring=0;
      GetTime();
      // page=0;
      // break;
    }
    instr=""; i=0;// reset state
    GetTime();
    delay(500);
  }
  //*****************************Get message********************************//
  // if(sms_flag==1){
  //   while(sms_flag==1)
  //   {
  //     display.clearDisplay();
  //     str_sms=instr;
  //     int loc=instr.indexOf("+CMT: \"");
  //     number=instr.substring(loc+7,loc+19); // Split phone number
  //     int L1=str_sms.indexOf("+CMT: \"");
  //     int L2 = str_sms.length();
  //     str_sms_2=str_sms.substring(L1,L2); // Split content
  //     int L3=str_sms_2.indexOf("\"\r\n");
  //     int L4=str_sms_2.length();
  //     str_sms_3=str_sms_2.substring(L3+3,L4); // Extract message content
  //     String sms = str_sms_3; 
  //     display.setCursor(0,10);
  //     display.print(number); // Get phone number
  //     display.setCursor(0,20);
  //     display.print("<");
  //     display.print(str_sms_3); // New message
  //     display.print("Noi dung: "); // New message
  //     display.print(">");
  //     display.display();
  //     delay(5000);
  //     page_9();
  //     // instr=""; // Reset state
  //     str_sms="";
  //     str_sms_2="";
  //     str_sms_3="";
  //     sms="";number="";
  //     sms_flag=0;
  //     page=0;
  //     GetTime();
  //   }
  //   delay(200);
    
  // }
 
  //Lock screen by button '0'
  if (myKey=='0')
  {
    if (page == 0){
      page=99;
    }
    else if (page == 99){
      page=0;
    }
  }
  // Change menu and variable
  //Button OK
  if (myKey=='A')
  {
    if (page == 1 && menu_item==3) 
    {
      if (backlight) 
      {
        backlight = false;
        digitalWrite(LIGHT,HIGH);
      }
      else 
      {
        backlight = true; 
        digitalWrite(LIGHT,LOW);
        
      }
    }
    if(page == 1 && menu_item ==2)
    {
      page=3;
    }
    else if (page == 1 && menu_item==1)
    {
      page=2;
    }
    else if (page == 0 )
    {
      page=1;
    }
    else if (page == 8 && temp1 == 1 )
    {
      DocTinNhan();
    }
    else if (page == 4 && message_item )
    {
      page_5();
    }
  }
  //Button up
  if (myKey=='B')
  {
    if (page == 0){
      contrast++;
      if(contrast>50) contrast=25;
      display.setContrast(contrast);
      display.display();
    }
    else if (page == 1 ) 
    {
      menu_item--;
      if (menu_item==0) menu_item=3;
    }
    else if(page ==3)
    {
      temp++;
    }
    else if(page == 4 ) 
    {
      message_item--;
      if (message_item==0) message_item=3;
    }
  }
//Button down
  if (myKey=='C')
  {
    if (page == 0){
      contrast--;
      if(contrast<25) contrast=50;
      display.setContrast(contrast);
      display.display();
    }
    else if (page == 1)
    {
      menu_item++;
      if (menu_item==4) menu_item=1;      
    }
    else if(page ==3)
    {
      temp--;
    }
    else if(page == 4 )
    {
      message_item++;
      if (message_item==4) message_item=1;
    }
  }
  
   //Button Back
   if (myKey=='D') //BACK
   { 
    if (page == 1 )
    {
      page=0;
    }
    else if (page == 2) 
    {
      page=1;
     }
    else if (page == 4) 
    {
      page=3;
    }
    else if (page == 0) 
    {
      page=8;
    }
    else if (page == 8) 
    {
      page=0;
    }
   }
   
}
void NhapSDT()
{
  count=0;
  NUMBER="";
  while(1) // true
  {
    char myKey = key.getKey();
    display.setTextSize(1);
    display.setTextColor(BLACK, WHITE);
    if(myKey=='1')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='2')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='3')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='4')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='5')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='6')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='7')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='8')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='9')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='0')
    {
      display.setCursor((count*6)+12, 25);
      display.print(myKey);
      NUMBER+=myKey;
      count++;
    }
    if(myKey=='A' && page == 2)// call button handle
    {
      NUMBER.remove(0,1);
      // Call command here
      PHONE.print("ATD+ +84"); // change your country code here
      PHONE.print(NUMBER);
      PHONE.println(";");
      delay(100);
      //******************************//
      page=6; // calling
      break;
    }
    if(myKey=='A' && page == 3)// handle send message button
    {
      NUMBER.remove(0,1);
      page=4; // choose message
      break;
    }
     
    if(myKey=='D') // button Delete and back
     {
        count--;
        NUMBER.remove(count);
        display.setCursor((count*6)+12, 25);
        display.print(" ");
     }
    if(myKey=='D' && count<0)
     {
       page=1;
       count=0;
       break;
     }
    if(count==0)
    {
        display.setCursor(58, 40);
        display.print("BACK");
    }
    else 
    {
        display.setCursor(58, 40);
        display.print("XOA ");
    }
    display.display();
  }
}
//********************Enter message to send***********************//
void NhapTinNhan()
{
  int x=0,y=0,num=0;
  msg="";
  while(1)
  {
    char myKey=key.getKey();
      // Enter data
       if(myKey=='1')
       {
         num=0;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='1')
          {
           num++;
           if(num>5)
           num=0;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
       else if(myKey=='2')
       {
         num=6;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='2')
          {
           num++;
           if(num>9)
           num=6;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
       else if(myKey=='3')
       {
         num=10;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='3')
          {
           num++;
           if(num>13)
           num=10;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
       else if(myKey=='4')
       {
         num=14;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='4')
          {
           num++;
           if(num>17)
           num=14;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
      else if(myKey=='5')
       {
         num=18;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='5')
          {
           num++;
           if(num>21)
           num=18;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
        else if(myKey=='6')
       {
         num=22;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='6')
          {
           num++;
           if(num>25)
           num=22;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
       else if(myKey=='7')
       {
         num=26;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='7')
          {
           num++;
           if(num>30)
           num=26;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
       else if(myKey=='8')
       {
         num=31;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='8')
          {
           num++;
           if(num>34)
           num=31;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
       else if(myKey=='9')
       {
         num=35;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='9')
          {
           num++;
           if(num>39)
           num=35;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
        else if(myKey=='0')
       {
         num=40;
         display.setCursor(x*6,(y*10)+10);
         display.print(ch[num]);
         display.display();
         for(int i=0;i<30000;i++)
         { 
          char myKey=key.getKey();
          if(myKey=='0')
          {
           num++;
           if(num>41)
           num=40;
           display.setCursor(x*6,(y*10)+10);
           display.print(ch[num]);
           display.display();
           i=0;
           delay(20);
          } 
         }
         x++;
         if(x>13)
         {
           x=0;
           y++;
           y%=3;
         }
         msg+=ch[num];
        }
        // Function button
        else if(myKey=='D')
        {
          x--;msg.remove(x);
          if(x<0 && y>0){
            x=13;
            y--;
          }
          if(x<0 && y==0) {
            page=3;
            break;
          }
          
          display.setCursor(x*6,(y*10)+10);
          display.print(" ");
          display.display();
        } 
        else if(myKey=='A')
        {
         // Serial.println("Gửi");
          page=5;
          break;
        } 
        else if(x==0 && y==0){
            display.setCursor(58, 40);
            display.print("BACK");
            display.display();
          }
        else if(x>0 && y>=0){
            display.setCursor(58, 40);
            display.print(" XOA");
            display.display();
          }
    }
}
//***********************Read Incoming Messages********************//
void DocTinNhan()
{
  display.clearDisplay();
  display.setCursor(0,0);
  display.print(number);
  display.setCursor(0,10);
  display.print("Please wait...");
  display.display();
  delay(2000);
  // PHONE.print("AT+CMGR=");
  // PHONE.println(sms_num);
  // PHONE.print("AT+CMGR=1");delay(100); //Save message to memory
  // str_sms="Dau Thanh Trung";
  // int l1=str_sms.indexOf("\"\r\n");
  // int l2=str_sms.indexOf("OK");
  // String sms=str_sms.substring(l1+3,l2-4);
  // display.clearDisplay();
  // display.print(sms);
  display.setCursor(58, 40);
  display.print("BACK");
  display.display();
  delay(5000);
}
//*********************Draw menu display********************//
void drawMenu()
    if (page==1) 
    {    
      page_1();//Show selection
    }
    else if (page==2) //execute command
    {
      page_2();    
    }
    else if (page==3) 
    {
      page_3();    
    }
    else if (page==4)
    {
      page_4(); 
    }
    else if (page==5)
    {
      page_5(); 
    }
    else if(page==6)
    {
      page_6();
    }
    else if(page==7)
    {
      page_7();
    }
    else if(page==8)
    {
      page_8();
    }
    else if(page==9)
    {
      page_9();
    }
    else if(page==99)
    {
      page_99();
    }
    else if (page==0) 
    {
      page_0(); 
    }
}
//****************************Standby screen + Temperature display**********************//
void page_0(){
    display.setTextSize(1);
    display.clearDisplay();
    display.drawBitmap(0, 0,  viettel, 16, 16, 1); // draw logo
    display.drawBitmap(72, 0,  battery_icon, 16, 16, 1); // draw logo
    display.setTextColor(BLACK,WHITE);
    display.setCursor(10, 1);
    display.print("Viettel");
    // display.drawRect(65,20,4,4,BLACK);//(Tọa độ x,y, kích thước 4x4, BLACK);
    display.setTextSize(2);
    display.setCursor(13, 12);
    display.print(hh);display.print(":");display.print(min);
    display.setTextSize(1);
    display.setCursor(13, 30);
    display.print(dd);display.print("/");
    display.print(mm);display.print("/20");
    display.print(yy);
    
    // display.setCursor(72, 20);
    display.setTextSize(1);
    display.setCursor(0, 40);
    display.print("MENU");
    // display.drawFastHLine(0,38,83,BLACK);
    display.setCursor(40, 40);
    display.print(contrast);
    display.drawBitmap(70, 40,  message_icon, 16, 16, 1); // draw logo
    display.display();
    
}
//****************************Display MENU**********************//
void page_1(){
  display.setTextSize(1);
  display.clearDisplay();
  display.setTextColor(BLACK, WHITE);
  display.setCursor(15, 0);
  display.print("MAIN MENU");
  display.drawFastHLine(0,8,83,BLACK);
  display.setCursor(0, 40);
  display.print("OK");
  display.setCursor(58, 40);
  display.print("BACK");
  display.setCursor(0, 10);
  if (menu_item==1){ 
    display.setTextColor(WHITE, BLACK);
  }else {
    display.setTextColor(BLACK, WHITE);
  }
  display.print(">GOI DIEN");
  if (menu_item==2){ 
    display.setTextColor(WHITE, BLACK);
  }
  else {
    display.setTextColor(BLACK, WHITE);
  }  
  display.setCursor(0, 20);
  display.print(">TIN NHAN");
  if (menu_item==3) 
  {
    display.setTextColor(WHITE, BLACK);
  }
  else 
  {
    display.setTextColor(BLACK, WHITE);
  }    
  
  display.setCursor(0, 30);
  display.print(">DEN PIN: ");
  
  if (backlight) 
  {
    display.print("OFF");
  }
  else 
  {
    display.print("ON");
  }
  display.display();
  display.clearDisplay();
}
//****************************Display Calling**********************//
void page_2(){
  display.setTextSize(1);
  //display.clearDisplay();
  display.setTextColor(WHITE,BLACK);
  display.fillRect(0, 0, 83, 9, BLACK);
  display.setCursor(18, 1);
  display.print("GOI DIEN");
  display.setTextColor(BLACK,WHITE);
  display.setTextSize(1);
  display.setCursor(0, 12);
  display.print("NHAP SDT:");
  display.setTextSize(1);
  display.setCursor(0, 40);
  display.print("GOI");
  display.setCursor(58, 40);
  display.print("BACK");
  display.display();
  NhapSDT();
}
//****************************Display send message**********************//
void page_3(){
  display.setTextSize(1);
  //display.clearDisplay();
  display.setTextColor(WHITE,BLACK);
  display.fillRect(0, 0, 83, 9, BLACK);
  display.setCursor(18, 1);
  display.print("MESSAGE");
  //display.drawFastHLine(0,10,83,BLACK);
  display.setTextColor(BLACK,WHITE);
  display.setTextSize(1);
  display.setCursor(0, 12);
  display.print("NHAP SDT:");
  display.setTextSize(1);
  display.setCursor(0, 40);
  display.print("NEXT");
  display.setCursor(58, 40);
  display.print("BACK");
  display.display();
  NhapSDT();
}
//****************************Screen to select message to send**********************//
void page_4(){ 
  display.setTextSize(1);
  display.clearDisplay();
  display.setTextColor(WHITE,BLACK);
  display.fillRect(0, 0, 83, 9, BLACK);
  display.setCursor(18, 1);
  display.print("MESSAGE");
  display.setTextColor(BLACK,WHITE);
  display.setCursor(0, 40);
  display.print("SEND");
  display.setCursor(58, 40);
  display.print("BACK");
  
  display.display();
  NhapTinNhan();
}
void page_5() // Send selected message
{
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setTextSize(1);
  display.setCursor(5, 0);
  display.print("DA GUI TIN");
  display.drawFastHLine(0,8,83,BLACK);
  //Send SMS command
  PHONE.println("AT+CMGF=1"); // Configuring TEXT mode
  delay(200);
  PHONE.print("AT+CMGS=\"+84"); // change your country code here
  PHONE.print(NUMBER);
  PHONE.println("\"");
  delay(200);
  PHONE.println(msg);
  delay(200);
  PHONE.write(26);
  //**************************************//
  display.display();
  delay(2000);
  display.clearDisplay();
  page=0;
}
void page_6() // Calling
{
  
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setTextSize(1);
  display.setCursor(5, 0);
  display.print("DANG GOI DIEN");
  display.drawFastHLine(0,8,83,BLACK);
  display.setCursor(6, 25);
  display.print("+84");
  display.print(NUMBER);
  display.setCursor(58, 40);
  display.print(" END");
  display.display();
  unsigned int ans=1;
  while(ans==1)
  {  
    char myKey=key.getKey();
    str1="";
    if(myKey == 'D')
    {
      PHONE.println("ATH"); // end call command
      delay(1000);
      ans=0;
      break;
    }
    while(PHONE.available()>0)
    {
      char ch=PHONE.read();
      str1+=ch;
      if(str1.indexOf("NO CARRIER")>0)
      {
        delay(500);
        ans=0;
        break;
      }
    }
  } 
  page=0; // return home screen
}
void page_7()
{
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setTextSize(1);
  display.setCursor(5, 0);
  display.print("CUOC GOI DEN");
  display.drawFastHLine(0,8,83,BLACK);
  display.setCursor(0, 40);
  display.print("NGHE");
  display.setCursor(58, 40);
  display.print(" HUY");
  display.display();
  number="";
  int loc=instr.indexOf("+CLIP: \"");
  if(loc > 0)
  {
    number+=instr.substring(loc+9,loc+10+8);
  }
  display.setCursor(6,25);
  display.print("+84");
  display.print(number);
  display.display();
  int ans=1;
  while(ans==1)
  {  
    char myKey=key.getKey();
    str1="";
    if(myKey == 'A')
    {
      PHONE.println("ATA"); // Accept call
      delay(500);
      page=6;
      ans=0;
      break;
    }
    if(myKey == 'D')
    {
      PHONE.println("ATH"); // end call command
      delay(500);
      ans=0;
      page=0;
      break;
    }
    while(PHONE.available()>0)
    {
      char ch=PHONE.read();
      str1+=ch;
      if(str1.indexOf("NO CARRIER")>0)
      {
        delay(500);
        page=0;
        ans=0;
        break;
      }
    }
  } 
}
void page_8() // Inbox
{
  display.setTextSize(1);
  display.clearDisplay();
  display.setTextColor(WHITE,BLACK);
  display.fillRect(0, 0, 83, 9, BLACK);
  display.setCursor(5, 1);
  display.print("HOP THU DEN");
  display.setTextColor(BLACK,WHITE);
  display.setCursor(0, 40);
  display.print("READ");
  display.setCursor(58, 40);
  display.print("BACK");
  display.display();
  //Message content here
  //******************************************//
}
void page_9() // Inbox
{
  display.setTextSize(1);
  display.clearDisplay();
  display.setTextColor(WHITE,BLACK);
  display.fillRect(0, 0, 83, 9, BLACK);
  display.setCursor(5, 1);
  display.print(number);
  display.setTextColor(BLACK,WHITE);
  display.setCursor(58, 40);
  display.print("BACK");
  display.display();
  //Message content here
  display.setCursor(0, 10);
  display.print(sms);
  display.display();
  //******************************************//
  delay(5000);
  display.clearDisplay();
  
}
void page_99()//Lock screen
{
  display.clearDisplay();
display.drawBitmap(0, 0,  LOCK, 84, 48, BLACK);
display.display();
}
//****************************END**********************//
void gsm_init()
{
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setTextSize(1);
  display.setCursor(8, 0);
  display.print("AUTO SETUP");
  display.setCursor(0, 10);
  display.print("MODULE SIM ");
  display.display();
  
  boolean at_flag=1;
  while(at_flag)
  {
    PHONE.println("AT");
    while(PHONE.available()>0)
    {
      if(PHONE.find("OK"))
      at_flag=0;
    }
    
    delay(1000);
  }
  delay(500);
  display.print("OK");
  display.display();
  display.setCursor(0, 20);
  display.print("ECHO ");
  display.display();
  boolean echo_flag=1;
  while(echo_flag)
  {
    PHONE.println("ATE1");
    while(PHONE.available()>0)
    {
      if(PHONE.find("OK"))
      echo_flag=0;
    }
    delay(500);
    display.print("OK");
    display.display();
    delay(1000);
  }
  display.setCursor(0, 30);
  display.print("KET NOI ");
  display.display();
  boolean net_flag=1;
  while(net_flag)
  {
    PHONE.println("AT+CPIN?");
    while(PHONE.available()>0)
    {
      if(PHONE.find("+CPIN: READY"))
      net_flag=0;
    }
    delay(500);
    display.print("OK");
    display.display();
    delay(1000);
  }
  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setTextSize(3);
  display.setCursor(8, 8);
  display.print("DONE");
  // PHONE.print("AT+CMGF=1");delay(100); //text mode configuration
  PHONE.println("AT+CLIP=1"); delay(100);
  // PHONE.println("ATM 9"); delay(100);
  // PHONE.println("AT+CLVL=100");delay(100);
  PHONE.println("AT+CRSL=100");delay(100);
  PHONE.println("AT+CEXTERNTONE=0");delay(1000);
  GetTime();
  display.display();
  delay(2000);display.clearDisplay();
  GetTime();
}
void GetTime(){ 
  PHONE.println("AT+CCLK?"); //get date and time
  delay(50);
  while(PHONE.available()){
    char ch=PHONE.read();
    time+=ch;
  }
  int num = time.indexOf('+CCLK:');
  time_now=time.substring(num+3,num+17);
  dd = time_now.substring(6,8);
  mm = time_now.substring(3,5);
  yy = time_now.substring(0,2);
  hh = time_now.substring(9,11);
  min = time_now.substring(12,14);
  time="";
  time_now="";
  //+CCLK: "21/08/05,19:28:10+28"
}
void serialEvent()// There is an incoming call or message, use interrupt to handle it.
{
  while(PHONE.available())
  {
    char ch=PHONE.read();
    instr+=ch;
    i++;
    if(instr[i-4] == 'R' && instr[i-3] == 'I' && instr[i-2] == 'N' && instr[i-1] == 'G' )
    {
       ring=1; // there is an incoming call
    }
    if(instr.indexOf("NO CARRIER")>=0)
    {
       ring=0;
       i=0;
    }
    if(instr.indexOf("+CMT:")>=0)
    {
      sms_flag=1;   // have message
    }
  }
}

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow