#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#define Main_Page_CNT 3
#define SUB_Page1_CNT 4
#define SUB_Page2_CNT 2
LiquidCrystal_I2C lcd(0x27 , 16,4);
// setup the enum with all the menu pages options
enum pageType {Main_Page, SUBPage1, SUBPage2};
//hold which page is currently selected
enum pageType currPage = Main_Page;
// constants holding pin numbers
const int ARROW_UP = 32;
const int ARROW_DOWN = 35;
const int ENTER_MENU = 34;
const int BACKLIGHT = 33;
const int cancel = 25;
// ========================================================================
// || SETUP ||
// ========================================================================
void setup() {
// INIT THE SERIAL AND THE LCD
Serial.begin(115200);
lcd.init();
// SETUP THE BUTTON I/O'S
pinMode (ARROW_UP, INPUT_PULLUP);
pinMode (ARROW_DOWN, INPUT_PULLUP);
pinMode (ENTER_MENU, INPUT_PULLUP);
pinMode (BACKLIGHT, INPUT_PULLUP);
pinMode (CANCEL, INPUT_PULLUP);
}
// ========================================================================
// || MAIN LOOP ||
// ========================================================================
void loop() {
// put your main code here, to run repeatedly:
switch (currPage){
case Main_Page: page_MainPage(); break;
case SUBPage1: page_PumpsList(); break;
case SUBPage2: page_Confirm(); break;
}
}
// ========================================================================
// || page_MainPage ||
// ========================================================================
void page_MainPage(){
//flag for updating the display
boolean updateDisplay = true;
// tracks when enter top of loop
unsigned long loopStart;
//tracks button states
boolean ARROW_UP_WasDown = false;
boolean ARROW_DOWN_WasDown = false;
boolean ENTER_MENU_WasDown = false;
// selected item pointer
uint8_t sub_Pos = 1;
// INNER LOOP
while (true) {
// capture start time
loopStart = millis();
// print the display
if (updateDisplay){
// clear the update flag
updateDisplay = false;
// clear the display
// lcd.clear();
// print the items
lcd.setCursor(0,0);
printSelected(1, sub_Pos); lcd.print("pump1 = ");
lcd.setCursor(0,1);
printSelected(2, sub_Pos); lcd.print("pump2 = ");
lcd.setCursor(0,2);
printSelected(3, sub_Pos); lcd.print("pump3 = ");
lcd.setCursor(0,3);
printSelected(4, sub_Pos); lcd.print("pump4 = ");
}
// CAPTURE THE BUTTON DOWN STATES
if (btnIsDown(ARROW_UP)) {ARROW_UP_WasDown = true;}
if (btnIsDown(ARROW_DOWN)) {ARROW_DOWN_WasDown = true;}
if (btnIsDown(ENTER_MENU)) {ENTER_MENU_WasDown = true;}
// move the pointer down
if (ARROW_DOWN_WasDown && btnIsUp(ARROW_DOWN)){
if (sub_Pos == Main_Page_CNT) {sub_Pos = 1;} else {sub_Pos ++;}
updateDisplay = true;
ARROW_DOWN_WasDown = false;
}
// move the pointer up //not working!!!!!!!!
/* if (ARROW_UP_WasDown && btnIsUp(ARROW_UP)){
if (sub_Pos == SUB_Page2_CNT) {sub_Pos = 4;} else {sub_Pos --;}
updateDisplay = true;
ARROW_UP_WasDown = false;
}
*/
// keep a specific pace
while (millis() - loopStart < 25)
{delay (2);} // change to wait(2)
}
}
// ========================================================================
// || page_PumpsList ||
// ========================================================================
void page_PumpsList(){
}
// ========================================================================
// || SETUP ||
// ========================================================================
void page_Confirm() {
}
// ========================================================================
// || TOOLS-DISPLAY ||
// ========================================================================
void printSelected(int p1, int p2){
if(p1 == p2){
// lcd.setCursor(0,0);
lcd.print ("> ");
}
else{
lcd.print(" ");
}
}
// ========================================================================
// || TOOLS-BUTTON PRESSING ||
// ========================================================================
boolean btnIsDown(int btn){
return digitalRead(btn) == LOW && digitalRead(btn)== LOW;
}
boolean btnIsUp(int btn){
return digitalRead(btn) == HIGH && digitalRead(btn)== HIGH;
}