Hi,
I'm looking to learn on how to install an LCD menu on my projects.
I have many projects working now but I want to enhance them with new feature like an LCD menu.
Example:
If in one of my project I have a condition like;
if (PSI == 3000){
do something
}
There is no probleme and my device will do what I want when it reach 3000PSI
But If I want to change that value for 2200, I need to change it in my code and instead, I would like
to change that with my push button.
I looked on a few examples and tutorials and I found one interresting. In this sketch I have 10 items and with 4 push buttons I can navigate from Item_0 to Item_9 and for each of them I can change the value.
This is working nice but I need to expand it and learn more about it in order to integrate it into my real projects.
That's the code I found, I change it a little and it's working nice
/*
* From the tutorial of EEEnthusiast
* Arduino LCD Menu system Implemented
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
//Input & Button Logic
const int numOfInputs = 4;
const int inputPins[numOfInputs] = {8,9,10,11};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW,LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW,LOW};
long lastDebounceTime[numOfInputs] = {0,0,0,0};
long debounceDelay = 5;
//LCD Menu Logic
const int numOfScreens = 10;
int currentScreen = 0;
String screens[numOfScreens][2] = {
{"Item_0","Value"},
{"Item_1","Value"},
{"Item_2","Value"},
{"Item_3","Value"},
{"Item_4","Value"},
{"Item_5","Value"},
{"Item_6","Value"},
{"Item_7","Value"},
{"Item_8","Value"},
{"Item_9","Value"}
};
int parameters[numOfScreens];
void setup(){
Serial.begin(9600);
lcd.init();
lcd.backlight();
for(int i = 0; i < numOfInputs; i++) {
pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH);
}
}
void setInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
void resolveInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
if(inputFlags[i] == HIGH) {
inputAction(i);
inputFlags[i] = LOW;
printScreen();
}
}
}
void inputAction(int input) { //input button
if(input == 0) {
if (currentScreen == 0) {
currentScreen = numOfScreens-1;
}else{
currentScreen--;
}
}else if(input == 1) {
if (currentScreen == numOfScreens-1) {
currentScreen = 0;
}else{
currentScreen++;
}
}else if(input == 2) {
parameterChange(0);
}else if(input == 3) {
parameterChange(1);
}
}
void parameterChange(int key) {
if(key == 0) {
parameters[currentScreen]++;
}else if(key == 1) {
parameters[currentScreen]--;
}
}
void printScreen() {
lcd.clear();
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1);
lcd.print(parameters[currentScreen]);
lcd.print(" ");
lcd.print(screens[currentScreen][1]);
serialPrint();
}
void serialPrint(){
Serial.print("Item ");
Serial.print(currentScreen);
Serial.print(" Value = ");
Serial.println(parameters[currentScreen]);
Serial.println(" ");
Serial.println(" ");
}
void loop() {
setInputFlags();
resolveInputFlags();
}
I know it will be very wide questions but I need to start somewhere.
I created a Serial.print to see what was store where and I found that the value that are changed in the menu are stored in parameters[currentScreen]
The value stay in memory for each item so If I increase the parameters to 5 in item_4 and to 7 in Item_5, it will stay in each item until I reboot the arduino.
Questions
How can I take the value for each item and store it in variables so I can use it for my example with the If condition?
It stays in memory until I reboot the Arduino but how can I store the variables in a way that it will stay
that way until I go back in the menu to change it again. I don't want it to be erase when I reboot the arduino.
Any suggestions
Thanks,