Code will run only once.

Hey guys, so the code below allows me to enter any phone number from 4x3 keypad, then when I press '#' it will save that number as fileName. FileName is then substituted into the gsm code where a phone number is supposed to be put so that a message can be sent to that number. My code does work, but it will do it only once. For example when i press '#' after i insert a phone number it sends a message automatically, but then when i enter another number again the numbers wont appear on the serial monitor. (When I had only the keypad code by itself it worked perfectly, i was able to save number as fileName everytime I entered a new number and pressed '#'. Do I need some type of reset, or clear function? hmmm

#include <SoftwareSerial.h>
SoftwareSerial sim(25, 27);
int _timeout;
String _buffer;

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {30, 32, 34}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char entryStr[12];
int i=0;

void setup()
{
delay(2000);
Serial.begin(9600);
_buffer.reserve(50);
sim.begin(9600);
delay(1000);

}

String _readSerial()
{
_timeout = 0;
while (!sim.available() && _timeout < 12000 )
{
delay(13);
_timeout++;
}
if (sim.available()) {
return sim.readString();
}
}

void keypadentry()
{
char key = keypad.getKey();

if (key)
{
if (key == '*')
{
memset(entryStr, 0, sizeof(entryStr));
i=0;
key=0;
Serial.println("");
Serial.println("Canceled");
}

else if (key != '#')
{

entryStr*= key;*

  • i++;*

  • Serial.print(key);*

  • }*

  • else*

  • {*

  • Serial.println("");*

  • i=0;*

  • key=0;*

  • String fileName = entryStr;*

  • memset(entryStr, 0, sizeof(entryStr));*

  • Serial.println(fileName);*

  • //Serial.println ("Sending Message");*
    sim.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
    delay(1000);
    //Serial.println ("Set SMS Number");
    sim.println("AT+CMGS="" + fileName + ""\r");
    delay(1000);
    String SMS = "Welcome to IPMS";
    sim.println(SMS);
    delay(100);
    sim.println((char)26);// ASCII code of CTRL+Z
    delay(1000);
    _buffer = _readSerial();

  • }*
    }
    }
    void loop()
    {
    keypadentry();
    }

it will save that number as fileName.

I suppose, then, that if you wanted to write the data to a file, you'd store the name of the file in a variable called phoneNumber...

   fileName = fileName;

Well, that's certainly novel. Was there a point?

Do I need some type of reset, or clear function?

I'll bet a box of doughnuts that you already know the answer to this.

Look at what happens when the '*' key is pressed for a clue as to how to implement a clear function.

PaulS:
I suppose, then, that if you wanted to write the data to a file, you'd store the name of the file in a variable called phoneNumber...

   fileName = fileName;

Well, that's certainly novel. Was there a point?
I'll bet a box of doughnuts that you already know the answer to this.

Look at what happens when the '*' key is pressed for a clue as to how to implement a clear function.

Hi paul thanks for your reply. Yes you are right I have no idea why i did fileName = fileName lool however, that '*' button yes it is used to clear and start a new line but after i confirm the number with '#' the message will be sent but then the keypad will stop working, so whatever i type on the keypad will not work (not even the * key) and will not show up on the serial monitor. I am a newbie but I have a feeling that in the keypadentry() loop, the code runs until the very end where it says '_buffer = _readSerial();' then pauses there, or this last function works nonstop (I did try delay but doesnt work.) UPDATE THE PROBLEM WAS THE _buffer = _readSerial();, I JUST DELETED THAT LOL

I looked at your code again.

This:

byte rowPins[ROWS] = {22, 24, 26, 28}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {30, 32, 34}; //connect to the column pinouts of the kpd

suggests you have a Mega.

This:

#include <SoftwareSerial.h>
SoftwareSerial sim(25, 27);

just plain confuses the hell out of me. If you have an Arduino with that many pins, it has more than one hardware serial port. Since you are not using all of the hardware serial ports (as hardware serial ports), there is NO reason to be doing software serial.

On the Mega, and some other boards, not all pins can be used for software serial. It is your responsibility to determine which pins can be used on the board you want to do software serial on, if you just absolutely must use a rusty, dull knife instead of a clean, sharp one.