I found this 2 source codes online and i want to combine them into 1. What i'm trying to do is change the rateA, rateB, rateC, rateD, and rateE values using the keypad and store them to EEPROM and at the same time use the menu functions. Thanks in advance.
Menu
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
// 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
//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:
lcd.clear();
lcd.home();
lcd.print("rateA");
lcd.setCursor(0, 1);
lcd.print(rateA);
break;
case 2:
lcd.clear();
lcd.home();
lcd.print("rateB");
lcd.setCursor(0, 1);
lcd.print(rateB);
break;
case 3:
lcd.clear();
lcd.home();
lcd.print("rateC");
lcd.setCursor(0, 1);
lcd.print(rateC);
break;
case 4:
lcd.clear();
lcd.home();
lcd.print("rateD");
lcd.setCursor(0, 1);
lcd.print(rateD);
break;
case 5:
lcd.clear();
lcd.home();
lcd.print("rateE");
lcd.setCursor(0, 1);
lcd.print(rateE);
break;
}
}
Saving values to eeprom
// TestLCD_I2C.ino
#include <Wire.h>
#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;
}
bool use_external_eeprom = true;
int eEPROMValue = 5000;
int eEPROMaddrA = 50;
int eEPROMaddrAA = 60;
// KEYPAD
const byte numRows = 4;
const byte numCols = 4;
byte rowPins[numRows] = {5,4,3,2};
byte colPins[numCols] = {9,8,7,6};
char keymap[numRows][numCols] =
{
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
#define CLEAR_KEY 'C'
#define SAVE_KEY '#'
#define RESTORE_KEY '*'
#define SWITCH_EEPROM_KEY 'D'
#define LCD_DISPLAY_KEY 'A'
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
String inputString = "";
// 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
bool lcdDisplay = true;
void setup() {
Wire.begin();
lcd.begin(16, 2);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
lcd.print("Starting...");
delay(1000);
readEEPROMAndDisplay();
}
void loop()
{
listenKeyboard();
}
void listenKeyboard()
{
char keyPressed = myKeypad.getKey();
if (keyPressed == NO_KEY)
return;
switch (keyPressed)
{
case RESTORE_KEY:
readEEPROMAndDisplay();
break;
case CLEAR_KEY:
lcd.clear();
lcd.home();
inputString = "";
break;
case SWITCH_EEPROM_KEY:
switchEEPROM();
break;
case LCD_DISPLAY_KEY:
setLCDDisplay();
break;
case SAVE_KEY:
writeEEPROMAndClear();
readEEPROMAndDisplay();
break;
default:
inputString.concat(keyPressed);
lcd.print(keyPressed);
break;
}
}
void setLCDDisplay()
{
lcdDisplay = !lcdDisplay;
if (lcdDisplay)
lcd.on();
else
lcd.off();
}
void switchEEPROM() {
use_external_eeprom = !use_external_eeprom;
lcd.clear();
lcd.home();
lcd.print("Switched to");
lcd.setCursor(0, 1);
if (use_external_eeprom)
lcd.print("external EEPROM");
else
lcd.print("internal EEPROM");
delay(1000);
lcd.clear();
lcd.home();
readEEPROMAndDisplay();
}
void readEEPROMAndDisplay()
{
String msg = "";
if (!use_external_eeprom)
{
EEPROM_readAnything(eEPROMaddrA , eEPROMValue );
msg = "Internal EEPROM";
}
else
{
EEPROM_readAnything(eEPROMaddrAA , eEPROMValue );
msg = "External EEPROM";
}
if (eEPROMValue >= 0) {
inputString = (String)eEPROMValue;
lcd.clear();
lcd.home();
lcd.print(msg);
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(inputString);
delay(1000);
lcd.clear();
lcd.home();
lcd.print(inputString);
}
}
void writeEEPROMAndClear() {
if (inputString.toInt() > 5000)
{
lcd.clear();
lcd.home();
lcd.print("Value is too large!");
delay(1000);
return;
}
if (inputString.toInt() >= 0 && inputString.toInt() <= 5000)
{
String msg = "";
eEPROMValue = inputString.toInt();
if (!use_external_eeprom)
{
EEPROM_writeAnything(eEPROMaddrA , eEPROMValue );
msg = "internal EEPROM";
}
else
{
EEPROM_writeAnything(eEPROMaddrAA , eEPROMValue );
msg = "external EEPROM";
}
lcd.clear();
lcd.home();
lcd.print("Saved value to");
lcd.setCursor(0, 1);
lcd.print(msg);
delay(1000);
inputString = "";
lcd.clear();
lcd.home();
}
}