#include "Wire.h"
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C LCD(0x27, 16, 2);

#include "RotaryEncoder.h"
RotaryEncoder encoder(4, 5);  // DT, CLK

#define STEPS  6
#define POSMIN 0
#define POSMAX 12
#define STEPS_2  10
#define POSMIN_2 0
#define POSMAX_2 250

int lastPos, newPos;
boolean buttonWasUp = true;
byte w = 2;

void setup() {
  LCD.init();
  LCD.backlight();

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(2, INPUT_PULLUP);

  Serial.begin(9600);
  encoder.setPosition(10 / STEPS);
}

void loop() {

   // CYCLE AVEC MENU PRINCIPAL À L'ÉCRAN
  while (w == 0) {
    LCD.setCursor(0, 0);
    LCD.print("LED1  LED2  LED3");
    encoder.tick();
    newPos = encoder.getPosition() * STEPS;
    if (newPos < POSMIN) {
      encoder.setPosition(POSMIN / STEPS);
      newPos = POSMIN;
    }
    else if (newPos > POSMAX) {
      encoder.setPosition(POSMAX / STEPS);
      newPos = POSMAX;
    }
    if (lastPos != newPos) {
      Serial.println(newPos);
      LCD.setCursor(lastPos, 1);
      LCD.print("    ");
      LCD.setCursor(newPos, 1);
      LCD.print("====");
      lastPos = newPos;
    }
    boolean buttonIsUp = digitalRead(2);
    if (buttonWasUp && !buttonIsUp) {
      delay(10);
      buttonIsUp = digitalRead(2);
      if (!buttonIsUp  && newPos == 0)  { LCD.clear(); delay(500); w = 1; }
      if (!buttonIsUp  && newPos == 6)  { LCD.clear(); delay(500); w = 2; }
      if (!buttonIsUp  && newPos == 12) { LCD.clear(); delay(500); w = 3; }
    }
  }

   // MENÚ ANIDADO CON UN PRIMER LED
  while (w == 1) {
    LCD.setCursor(0, 0);
    LCD.print("LED1  - ");
    encoder.tick();
    newPos = encoder.getPosition() * STEPS_2;
    if (newPos < POSMIN_2) {
      encoder.setPosition(POSMIN_2 / STEPS_2);
      newPos = POSMIN_2;
    }
    else if (newPos > POSMAX_2) {
      encoder.setPosition(POSMAX_2 / STEPS_2);
      newPos = POSMAX_2;
    }
    if (lastPos != newPos) {
      LCD.setCursor(7, 0);
      LCD.print(newPos);
      analogWrite(9, newPos);
    }
    boolean buttonIsUp = digitalRead(2);
    if (buttonWasUp && !buttonIsUp) {
      delay(10);
      buttonIsUp = digitalRead(2);
      if (!buttonIsUp) { LCD.clear();digitalWrite(9,LOW);delay(500);w=0; }
  } 
}

   // MENÚ ANIDADO CON UN SEGUNDO LED
  while (w == 2) {
    LCD.setCursor(0, 0);
    LCD.print("LED2 - ");
    encoder.tick();
    newPos = encoder.getPosition() * STEPS_2;
    if (newPos < POSMIN_2) {
      encoder.setPosition(POSMIN_2 / STEPS_2);
      newPos = POSMIN_2;
    }
    else if (newPos > POSMAX_2) {
      encoder.setPosition(POSMAX_2 / STEPS_2);
      newPos = POSMAX_2;
    }
    if (lastPos != newPos) {
      LCD.setCursor(7, 0);
      LCD.print(newPos);
      analogWrite(10, newPos);
    }
    boolean buttonIsUp = digitalRead(2);
    if (buttonWasUp && !buttonIsUp) {
      delay(10);
      buttonIsUp = digitalRead(2);
      if (!buttonIsUp) { LCD.clear();digitalWrite(10,LOW);delay(500);w=0; }
  }
}

   // MENÚ ANIDADO CON UN TERCER LED
  while (w == 3) {
    LCD.setCursor(0, 0);
    LCD.print("LED3 - ");
    encoder.tick();
    newPos = encoder.getPosition() * STEPS_2;
    if (newPos < POSMIN_2) {
      encoder.setPosition(POSMIN_2 / STEPS_2);
      newPos = POSMIN_2;
    }
    else if (newPos > POSMAX_2) {
      encoder.setPosition(POSMAX_2 / STEPS_2);
      newPos = POSMAX_2;
    }
    if (lastPos != newPos) {
      LCD.setCursor(7, 0);
      LCD.print(newPos);
      analogWrite(11, newPos);
    }
    boolean buttonIsUp = digitalRead(2);
    if (buttonWasUp && !buttonIsUp) {
      delay(10);
      buttonIsUp = digitalRead(2);
      if (!buttonIsUp) { LCD.clear();digitalWrite(11,LOW);delay(500);w=0; }
  } 
}

}