// Solution du laboratoire 

#include <LiquidCrystal_I2C.h>

#define BOUTON                  2
#define BOUTON_DELAI_REBOND     5
#define LCD_NB_COLONNE          20
#define LCD_NB_LIGNE            4
#define LCD_ADRESSE_I2C         0x27
#define LCD_POSITION_POT        9
#define LCD_POSITION_SEC        15
#define LCD_POSITION_MIN        12
#define LCD_POSITION_HR         9
#define LCD_DEBUT_LIGNE         0
#define LCD_LIGNE1              0
#define LCD_LIGNE2              1
#define LCD_LIGNE3              2
#define LCD_LIGNE4              3
#define LCD_TROIS_ESPACES       "   "
#define LCD_UN_ESPACE           " "

LiquidCrystal_I2C ecranLCD(LCD_ADRESSE_I2C, LCD_NB_COLONNE, LCD_NB_LIGNE);

void setup() {
    pinMode(BOUTON, INPUT);
    ecranLCD.begin(LCD_NB_COLONNE, LCD_NB_LIGNE);
    ecranLCD.backlight();
    ecranLCD.print("Pot    : ");
    ecranLCD.setCursor(LCD_DEBUT_LIGNE,LCD_LIGNE2);
    ecranLCD.print("Volume : ");
    ecranLCD.setCursor(LCD_DEBUT_LIGNE,LCD_LIGNE3);
    ecranLCD.print("--------------------");

    ecranLCD.setCursor(LCD_DEBUT_LIGNE,LCD_LIGNE4);
    ecranLCD.print("Temps  : hh:mm:ss");

} // setup()


void loop() {
    static bool etatRetroEclairage = true;
    char temps[12] = "";
    ecranLCD.setCursor(LCD_POSITION_POT,LCD_LIGNE1);
    ecranLCD.print(analogRead(A0)); 
    ecranLCD.print(LCD_TROIS_ESPACES); 
    ecranLCD.setCursor(LCD_POSITION_POT,LCD_LIGNE2);
    ecranLCD.print(map(analogRead(A0), 0, 1023, 0, 10)); 
    ecranLCD.print(LCD_UN_ESPACE); 

   ecranLCD.setCursor(LCD_POSITION_POT,LCD_LIGNE4);
   unsigned long lesMillis = millis();
   unsigned long secondes = lesMillis / 1000;
   int jours = secondes / 86400;
   secondes %= 86400;
   byte heures = secondes / 3600;
   secondes %= 3600;
   byte minutes = secondes / 60;
   secondes %= 60;
   snprintf(temps, sizeof(temps), "%02d:%02d:%02d:%02d", jours, heures, minutes, secondes);
   ecranLCD.print(temps); 

    // Au besoin, permuter le rétro-éclairage
    if (digitalRead(BOUTON)) {
        delay(BOUTON_DELAI_REBOND);
        etatRetroEclairage = !etatRetroEclairage;
        ecranLCD.setBacklight(etatRetroEclairage);
        while(digitalRead(BOUTON));
        delay(BOUTON_DELAI_REBOND);
    } if BOUTON

} // loop()