#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 MINTEMP = 20;
const int MAXTEMP = 40;
const int MINHUM = 20;
const int MAXHUM = 85;
const int MINDAY = 1;
const int MAXDAY = 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 = 2;
}
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("Showing Main Menu");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Setting :");
lcd.setCursor(0, 1);
lcd.println(" Temp| Hum| Day");
//lcd.println("hgfgfhjgjjkhkjg");
}
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;
if(SETTEMP > MAXTEMP){SETTEMP = MAXTEMP;}
lcdprint("Temperature", "Temp", dtostrf(SETTEMP, 2, 2, value));
} else if (submenuselection == 1) {
Serial.println("Humidity Increment");
SETHUM = SETHUM + 0.5;
if(SETHUM > MAXHUM){SETHUM = MAXHUM;}
lcdprint("Humidity", "Hum", dtostrf(SETHUM, 2, 2, value));
} else if (submenuselection == 2) {
Serial.println("Day Increment");
SETDAY = SETDAY + 1;
if(SETDAY > MAXDAY){SETDAY = MAXDAY;}
lcdprint("Days", "Day", dtostrf(SETDAY, 2, 0, value));
}
} else if (op == 1) {
if (submenuselection == 0) {
Serial.println("Temperature Decrement");
SETTEMP = SETTEMP - 0.5;
if(SETTEMP < MINTEMP){SETTEMP = MINTEMP;}
lcdprint("Temperature", "Temp", dtostrf(SETTEMP, 2, 2, value));
} else if (submenuselection == 1) {
Serial.println("Humidity Decrement");
SETHUM = SETHUM - 0.5;
if(SETHUM < MINHUM){SETHUM = MINHUM;}
lcdprint("Humidity", "Hum", dtostrf(SETHUM, 2, 2, value));
} else if (submenuselection == 2) {
Serial.println("Day Decrement");
SETDAY = SETDAY - 1;
if(SETDAY < MINDAY){SETDAY = MINDAY;}
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(' ');
} 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;
}