#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ROW_NUM = 4; // four rows
const int COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
char customKey;
const int BUZZER_PIN = 10;
#define S_START 0
#define S_PHONE 1
#define S_RECEIPT 4
byte State = S_START;
// Input buffer string
#define MAXBUF 20
char buf[MAXBUF];
byte ptr = 0;
char PhoneNo[20];
void setup () {
Serial.begin(9600);
pinMode(BUZZER_PIN, OUTPUT);
LcdInit();
}
byte PrevState = 99;
void loop () {
switch (State) {
case S_START:
WaitKey();
break;
case S_PHONE:
GetPhone();
break;
case S_RECEIPT:
break;
}
}
// ---------------------------------
// KEY INPUT
// ---------------------------------
byte GetKey(const char* Allowed, bool ShowChar = true) {
byte NumAllowed = strlen(Allowed);
customKey = customKeypad.getKey();
if (customKey != NO_KEY && ptr < MAXBUF-1) {
for(byte i=0; i<NumAllowed; ++i) {
if (customKey == Allowed[i]) {
tone(BUZZER_PIN, 750, 100);
if (ShowChar) {
lcd.print(customKey);
}
buf[ptr++] = customKey;
buf[ptr] = 0x0;
return customKey;
}
}
}
return NO_KEY;
}
void BufReset() {
ptr = 0;
buf[0] = 0x0;
}
void BufRemoveLast() {
buf[--ptr] = 0x0;
}
void WaitKey() {
// Prompt?
if (State != PrevState) {
LcdPrompt("Apasa *");
PrevState = State;
}
if (GetKey("*", false) == '*') {
lcd.clear();
BufReset();
// Next state
State = S_PHONE;
}
}
void GetPhone()
{
// Prompt?
if (State != PrevState) {
LcdPrompt("Numar: (#=stop)");
PrevState = State;
}
switch (GetKey("0123456789#CD")) {
case '#':
// Phone end
BufRemoveLast();
strcpy(PhoneNo, buf);
BufReset();
Serial.print("Numarul de telefon afisat: ");
Serial.println(PhoneNo);
return;
case 'C':
BufReset();
Serial.println(" *CLEAR*");
LcdClearRow();
return;
case 'D':
BufReset();
Serial.println(" *RESET*");
LcdClear();
// Next state
State = S_START;
return;
}
}
void LcdInit() {
lcd.init();
lcd.backlight();
LcdClear();
}
void LcdClear() {
lcd.clear();
lcd.setCursor(0,0);
}
void LcdPrompt(char* Line) {
LcdClear();
lcd.print(Line);
lcd.setCursor(0,1);
}
void LcdClearRow() {
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
}