#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

const int btn_SelectMenu = 2; bool etat_btn_SelectMenu;
const int btn_DragUpMenu = 3; bool etat_btn_DragUpMenu;
const int btn_StartStop = 4;   bool etat_btn_StartStop;
const int btn_Reset = 5;       bool etat_btn_Reset;
bool etatBtnPresser;

const int buzzerPin = 8;
// Liste des fonction au menu
char* selectMenu[] = {"Chronometre", "Minuteur", "Minuteur Cyclique",
                      "Chrono & Minuteur", "Test Alarm !", "press again"
                     };
//Liste des entêtes
char* header[6] = {"Menu principal Fn", "ALARM TESTING!", "CHRONOMETRE",
                   "MINUTEUR", "MINUTEUR CYCLIQUE", "CHRONO & MINUTEUR"
                  };

char* instructions[6] = {"                   ", "For select function",
                         "Press Yellow button", "For this function",
                         "Press red button", "Or Yellow button"
                        };

int indexMenu = 0;

char MyFunction[20];
char tempsON[20], tempsOFF[20];
char Chrono[20], MinuteurON[20], MinuteurOFF[20];

// Variables pour centrer du texte
// wide = 16 pour un afficheur à 16 caractères par ligne
// wide = 20 pour un afficheur à 20 caractères par ligne
int wide = 20, ligne = 0 ; // position du curseur
String text = "";
int len = text.length();
long onTime, offTime;
long MyTimeON, H_ON = 0, M_ON = 0, S_ON = 0, MS_ON = 0;
long MyTimeOFF, H_OFF = 0, M_OFF = 0, S_OFF = 0, MS_OFF = 0;
long minutes_OFF = 0, minutes_ON = 0;
long TempsEcoule;


long Milliseconde, Seconde, Minute, Heure;
unsigned long currentTime = 0, previousTime = 0;
unsigned long startMillis, currentMillis;
unsigned long period = 5000;

void setup() {
  lcd.init(); lcd.backlight();
  pinMode(btn_SelectMenu, INPUT);
  pinMode(btn_DragUpMenu, INPUT);
  //pinMode(btn_DragDnMenu, INPUT);
  pinMode(btn_StartStop, INPUT_PULLUP);
  pinMode(btn_Reset, INPUT_PULLUP);
  pinMode(buzzerPin, OUTPUT);

  // écran de démarrage...
  beep(); printCenter(header[0], 0); delay(1000); beep();
  printCenter(instructions[1], 2); delay(1000); beep();
  printCenter(instructions[2], 3); delay(1000);
}

// fonction perso pour centrer le texte à afficher.
void printCenter(String text, int ligne) {
  len = text.length(); lcd.setCursor((20 - len) / 2, ligne);
  lcd.print(text);
}

void loop() {
  Fn_Boutons();

  
}

void Fn_Boutons() {
  etat_btn_SelectMenu = digitalRead(btn_SelectMenu);
  etat_btn_DragUpMenu = digitalRead(btn_DragUpMenu);
  etat_btn_StartStop = digitalRead(btn_StartStop);
  etat_btn_Reset = digitalRead(btn_Reset);

  //etat_btn_DragDnMenu = digitalRead(btn_DragDnMenu);

  // RED button/bouton rouge ////////////////////////
  if (etat_btn_DragUpMenu) {
    Fn_Display();

    indexMenu++;
    if (indexMenu >= 6) {
      indexMenu = 0;
      lcd.setCursor(19, 0); lcd.print(indexMenu);

    }
    //Fn_Display();

    delay(500);
    while (etat_btn_DragUpMenu)etat_btn_DragUpMenu = digitalRead(btn_DragUpMenu);
  }
  // RED button/bouton rouge ////////////////////////

  // Fonctions disponible...
  if (etat_btn_SelectMenu) {
    Select_Fn();
    delay(500);
    while (etat_btn_SelectMenu)etat_btn_SelectMenu = digitalRead(btn_SelectMenu);
  }
}
void Select_Fn() {
  switch (indexMenu) {
    case 0: ///Fn_Chrono();
      break;
    case 1: lcd.clear();printCenter(header[2], 0);Fn_Chrono();
      break;
    case 2: Fn_Minuteur();
      break;
    case 5: Alarm();
      Fn_Display();
      break;
  }
}

void Fn_Display() {
  printCenter(header[0], 0);
  lcd.setCursor(19, 0); lcd.print(indexMenu + 1);
  beep(); // Émet une tonalité de 1 kHz pendant 100 ms
  /*
    if (indexMenu > 4) {
    indexMenu = 0;
    }
  */
  printCenter(instructions[0], 1); // lcd.setCursor(6, 1); lcd.print("              ");
  printCenter(selectMenu[indexMenu], 1); // sprintf(MyFunction, "%1d %s", indexMenu + 1, selectMenu[indexMenu] );
  //lcd.setCursor(0, 1); lcd.print(MyFunction);
  delay(1000);
  beep();
  lcd.setCursor(0, 2); lcd.print("  For this function ");
  lcd.setCursor(0, 3); lcd.print("  Press red button  ");
  delay(1000);
  beep();
  lcd.setCursor(0, 2); lcd.print("                    ");
  lcd.setCursor(0, 3); lcd.print("  Or Yellow button  ");
  //lcd.setCursor(19, 0); lcd.print(indexMenu);
}

void beep() { // Émet une tonalité de 1 kHz pendant 100 ms
  tone(buzzerPin, 1000, 100); // Plays 1kHz tone for 100ms
}

void Alarm() {
  lcd.clear(); printCenter(header[1], 0);
  //delay(500);
  for (int repeat = 0; repeat <= 10; ++repeat) {
    tone(buzzerPin, 800, 300); delay(200);
    tone(buzzerPin, 500, 300); delay(200);
    for (int flash = 0; flash <= 10; ++flash) {
      lcd.noBacklight();
    }
    delay(100);
    lcd.backlight();
  }
}

void Fn_Chrono() {
  
  
  
  
  
  
  //printCenter(header[2], 0);
  sprintf(Chrono, "%02ld:%02ld:%02ld:%03ld", Heure, Minute, Seconde, Milliseconde);//Formate l'affichage.
  printCenter(Chrono, 1);
  delay(100);


}
void Fn_Minuteur() {
  lcd.clear();
  printCenter(header[3], 0);
  sprintf(MinuteurON, "Timer: %02ld:%02ld:%02ld", H_ON, M_ON, S_ON); //Formate l'affichage.
  // sprintf(MinuteurOFF, "T-OFF: %02ld:%02ld", M_OFF, S_OFF); //Formate l'affichage.
  printCenter(MinuteurON, 2);
  //printCenter(MinuteurOFF,3);


}

$abcdeabcde151015202530354045505560fghijfghij