#include <Arduino.h>
#include <Wire.h>
//#include <LiquidCrystal_PCF8574.h>
#include <LiquidCrystal_I2C.h>
//for buttons
#define buttonPin 23 // the number of the pushbutton pin
#define button2Pin 25 // the number of the pushbutton pin
#define button3Pin 26 // the number of the pushbutton pin
uint8_t debounceDelay = 50;
// variables will change:
uint8_t buttonState = 0; // variable for reading the pushbutton status
uint8_t lastButtonState = 0; // previous state of the button
uint8_t buttonState2 = 0; // variable for reading the pushbutton status
uint8_t lastButtonState2 = 0; // previous state of the button
uint8_t buttonState3 = 0; // variable for reading the pushbutton status
uint8_t lastButtonState3 = 0; // previous state of the button
int lastMillis = 0;
// Timers auxiliar variables
int counter = 0;
int lcdCounter = 0; //LCD
unsigned long next_measurement;
unsigned long lcd_off; //LCD
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000;
unsigned long lastMeasure = 0;
unsigned long lastLcdMeasure = 0; //LCD
unsigned int measuretime = 3600000; //60 min
unsigned int lcdTimer = 300000; //LCD
//LCD variables
bool LCDstate = false;
//menu variables
uint8_t menu_level = 0;
uint8_t menu_sublevel = 0;
uint8_t button1 = 0;
uint8_t button2 = 0;
uint8_t button3 = 0;
const char* devicename = "lajosmizse3";
// set the LCD number of columns and rows
int lcdColumns = 20;
int lcdRows = 4;
int stringStart, stringStop = 0;
int scrollCursor = lcdColumns;
//LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// custom characters
byte arrow_down[] = {
B00100,
B00100,
B00100,
B00100,
B00100,
B10101,
B01110,
B00100
};
byte arrow_right[] = {
B10000,
B11100,
B11110,
B11111,
B11111,
B11110,
B11100,
B10000
};
byte enter[] = {
B00001,
B00001,
B00001,
B00101,
B01001,
B11111,
B01000,
B00100
};
byte back[] = {
B00100,
B01100,
B11111,
B01101,
B00101,
B10001,
B10001,
B11111
};
void IRAM_ATTR function_ISR() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(button2Pin);
buttonState3 = digitalRead(button3Pin);
if (buttonState != lastButtonState || buttonState2 != lastButtonState2 || buttonState3 != lastButtonState3) {
if((millis() - lastMillis) > debounceDelay){ // Software debouncing buton
if (buttonState == HIGH) {
Serial.println("Megnyomtad az első gombot");
ets_printf("ISR triggered\n");
button1 = 1;
LCDstate = true;
lcdCounter = 0;
// buttonState = !buttonState;
}
if (buttonState2 == HIGH) {
Serial.println("Megnyomtad a második gombot");
ets_printf("ISR triggered\n");
button2 = 1;
LCDstate = true;
lcdCounter = 0;
// buttonState = !buttonState;
}
if (buttonState3 == HIGH) {
Serial.println("Megnyomtad a harmadik gombot");
ets_printf("ISR triggered\n");
button3 = 1;
LCDstate = true;
lcdCounter = 0;
// buttonState = !buttonState;
}
}
lastMillis = millis();
}
lastButtonState = buttonState;
lastButtonState2 = buttonState2;
lastButtonState3 = buttonState3;
}
void lcd_clr(){
lcd.clear();
lcd.noBlink();
lcd.noCursor();
}
void lcd_print(int row, String message) {
lcd.setCursor(0,row);
lcd.print(" ");
lcd.setCursor(0, row);
lcd.print(message);
}
// row = row; message = LCD print message, ownChar= own characters number, bool front_back = 0 front ownChar 1 back ownChar
void lcd_write(int row, String message, uint8_t ownChar, bool front_back) {
lcd.setCursor(0,row);
lcd.print(" ");
lcd.setCursor(0, row);
if (front_back == 0){
lcd.write(ownChar);
lcd.print(message);
} else {
lcd.print(message);
lcd.write(ownChar);
}
}
void lcd_write_button2(int row, uint8_t enable1, uint8_t enable2, uint8_t enable3) {
lcd.setCursor(0, row);
lcd.print(" ");
lcd.setCursor(0, row);
if (enable1 == 1){
lcd.write(1);
lcd.print(" ");
} else {
lcd.print(" ");
}
if (enable2 == 1){
lcd.write(3);
lcd.print(" ");
} else {
lcd.print(" ");
}
if (enable3 == 1){
lcd.write(4);
} else {
lcd.print(" ");
}
}
void init_lcd() {
/* int error;
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
// lcd.begin(16, 2); // initialize the lcd
lcd.begin(lcdColumns, lcdRows); // initialize the lcd
lcd.createChar(1, arrow_down);
lcd.createChar(2, arrow_right);
lcd.createChar(3, enter);
lcd.createChar(4, back);
} else {
Serial.println(": LCD not found.");
} // if
*/
lcd.begin();
lcd.createChar(1, arrow_down);
lcd.createChar(2, arrow_right);
lcd.createChar(3, enter);
lcd.createChar(4, back);
}
void setup(){
Serial.begin(115200);
// initialize LCD
Wire.begin(21,22); // ESP32s SDA-P21 SCL-P22 WAK-G put here the Pins of I2C
pinMode(buttonPin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
attachInterrupt(buttonPin, function_ISR, CHANGE);
attachInterrupt(button2Pin, function_ISR, CHANGE);
attachInterrupt(button3Pin, function_ISR, CHANGE);
init_lcd();
// turn on LCD backlight
lcd.setBacklight(255);
lcd.home();
lcd.clear();
delay(1000);
swLCD();
}
void loop(){
unsigned long chkTime = millis();
unsigned long chkTime2 = millis();
counter_print2(chkTime);
if (chkTime - lastMeasure > measuretime) {
counter = 0;
lastMeasure = chkTime;
}
if (chkTime2 - lastLcdMeasure > lcdTimer) {
lcdCounter = 0;
lastLcdMeasure = chkTime2;
LCDstate = false;
swLCD();
}
menu_selet();
// menu_l1();
// menu_l1_1();
// menu_l1_2();
// menu_l1_3();
// Serial.println((String)"Menu level: " + menu_level);
// Serial.println((String)"Menu sublevel: " + menu_sublevel);
delay(1000);
}
void menu_selet() {
if (menu_level == 0) {
if(button1 == 1) {
menu_level = 1;
menu_sublevel = 0;
button1 = 0;
}
}
//menu level1
if (menu_level == 1) {
//menu sublevel 0
if(menu_sublevel == 0 && button1 == 1) {
menu_level = 1;
menu_sublevel = 1;
button1 = 0;
}
if(menu_sublevel == 0 && button2 == 1) {
menu_level = 2;
menu_sublevel = 0;
button2 = 0;
}
//menu sublevel 1
if(menu_sublevel == 1 && button1 == 1) {
menu_level = 1;
menu_sublevel = 2;
button1 = 0;
}
if(menu_sublevel == 1 && button2 == 1) {
menu_level = 2;
menu_sublevel = 1;
button2 = 0;
}
//menu sublevel 2
if(menu_sublevel == 2 && button1 == 1) {
menu_level = 1;
menu_sublevel = 0;
button1 = 0;
}
if(menu_sublevel == 2 && button2 == 1) {
menu_level = 2;
menu_sublevel = 2;
button2 = 0;
}
}
//button3 == 1
if(button3 == 1) {
if (menu_level == 1) {
menu_level = 0;
menu_sublevel = 0;
button3 = 0;
}
if (menu_level == 2 && menu_sublevel == 0) {
menu_level = 1;
menu_sublevel = 0;
button3 = 0;
}
if (menu_level == 2 && menu_sublevel == 1) {
menu_level = 1;
menu_sublevel = 1;
button3 = 0;
}
if (menu_level == 2 && menu_sublevel == 2) {
menu_level = 1;
menu_sublevel = 2;
button3 = 0;
}
}
print_lcd_menu();
}
void print_lcd_menu() {
swLCD();
lcd_clr();
if (menu_level == 0) {
String line = devicename;
String line3 = ("Menu");
lcd_print(0, line);
lcd_print(3, line3);
}
if (menu_level == 1) {
String line = " Air measurement";
String line1 = " Water measurement";
String line2 = " Sensor details";
if (menu_sublevel == 0) {
lcd_write(0, line, 2, 0);
lcd_print(1, line1);
lcd_print(2, line2);
}
if (menu_sublevel == 1) {
lcd_print(0, line);
lcd_write(1, line1, 2, 0);
lcd_print(2, line2);
}
if (menu_sublevel == 2) {
lcd_print(0, line);
lcd_print(1, line1);
lcd_write(2, line2, 2, 0);
}
lcd_write_button2(3, 1, 1, 1);
}
if (menu_level == 2) {
String line = "";
if (menu_sublevel == 0) {
line = " Air measurement";
}
if (menu_sublevel == 1) {
line = " Water measurement";
}
if (menu_sublevel == 2) {
line = " Sensor details";
};
lcd_print(0, line);
lcd_write_button2(3, 0, 0, 1);
}
}
void swLCD(){
if (LCDstate == false) {
lcd.clear();
lcd.noDisplay();
lcd.setBacklight(0);
}
if (LCDstate == true) {
lcd.display();
lcd.setBacklight(255);
menu_level = 0;
menu_sublevel = 0;
button1 = 0;
button2 = 0;
button3 = 0;
}
}
void counter_print2(unsigned long _currentMillis) {
//lcdTimer = 300000;
//lcdCounter = 0;
//lcd_off;
if (_currentMillis - previousMillis >= interval) {
previousMillis = _currentMillis;
next_measurement = ((measuretime / 1000) - counter);
lcd_off = ((lcdTimer / 1000) - lcdCounter);
Serial.println ((String)"Following measure: " + next_measurement);
Serial.println ((String)"LCD blackligth will off: " + lcd_off);
counter ++;
lcdCounter ++;
}
}
/*
void counter_print(unsigned long _currentMillis) {
// unsigned long _currentMillis = millis();
if (_currentMillis - previousMillis >= interval) {
previousMillis = _currentMillis;
next_measurement = ((measuretime / 1000) - counter);
Serial.println ((String)"Following measure: " + next_measurement);
counter ++;
}
}
*/