Hi Everyone,
With the help and guidance from LightuC I am attempting to work on a very simple menu for myself since the others that I have seen were overkill for me to really understand and work with. So far the test menu seems to be working as I go from screen to screen, I am having a few difficulties however.
This setup is just an lCD screen with 3 push buttons. The LCD only has 4 pins to connect to the arduino.
I took out some of the comments to keep the code a bit more compact. I do have two requests if I can get some help with them i will have made my menu system.
- My function below I am not getting my value to flip back and forth from auto to manual.
void updateBoolValue()
- While looking at the code how do I get everything a bit more compact without having code duplicated? Can someone offer some guidance? I was just trying to get what I was working on to work and I am sure there are areas that can be coded better.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define btnSelect 2 // select push button to scroll menu pages
#define btnUp 3 // up pushbutton to increment values
#define btnDown 4 // down pushbutton to decrement values
bool btn_Select_Pressed = false; // keep track if the button has been pressed
int btnSelectCurState; // the current reading from the input pin
int btnSelectLastState = HIGH; // the previous reading from the select button
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50;
bool btn_Up_Pressed = false; // keep track if the button has been pressed
int btnUpCurState; // the current reading from the input pin
int btnUpLastState = HIGH; // the previous reading from the select button
int menuScreen = 0;
int value = 0;
bool disp;
#define DEBUG
bool isSelectButtonPressed()
{
int btnSelectState = digitalRead(btnSelect);
if(btnSelectState != btnSelectLastState)
{
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay)
{
if(btnSelectState != btnSelectCurState)
{
btnSelectCurState = btnSelectState;
if(btnSelectCurState == LOW)
{
btn_Select_Pressed = true;
menuScreen++;
if((menuScreen < 0) || (menuScreen > 2))
{
menuScreen = 0;
}
}
}
}
btnSelectLastState = btnSelectState;
if(btn_Select_Pressed == true)
{
#ifdef DEBUG
Serial.print("btn_Select_Pressed = ");
Serial.println(btn_Select_Pressed);
#endif
btn_Select_Pressed = false; // reset the variable back to false
return true;
}
else
{
return false;
}
}
bool isButtonUpPressed()
{
int btnUpState = digitalRead(btnUp);
if(btnUpState != btnUpLastState)
{
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay)
{
if(btnUpState != btnUpCurState)
{
btnUpCurState = btnUpState;
if(btnUpCurState == LOW)
{
btn_Up_Pressed = true;
}
}
}
btnUpLastState = btnUpState;
if(btn_Up_Pressed == true)
{
#ifdef DEBUG
Serial.print("btn_Up_Pressed = ");
Serial.println(btn_Up_Pressed);
#endif
btn_Up_Pressed = false; // reset the variable back to false
return true;
}
else
{
return false;
}
}
void getNextMenuPage()
{
switch(menuScreen)
{
case 0:
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(" *Main Screen *");
lcd.setCursor(4, 2);
lcd.print("Testing the");
lcd.setCursor(4, 3);
lcd.print("Main Screen");
break;
case 1:
lcd.clear();
lcd.setCursor(2, 2);
lcd.print(" ");
lcd.setCursor(2, 2);
lcd.print("Config Screen");
lcd.setCursor(0, 3);
lcd.print(value);
break;
case 2:
lcd.setCursor(2, 2);
lcd.print("Runtime Screen");
lcd.setCursor(0, 3);
lcd.print(disp);
break;
}
}
void updatePageValue()
{
if(menuScreen == 1)
{
value++;
lcd.setCursor(0, 3);
lcd.print(value);
}
}
void updateBoolValue()
{
#ifdef DEBUG
Serial.print("disp value = ");
Serial.println(disp);
#endif
lcd.setCursor(0, 3);
lcd.print((disp == false) ? "MANUAL" : "AUTO" );
}
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
#endif
pinMode(btnSelect, INPUT_PULLUP);
pinMode(btnUp, INPUT_PULLUP);
pinMode(btnDown, INPUT_PULLUP);
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
lcd.setCursor(2, 0);
lcd.print("**Main Screen**");
#ifdef DEBUG
Serial.println("Ready.");
#endif
}
void loop()
{
if(isSelectButtonPressed())
{
getNextMenuPage();
}
if(isButtonUpPressed())
{
if(menuScreen == 1)
{
updatePageValue();
}
else if(menuScreen == 2)
{
updateBoolValue();
}
}
}
Thanks
John