#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

int upButton = 2;
int downButton = 3;
int selectButton = 4;
int backButton = 5;
int analogPin = A3;
int val = 0;
int menu = 1;
int stop = 0;



void setup() {
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print(" Pressure ");
  lcd.setCursor(3,1);
  lcd.print("Controller");
  delay(1500);
  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(selectButton, INPUT_PULLUP);
  pinMode(backButton, INPUT_PULLUP);
  updateMenu();
}

void loop() {
  //val = analogRead(analogPin); 
  //Serial.println(val);
  if (!digitalRead(downButton)){
    menu++;
    updateMenu();
    delay(100);
    while (!digitalRead(downButton));
  }
  if (!digitalRead(upButton)){
    menu--;
    updateMenu();
    delay(100);
    while(!digitalRead(upButton));
  }
  if (!digitalRead(selectButton)){
    executeAction();
    //updateMenu();
    delay(100);
    while (!digitalRead(selectButton));
  }
  if (!digitalRead(backButton)){
    if (stop==1) {
      lcd.clear();
      lcd.setCursor(4,0);
      lcd.print("Test");
      lcd.setCursor(3,1);
      lcd.print("Stopped");
      stop=0;
    }
    else
    {
      updateMenu();
    }
    delay(100);
    while (!digitalRead(backButton));
  }

}

void updateMenu() {
  switch (menu) {
    case 0:
      menu = 1;
      break;
    case 1:
      lcd.clear();
      lcd.print("->25 mBar Test");
      lcd.setCursor(0, 1);
      lcd.print("  50 mBar Test");
      break;
    case 2:
      lcd.clear();
      lcd.print("  25 mBar Test");
      lcd.setCursor(0, 1);
      lcd.print("->50 mBar Test");
      break;
    case 3:
      lcd.clear();
      lcd.print("->100 mBar Test");
      lcd.setCursor(0, 1);
      lcd.print("  Burst Test");
      break;
    case 4:
      lcd.clear();
      lcd.print("  100 mBar Test");
      lcd.setCursor(0, 1);
      lcd.print("->Burst Test");
      break;
    case 5:
      menu = 4;
      break;
  }
}

void executeAction() {
  switch (menu) {
    case 1:
      action1();
      break;
    case 2:
      action2();
      break;
    case 3:
      action3();
      break;
    case 4:
      action4();
      break;
  }
}

void action1() {
  lcd.clear();
  lcd.print("25mBar Test Run");
    // read the input pin
            // debug value
  lcd.setCursor(1,1);
  lcd.print("P:");
  lcd.print(val);
  stop=1;
  
}
void action2() {
  lcd.clear();
  lcd.print("Executing #2");
  stop=1;
 
}
void action3() {
  lcd.clear();
  lcd.print("Executing #3");
  stop=1; 
}
void action4() {
  lcd.clear();
  lcd.print("Executing #4");
  stop=1;
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r