Hello! I am trying to retrieve phone numbers from the gsm module and store it in an array which is working. But there is just a little problem, after uploading, when I hit '1' from the keypad, it wouldn't show the number. But after hitting another key, that will be the time the previous number will be shown.Hence, the output is delayed.
#include <Keypad.h>
//SMS
char incoming_char=0; //Initiate a variable that will hold the incoming character from the Serial Port.
char numberIn[12];
//Keypad
char key3;
const byte ROWS = 4;
const byte COLS = 3;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3',},
{'4','5','6',},
{'7','8','9',},
{'*','0','#',}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {24,34,32,28};
byte colPins[COLS] = {26,22,30};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
key3 = keypad.getKey();
if (key3=='1')
{
Serial.print("keypress: ");
Serial.println(key3);
Serial1.print("AT+CPBR=");
Serial1.println(key3);
readPBentry();
}
if (key3=='2')
{
Serial.print("keypress: ");
Serial.println(key3);
Serial1.print("AT+CPBR=");
Serial1.println(key3);
readPBentry();
}
if (key3=='3')
{
Serial.print("keypress: ");
Serial.println(key3);
Serial1.print("AT+CPBR=");
Serial1.println(key3);
readPBentry();
}
if (key3=='4')
{
Serial.print("keypress: ");
Serial.println(key3);
Serial1.print("AT+CPBR=");
Serial1.println(key3);
readPBentry();
}
if (key3=='5')
{
Serial.print("keypress: ");
Serial.println(key3);
Serial1.print("AT+CPBR=");
Serial1.println(key3);
readPBentry();
}
}
void readPBentry()
{
if(Serial1.available() >0)
{
incoming_char=Serial1.read();
Serial.print(incoming_char);
while(incoming_char != '+') //Wait for the start of the communication from
{ // the shield
incoming_char=Serial1.read();
delay(10);
}
incoming_char=Serial1.read();
Serial.print(incoming_char);
if(incoming_char == 'C') //Wait for the character CMGR
{
incoming_char=Serial1.read();
Serial.print(incoming_char);
delay(10);
if(incoming_char == 'P')
{
incoming_char=Serial1.read();
Serial.print(incoming_char);
delay(10);
if(incoming_char == 'B')
{
incoming_char=Serial1.read();
Serial.print(incoming_char);
delay(10);
if(incoming_char == 'R')
{
while(incoming_char != ',')
{
incoming_char=Serial1.read();
Serial.print(incoming_char);
}
incoming_char=Serial1.read();
Serial.println(incoming_char);
delay(10);
for (int readNumber=0; readNumber<=10; readNumber++)
{
incoming_char=Serial1.read();
Serial.print(incoming_char);
numberIn[readNumber]=incoming_char;
numberIn[readNumber+1]='\0';
}
Serial.println();
Serial.print("num: ");
Serial.println(numberIn);
Serial.print(numberIn);
}
}
}
}
}
//If a character is coming from the terminal to the Arduino...
if(Serial.available() >0)
{
incoming_char=Serial.read(); //Get the character coming from the terminal
Serial1.print(incoming_char); //Send the character to the cellular module.
}
}