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

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Setup the encoder with the pins connected
Encoder myEnc(2, 3);

// Define the menus
String mainMenu[] = {"channel 1", "channel 2", "channel 3", "channel 4", "Start/Stop"};
String patternMenu[] = {"off", "DI", "stop", "Po / tail", "fog"};

// Variables to hold the current position in the menus
int mainMenuPos = 0;
int patternMenuPos = 0;

// Variables to hold the current state of the LEDs
bool ledState[] = {false, false, false, false};

void setup()
{
  lcd.begin(16,2);  
  lcd.print("Welcome!");
  delay(1000);

  // Initialize the LED pins as outputs
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  // Check the encoder for changes
  long newPos = myEnc.read();
  if (newPos > mainMenuPos) {
    mainMenuPos++;
    if (mainMenuPos > 4) mainMenuPos = 0;
  } else if (newPos < mainMenuPos) {
    mainMenuPos--;
    if (mainMenuPos < 0) mainMenuPos = 4;
  }

  // Display the current menu option
  lcd.clear();
  lcd.print(mainMenu[mainMenuPos]);

  // Wait for a button press (assuming it's connected to pin 4)
  if (digitalRead(4) == HIGH) {
    // If a button press is detected, go into the pattern menu
    while (digitalRead(4) == HIGH) {
      // Same as above, but for the pattern menu
      newPos = myEnc.read();
      if (newPos > patternMenuPos) {
        patternMenuPos++;
        if (patternMenuPos > 4) patternMenuPos = 0;
      } else if (newPos < patternMenuPos) {
        patternMenuPos--;
        if (patternMenuPos < 0) patternMenuPos = 4;
      }

      lcd.clear();
      lcd.print(patternMenu[patternMenuPos]);
      delay(100);
    }

    // After a pattern is selected, set the LED state for the selected channel
    ledState[mainMenuPos] = patternMenuPos != 0; // The LED is off if the pattern is "off", on otherwise
  }

  // Update the LEDs
  digitalWrite(8, ledState[0]);
  digitalWrite(9, ledState[1]);
  digitalWrite(10, ledState[2]);
  digitalWrite(11, ledState[3]);

  delay(100);
}
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
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
led4:A
led4:C
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
encoder1:CLK
encoder1:DT
encoder1:SW
encoder1:VCC
encoder1:GND