Hi all.
I'm working on this project where I wan to recharge a line in a GSM module.
I want to be able to enter the pin of the airtime using the keypad and store it in a variable so I can use "AT commands" to recharge this line via USSD.
Right now, I am checking to see if the keys are successfully stored in the variable.
I have been able to get the keys to display on an LCD in succession.
The action to store them in the variable is in the same function but when I try to print out the variable (with key "E"), I just get the key I just pressed which is key E.
I want to also be able to press a key to exit the function (either back to the main loop or wherever) when I have entered the pin completely.
These are my two challenges.
Below is my code.
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Keypad.h>
SoftwareSerial mySerial(3, 2); //Rx and Tx respectively
const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
char rechargepin[20];
short pin = 0, j = 0;
const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
//The keymap for the keypad
char keymap[numRows][numCols] =
{
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', '0', 'A', 'B'},
{'C', 'D', 'E', 'F'}
};
char keypressed;
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
lcd.begin (16, 2);
Serial.begin(9600);
Serial.setTimeout(100);
mySerial.begin(9600); //
mySerial.setTimeout(100);//
Serial.println("Starting...");
delay(1000);
mySerial.println("AT");//
delay(100);
mySerial.println("AT+CMGF=1");//
delay(100);
mySerial.println("AT+CNMI=1,2,0,0,0");//
delay(500);
}
void loop()
{
keypressed = myKeypad.getKey();
if (keypressed == 'B') {
lcd.setCursor(0, 0);
lcd.print("Enter pin below:");
recharge();
}
if (keypressed == 'E') {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(rechargepin);
}
}
void recharge() {
while (keypressed != 'A') {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY && keypressed != 'A' ) { //If the char typed isn't A and neither "nothing"
pin = 0;
lcd.setCursor(j, 1); //This to write the key pressed on the LCD, whenever a key is pressed it's position is controlled by j
lcd.print(keypressed);
rechargepin[pin] = keypressed;
pin++;
j++;
}
}
}