Hello All,
I have setup a code and project to test the menu functionality at wokwi.com - push button test). The project (menu specific) there works fine except the day shows "2Aay" instead of "Day" in the menu and I couldn't figure out why. I checked all the code but I don't know why it has the problem. The first button is menu and select, the second button is next/increment, the third is prev/decrement and the last one is exit. Can anybody please take a look and advise me about the bug. Please.
1 Like
Please post your code and schematic here.
ok I Will.
Here it is
#include<LiquidCrystal_I2C.h>
#define MENUSET 7
#define INCRSET 11
#define DECRSET 13
#define EXIT 5
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool ShowMenu = 0;
bool menupressed = 0;
bool menuactive = 0;
bool exitpressed = 0;
int submenuselection = 0;
bool submenuentered = 0;
bool incrpressed = 0;
bool tempmenu = 0;
bool humidmenu = 0;
bool daymenu = 0;
bool decrpressed = 0;
float hum = 0;
float temp = 0;
// const int MIN_TEMP = 20;
// const int MAX_TEMP = 40;
// const int MIN_HUM = 20;
// const int MAX_HUM = 85;
// const int MIN_DAY = 1;
// const int MAX_DAY = 45;
float SETTEMP = 37.50;
float SETHUM = 55.00;
int SETDAY = 21;
float prevhum = 0;
float prevtemp = 0;
int day = 0;
unsigned long dayprevMillis = 0;
unsigned long daycurrentMillis = 0;
int hour = 0;
unsigned long hourprevMillis = 0;
unsigned long hourcurrentMillis = 0;
// Debounce delay in milliseconds
const unsigned long DEBOUNCE_DELAY = 300;
// Button states for debounce logic
bool menuState = LOW;
bool incrState = LOW;
bool decrState = LOW;
bool exitState = LOW;
char value[16]; // for lcd display
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(MENUSET, INPUT);
pinMode(INCRSET, INPUT);
pinMode(DECRSET, INPUT);
pinMode(EXIT, INPUT);
digitalWrite(MENUSET, LOW);
digitalWrite(INCRSET, LOW);
digitalWrite(DECRSET, LOW);
digitalWrite(EXIT, LOW);
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
pollpins();
}
void pollpins() {
// Read menu button with debounce
static unsigned long lastMenuTime = 0;
bool currentMenuState = digitalRead(MENUSET);
if (currentMenuState != menuState && millis() - lastMenuTime > DEBOUNCE_DELAY) {
menuState = currentMenuState;
if (menuState == HIGH) {
if (menuactive == true) {
submenuentered = true;
ShowSubMenu(submenuselection);
Serial.println("Sub menu entered");
} else {
menuactive = true;
MainMenu();
ShowSelectedItem();
}
}
lastMenuTime = millis();
}
// Read increment button with debounce
static unsigned long lastIncrTime = 0;
bool currentIncrState = digitalRead(INCRSET);
if (currentIncrState != incrState && millis() - lastIncrTime > DEBOUNCE_DELAY) {
incrState = currentIncrState;
if (incrState == HIGH) {
if (menuactive == 1 && submenuentered == 0) {
submenuselection++;
if (submenuselection > 2) {
submenuselection = 0;
}
ShowSelectedItem();
} else if (menuactive == 1 && submenuentered == 1) {
inc_dec(0,submenuselection);
}
}
lastIncrTime = millis();
}
// Read decrement button with debounce
static unsigned long lastDecrTime = 0;
bool currentDecrState = digitalRead(DECRSET);
if (currentDecrState != decrState && millis() - lastDecrTime > DEBOUNCE_DELAY) {
decrState = currentDecrState;
if (decrState == HIGH) {
if (menuactive == 1 && submenuentered == 0) {
submenuselection--;
if (submenuselection < 0) {
submenuselection = 1;
}
ShowSelectedItem();
} else if (menuactive == 1 && submenuentered == 1) {
inc_dec(1,submenuselection);
}
}
lastDecrTime = millis();
}
// Read exit button with debounce
static unsigned long lastExitTime = 0;
bool currentExitState = digitalRead(EXIT);
if (currentExitState != exitState && millis() - lastExitTime > DEBOUNCE_DELAY) {
exitState = currentExitState;
if (exitState == HIGH) {
submenuentered = false;
menuactive = false;
submenuselection = 0;
Serial.print("Called ShowValues()");
}
lastExitTime = millis();
//ShowValues();
lcd.clear();
lcd.print("Called Show values");
}
}
void MainMenu() {
// Lcd shows Menu
//Serial.begin(9600);
Serial.println("Main Menu Showing");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Setting :");
lcd.setCursor(0, 1);
lcd.println(" Temp| Hum| Day");
}
void ShowSubMenu(int submenuselection) {
Serial.println("Showing submenu");
if (submenuselection == 0) {
Serial.println("Temperature menu");
lcdprint("Temperature", "Temp", dtostrf(SETTEMP, 2, 1, value));
} else if (submenuselection == 1) {
Serial.println("Humidity menu");
lcdprint("Humidity", "Hum", dtostrf(SETHUM, 2, 1, value));
} else if (submenuselection == 2) {
Serial.println("Day menu");
lcdprint("Days", "Day", dtostrf(SETDAY, 2, 0, value));
}
}
void inc_dec(int op, int submenuselection) {
//Serial.println("Showing submenu Details");
if (op == 0) {
if (submenuselection == 0) {
Serial.println("Temperature Increment");
SETTEMP = SETTEMP + 0.5;
lcdprint("Temperature", "Temp", dtostrf(SETTEMP, 2, 2, value));
} else if (submenuselection == 1) {
Serial.println("Humidity Increment");
SETHUM = SETHUM + 0.5;
lcdprint("Humidity", "Hum", dtostrf(SETHUM, 2, 2, value));
} else if (submenuselection == 2) {
Serial.println("Day Increment");
SETDAY = SETDAY + 1;
lcdprint("Days", "Day", dtostrf(SETDAY, 2, 0, value));
}
} else if (op == 1) {
if (submenuselection == 0) {
Serial.println("Temperature Decrement");
SETTEMP = SETTEMP - 0.5;
lcdprint("Temperature", "Temp", dtostrf(SETTEMP, 2, 2, value));
} else if (submenuselection == 1) {
Serial.println("Humidity Decrement");
SETHUM = SETHUM - 0.5;
lcdprint("Humidity", "Hum", dtostrf(SETHUM, 2, 2, value));
} else if (submenuselection == 2) {
Serial.println("Day Decrement");
SETDAY = SETDAY - 1;
lcdprint("Days", "Day", dtostrf(SETDAY, 2, 0, value));
}
}
}
void ShowSelectedItem() {
Serial.print("Showing Submenu Selection ");
Serial.println(submenuselection);
if (submenuselection == 0) {
lcd.setCursor(0, 1);
lcd.print('>');
lcd.setCursor(6, 1);
lcd.print(' ');
lcd.setCursor(11, 1);
lcd.print(' ',11);
} else if (submenuselection == 1) {
lcd.setCursor(0, 1);
lcd.print(' ');
lcd.setCursor(6, 1);
lcd.print('>');
lcd.setCursor(11, 1);
lcd.print(' ');
} else if (submenuselection == 2) {
lcd.setCursor(0, 1);
lcd.print(' ');
lcd.setCursor(6, 1);
lcd.print(' ');
lcd.setCursor(11, 1);
lcd.print('>');
}
}
void lcdprint(char *menuname, char *mnushort, char *value) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set ");
lcd.print(menuname);
lcd.print(":");
lcd.setCursor(0, 1);
lcd.print(mnushort);
lcd.print(" -> ");
lcd.print(value);
}
int daycounter() {
daycurrentMillis = millis();
//day = millis();
if (daycurrentMillis - dayprevMillis >= 86400000) {
day++;
dayprevMillis = daycurrentMillis;
// Serial.println(day);
}
return day;
}
int hourcounter() {
hourcurrentMillis = millis();
//day = millis();
if (hourcurrentMillis - hourprevMillis >= 3600000) {
hour++;
hourprevMillis = hourcurrentMillis;
if (hour == 24) { hour = 0; }
}
Serial.println(hour);
return hour;
}
This is the library used - GitHub - johnrickman/LiquidCrystal_I2C: LiquidCrystal Arduino library for the DFRobot I2C LCD displays
Found the error, Please mark it as closed.
There is an extra parameter in the lcd.print() statement in your showselecteditem() function in the "Day" if block.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.