Please help! i want to be able to enter number keys when deleteKey=‘D’ is pressed.
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
int i;
for (i = 0; i < sizeof(value); i++)
EEPROM.update(ee++, *p++);
return i;
}
template <class T> int EEPROM_readAnything(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
int i;
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return i;
}
// KEYPAD
const byte numRows = 4;
const byte numCols = 4;
byte rowPins[numRows] = {5,4,3,2};
byte colPins[numCols] = {9,8,7,6};
char keys[numRows][numCols] =
{
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
//KEYPAD
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, numRows, numCols);
// LCD
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
//0x27 for red i2c, 0x3F for black I2c
String inputString = "";
//Keys definition
/////////////////////////////////////////////////////////////////////////////////////////////////////
const int upKey = 'A';
const int downKey = 'B';
const int selectKey = 'C';
const int deleteKey = 'D';
const int saveKey = '#';
/////////////////////////////////////////////////////////////////////////////////////////////////////
//Menu definition
/////////////////////////////////////////////////////////////////////////////////////////////////////
int currentMenuItem = 0;
int lastState = 0;
const int maxMenu = 5;
const int minMenu = 1;
////////////////////////////////////////////////////////////////////////////////////////////////////
int rateA = 5;
int eEPROMaddrA = 50;
int rateB = 10;
int eEPROMaddrB = 60;
int rateC = 30;
int eEPROMaddrC = 70;
int rateD = 150;
int eEPROMaddrD = 80;
int rateE = 350;
int eEPROMaddrE = 90;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
lcd.print("Starting...");
lcd.setCursor(0, 1);
delay(1000);
lcd.clear();
lcd.home();
}
void loop()
{
mainMenu();
}
void mainMenu() {
int state = 0;
int x = int(keys);
char key = myKeypad.getKey();
if (key != NO_KEY) {
if (key == upKey) {
state = 1;
}
else if (key == downKey) {
state = 2;
}
else if (key == selectKey) {
state = 3;
}
else if (key == deleteKey) {
state = 4;
}
else if (key == saveKey) {
state = 5;
}
}
if (state != lastState) {
if (state == 1) {
//If Up
currentMenuItem ++;
if (currentMenuItem > maxMenu) currentMenuItem = minMenu;
displayMenu(currentMenuItem);
}
else if (state == 2) {
//If Down
currentMenuItem--;
if(currentMenuItem < minMenu) currentMenuItem = maxMenu;
displayMenu(currentMenuItem);
}
else if (state == 4 && currentMenuItem == 1) {
//If Selected
lcd.setCursor(0, 1);
lcd.print(" ");
}
else if (state == 4 && currentMenuItem == 2) {
//If Selected
lcd.setCursor(0, 1);
lcd.print(" ");
}
else if (state == 4 && currentMenuItem == 3) {
//If Selected
lcd.setCursor(0, 1);
lcd.print(" ");
}
else if (state == 4 && currentMenuItem == 4) {
//If Selected
lcd.setCursor(0, 1);
lcd.print(" ");
}
else if (state == 4 && currentMenuItem == 5) {
//If Selected
lcd.setCursor(0, 1);
lcd.print(" ");
}
lastState = state;
}
delay(5);
}
void displayMenu(int x) {
switch (x) {
case 1:
EEPROM_readAnything(eEPROMaddrA , rateA );
inputString = (String)rateA;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rate A");
lcd.setCursor(0, 1);
lcd.print(inputString);
break;
case 2:
EEPROM_readAnything(eEPROMaddrB , rateB );
inputString = (String)rateB;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rate B");
lcd.setCursor(0, 1);
lcd.print(inputString);
break;
case 3:
EEPROM_readAnything(eEPROMaddrC , rateC );
inputString = (String)rateC;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rate C");
lcd.setCursor(0, 1);
lcd.print(inputString);
break;
case 4:
EEPROM_readAnything(eEPROMaddrD , rateD );
inputString = (String)rateD;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rate D");
lcd.setCursor(0, 1);
lcd.print(inputString);
break;
case 5:
EEPROM_readAnything(eEPROMaddrE , rateE );
inputString = (String)rateE;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rate E");
lcd.setCursor(0, 1);
lcd.print(inputString);
break;
}
}