/* Claude EMERY 08/02/2026
Chronometre LCD-I2C
Bouton Start/Pause/Reset
Heure 00 à 99
Minute 00 à 60
Seconde 00 à 60
Centieme 00 à 999
|-------------------------------------------------------------------------------|
| Etat | Mise sous Tension | Press Start | Press Pause | Press Reset |
|-------------------------------------------------------------------------------|
| EtatBtAppuyer | 0 | 0 -> 1 -> 0 | 0 -> 1 -> 0 | 0 -> 1 -> 0 |
| EtatBtStartStop | 1 | 1 -> 0 -> 1 | 1 -> 0 -> 1 | 1 |
| EtatChronoOnOff | 0 | 1 | 0 | 0 |
| EtatBtReset | 1 | 1 | 1 | 1 -> 0 -> 1 |
|-------------------------------------------------------------------------------|
*/
//#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcdChrono(0x27,16,2); // LCD 0x27 16 caracteres 2 lignes
LiquidCrystal_I2C lcdInfo(0x26,16,2); // LCD 0x26 16 caracteres 2 lignes
int BtStartStop = 3; // BtStartStop - Aucun Appui Bt => 5V , Appui Bt => 0V
int BtReset = 2; // BtReset - Aucun Appui Bt => 5V , Appui Bt => 0V
bool EtatBtStartStop; // Etat btStartStop => Repos = 1 , Appuyer = 0
bool EtatBtReset; // Etat BtReset => Repos = 1 , Appuyer = 0
int EtatBtAppuyer; // EtatBtAppuyer => Repos = 0 , Appuyer = 1
int EtatChronoOnOff; // EtatChronoOnOff=> Repos = 0, si Start=1 => Etat=1, si Stop=1 => Etat=0 , Appuyer = 0
int TempsEcoule;
int Milliseconde;
int Seconde;
int Minute;
int Heure;
unsigned long currentTime = 0; // Temps actuel
unsigned long previousTime = 0; // Temps précédent
void setup() {
pinMode(BtStartStop, INPUT_PULLUP);
pinMode(BtReset, INPUT_PULLUP);
lcdChrono.init();
lcdChrono.backlight();
lcdChrono.setCursor(1,0);
lcdChrono.print ("Chronometre :");
lcdInfo.init();
lcdInfo.backlight();
lcdInfo.setCursor(2, 0);
lcdInfo.print ("Press Start");
lcdInfo.setCursor(4,1);
lcdInfo.print("Bt Vert ");
Serial.begin(9600);
}
void loop() {
EtatBtStartStop = digitalRead(BtStartStop); // lecture etat BtStartStop -> Repos=1 Appuyer=0
EtatBtReset = digitalRead(BtReset); // lecture etat BtReset -> Repos=1 Appuyer=0
// controle des états
//Serial.print("EtatBtStartStop "); // Repos=1 Appuyer=0
//Serial.println(EtatBtStartStop);
//Serial.print("EtatBtReset "); // Repos=1 Appuyer=0
//Serial.println(EtatBtReset);
//Serial.print("EtatBtAppuyer "); // Repos=0 Appuyer=1
//Serial.println(EtatBtAppuyer);
//Serial.print("EtatChronoOnOFF "); // Repos=0, si Start=1 => Etat=1, si Stop=1 => Etat=0, si Reset Appuyer=0
//Serial.println(EtatChronoOnOFF);
// Lancer ou arreter le chrono
// Si EtatBtStartStop == LOW et EtatBtAppuyer == 0 on exécute les actions entre {}
// EtatBtStartStop -> Repos=1 Appuyer=0
// EtatBoutonAppuyer -> Repos=0 Appuyer=1
if (EtatBtStartStop == LOW && EtatBtAppuyer == 0) {
EtatBtAppuyer = 1; // EtatBtAppuyer prend la valeur de 1
EtatChronoOnOff = !EtatChronoOnOff; // Si EtatChronoOnOff = 0 alors EtatChronoOnOff = 1 et inversement
affichage(); // Affichage lcdInfo "Start <-> Stop ou Reset"
}
// Reset le chrono
// Si EtatBtReset == LOW et EtatChronoOnOff == 0 et EtatBtAppuyer == 0
if (EtatBtReset == LOW && EtatChronoOnOff == 0 && EtatBtAppuyer == 0) {
EtatBtAppuyer = 1; // EtatBtAppuyer prend la valeur de 1
// Mise à 0 des valeurs
Milliseconde = 0;
Seconde = 0;
Minute = 0;
Heure = 0;
}
//Détecte les 2 boutons relâchés
// Si EtatBtStartStop == HIGH et EtatBtReset == HIGH on exécute les actions entre {}
if (EtatBtStartStop == HIGH && EtatBtReset == HIGH) {
EtatBtAppuyer = 0; // EtatBtAppuyer = 0
}
// Calcul du temps
currentTime = millis(); // Temps actuel en milliseconde depuis la mise sous tension
TempsEcoule = currentTime - previousTime; // Temps écoulé
previousTime = millis(); // Temps précedent
// Si EtatChronoOnOFF == 1 on exécute les actions entre {}
if (EtatChronoOnOff == 1) {
Milliseconde = Milliseconde + TempsEcoule; // Calcul Milliseconde + TempsEcoule.
if (Milliseconde > 999) { Milliseconde = Milliseconde - 1000; Seconde++; }
if (Seconde > 59) { Seconde = 0; Minute++; }
if (Minute > 59) { Minute = 0; Heure++; }
}
afficheChrono(); // fonction affichage du chronometre
}
/*
Representation de l'ecran du lcdChrono
|--------------------------------------------------------------------
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15|
|--------------------------------------------------------------------
| 0 | | C | H | R | O | N | O | M | E | T | R | E | : | | | |
|--------------------------------------------------------------------
| 1 | | 0 | 0 | : | 0 | 0 | : | 0 | 0 | : | 0 | 0 | 0 | | | |
|--------------------------------------------------------------------
|-------| Heure | Minute | Seconde | Milliseconde |------------|
*/
void afficheChrono(){ // fonction affichage du chronometre
// AFFICHAGE MILLISECONDE
lcdChrono.setCursor(9, 1);
lcdChrono.print(":");
if (Milliseconde > 99) {
lcdChrono.setCursor(10, 1);
lcdChrono.print(Milliseconde);
}
else if (Milliseconde > 9 && Milliseconde < 100) {
lcdChrono.setCursor(10, 1);
lcdChrono.print("0");
lcdChrono.setCursor(11, 1);
lcdChrono.print(Milliseconde);
}
else if (Milliseconde > 0 && Milliseconde < 10) {
lcdChrono.setCursor(10, 1);
lcdChrono.print("00");
lcdChrono.setCursor(12, 1);
lcdChrono.print(Milliseconde);
}
else {
lcdChrono.setCursor(9, 1);
lcdChrono.print(":000");
}
// AFFICHAGE SECONDE
lcdChrono.setCursor(6, 1);
lcdChrono.print(":");
if (Seconde < 10) {
lcdChrono.setCursor(7, 1);
lcdChrono.print("0");
lcdChrono.setCursor(8, 1);
lcdChrono.print(Seconde);
}
else {
lcdChrono.setCursor(7, 1);
lcdChrono.print(Seconde);
}
// AFFICHAGE MINUTE
lcdChrono.setCursor(3, 1);
lcdChrono.print(":");
if (Minute < 10) {
lcdChrono.setCursor(4, 1);
lcdChrono.print("0");
lcdChrono.setCursor(5, 1);
lcdChrono.print(Minute);
}
else {
lcdChrono.setCursor(4, 1);
lcdChrono.print(Minute);
}
// AFFICHAGE HEURE
if (Heure < 10) {
lcdChrono.setCursor(1, 1);
lcdChrono.print("0");
lcdChrono.setCursor(2, 1);
lcdChrono.print(Heure);
}
else {
lcdChrono.setCursor(1, 1);
lcdChrono.print(Heure);
}
}
void affichage(){ // fonction affichage du texte sur lcdInfo
// Affichage "Start <-> Stop ou Reset"
lcdInfo.clear();
lcdInfo.setCursor(0,0);
lcdInfo.print("Stop/Start Vert");
lcdInfo.setCursor(2,1);
lcdInfo.print("Reset Rouge ");
}
Start / Stop
Reset
Chronometre
Information Boutons
Tension sur D1 et D2, Aucun Appui sur Bt => 5V, Appui sur Bt => 0V