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

// Define the I2C LCD object
LiquidCrystal_I2C lcd(0x27, 20, 4);  // I2C address may vary, adjust if necessary

// Define the keypad layout
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};     // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2};     // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Variables
int menuState = 0;     // Current menu state
int selection = 0;     // Current selected option
bool stopped = 0;  // Flag to indicate if it's stopped
int Led_1 = 13;

void setup() {
  lcd.init();   // Initialize the LCD
  lcd.backlight();
  lcd.setCursor(5,1);
  lcd.print("Pressure");
  lcd.setCursor(4,2);
  lcd.print("Tester v1.0");
  delay(2000);
  updateMenu();
}

void loop() {
   pinMode(Led_1, OUTPUT);
  char key = keypad.getKey();  // Read the keypad input

  if (key != NO_KEY) {
    // Handle key press
    switch (key) {
      case 'A':
        upKeyPressed();
        break;
      case 'B':
        downKeyPressed();
        break;
      case 'C':
        enterKeyPressed();
        break;
      case 'D':
        dKeyPressed();
        break;
      default:
        break;
    }
  }
}

void upKeyPressed() {
  if (menuState == 0) {
    if (selection > 0) {
      selection--;
      updateMenu();
    }
  }
}

void downKeyPressed() {
  if (menuState == 0) {
    if (selection < 1) {
      selection++;
      updateMenu();
    }
  }
}

void enterKeyPressed() {
  if (menuState == 0) {
    switch (selection) {
      case 0:
        lcd.clear();
        lcd.print("Manual Input:");
        // Perform action for Manual Input
        digitalWrite(Led_1, HIGH);
        break;
      case 1:
        lcd.clear();
        lcd.print("Burst Test:");
        // Perform action for Burst Test
        break;
      default:
        break;
    }
    menuState = 1;  // Change menu state to indicate selection made
  }
}

void dKeyPressed() {
  if (menuState ==1) {
    if (stopped==0){ 
      lcd.clear();
      lcd.setCursor(4,0);
      lcd.print(" Test Ended ");
      lcd.setCursor(2,1);
      lcd.print("Last press val:");
      lcd.setCursor(7,2);
      lcd.print("X mBar");
      // Perform action for stop
      digitalWrite(Led_1, LOW);
      stopped = 1;
     
    } else{
      lcd.clear();
      stopped = 0;
      updateMenu();
      menuState=0;
    
    }
  }
}

void updateMenu() {
  lcd.clear();
  lcd.print("Menu:");
  lcd.setCursor(0, 1);
  if (selection == 0) {
    lcd.print("> Manual Input");
    lcd.setCursor(0, 2);
    lcd.print("  Burst Test");
  } else {
    lcd.print("  Manual Input");
    lcd.setCursor(0, 2);
    lcd.print("> Burst Test");
  }
}
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
keypad:R1
keypad:R2
keypad:R3
keypad:R4
keypad:C1
keypad:C2
keypad:C3
keypad:C4
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
led1:A
led1:C