/* ====CHRONOMETRE ================
2 LCD I2C de 2 lignes de 16 caracteres
2 BP
*/
#include <Wire.h> //bibli cablage
#include <LiquidCrystal_I2C.h> //bibli I2C
LiquidCrystal_I2C lcdChrono(0x27, 16, 2); //designation affi 1
LiquidCrystal_I2C lcdInfo(0x26, 16, 2); //design aff2
int BtStartStop = 3; //declaration bouton Start Stop
int BtReset = 2; //declaration bouton Reset
bool EtatBtStartStop ;
bool EtatBtReset ;
int EtatBtAppuyer ;
int EtatChronoOnOff ;
int TempsEcoule ;
int Milliseconde ;
int Seconde ;
int Minute ;
int Heure ;
unsigned long currentTime = 0 ;
unsigned long previousTime = 0 ;
char str [16];
void setup()
{ pinMode(BtStartStop, INPUT_PULLUP) ;
pinMode(BtReset, INPUT_PULLUP) ;
lcdChrono.init (); lcdInfo.init ();
lcdChrono.backlight () ; lcdInfo.backlight ();
lcdChrono.setCursor(1, 0) ; lcdInfo.setCursor(2, 0);
lcdChrono.print (" CHRONOMETRE");
lcdInfo.print ("Press Start");
lcdInfo.setCursor(2, 1);
lcdInfo.print("Bouton Vert");
}
void loop()
{
EtatBtStartStop = digitalRead(BtStartStop); // repos=1 appuye=0
EtatBtReset = digitalRead(BtReset);
if (EtatBtStartStop == LOW && EtatBtAppuyer == 0) {
EtatBtAppuyer = 1;
EtatChronoOnOff = !EtatChronoOnOff;
affichage(); // appel fonction
}
if (EtatBtReset == LOW && EtatChronoOnOff == 0 && EtatBtAppuyer == 0)
{ EtatBtAppuyer = 1;
Milliseconde = 0; Seconde = 0; Minute = 0; Heure = 0;
}
if (EtatBtStartStop == HIGH && EtatBtReset == HIGH)
{ EtatBtAppuyer = 0;
}
// calcul du temps
currentTime = millis();
TempsEcoule = currentTime - previousTime;
previousTime = millis();
// si EtatChronoOnOff == 1
if (EtatChronoOnOff == 1) {
Milliseconde = Milliseconde + TempsEcoule;
if (Milliseconde > 999) {
Milliseconde = 0;
Seconde ++;
}
if (Seconde > 59) {
Seconde = 0 ;
Minute ++;
}
if (Minute > 59) {
Minute = 0 ;
Heure ++;
}
}
// affichechrono
afficheChrono(); // appel fonction affiche chrono
}
void affichage ()
{
lcdInfo.clear();
lcdInfo.setCursor(0, 0);
lcdInfo.print("Stop/Start Vert");
lcdInfo.setCursor(2, 1);
lcdInfo.print("Reset Rouge");
}
void afficheChrono()
{
// ===============AFFICHAGE TEMPS =============================
sprintf(str, "%2uh %2umn %2us %3u", Heure, Minute, Seconde, Milliseconde); // use sprintf() to compose the string str
lcdChrono.setCursor(0, 1); // Placer le curseur à la position caractère, ligne
lcdChrono.print(str); //affiche Temps (str) sur LCD
// =============================================================
}
Start /
Stop
RESET
LCD INFO
LCD Chrono