Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Chronomètre
- Affichage du temps courant
- Affichage du meilleur temps
- Remise à zéro des compteurs
- Nécessite :
- Arduino nano ou équivalent
- écran LCD1602A I2C
- régulateur de tension LM2596 pour fournir une tension 5 volts (ou légérement supérieure) à partir du 12 volts batterie
- 2 boutons
- 2 résistances 1kOhm
- Auteur : Patrick Dubois
- squiplanche@free.fr
- sur une idée d'Equatorus Rider (Facebook)
- Date : 2022-03-10
- Copyright : domaine public
- */
- #include <LiquidCrystal_I2C.h> // use this library for LCD displays with I2C backpack
- #include <Wire.h> // needed for the LCD I2C library. SCL connects to pin A5. SDA connects to pin A4.
- // Jumper the LED backlight pin on the I2C board
- #define STARTSTOPBUTTON 2 // D2 interruptions only working on D2 and D3
- #define REINITZEROBUTTON 3 // D3
- int buttonStartStopState; // variable to store the status
- boolean bStartButton; // timer is running or not
- unsigned long lStartTime, lCurrentTime, lBestTime; // to manage timer counters and display
- LiquidCrystal_I2C lcd(0x27, 16, 2); // use this for I2C LCD. Assumes default address of 0x27. 16 columns 2 rows
- String MM_SS_ccc(unsigned long elapsed) { // converts millisecondes to MM:SS:ccc
- char cReturn[10];
- int MM = elapsed / 60000;
- int SS = (elapsed - (MM * 60000)) / 1000;
- int ccc = (elapsed - (MM * 60000) - (SS * 1000));
- sprintf(cReturn,"%02d:%02d.%03d", MM, SS, ccc);
- return cReturn;
- }
- void setup() {
- lcd.init(); // I2C LCD init command
- lcd.backlight(); // I2C LCD turn backlight on
- lcd.setCursor(0, 0); // Startup screen start
- lcd.print("Time Attack 1.00");
- lcd.setCursor(0, 1);
- lcd.print("by Patrick D.");
- delay(2500);
- lcd.clear(); // Startup screen end
- lcd.setCursor(0, 0); // cols, rows
- lcd.print("Time: ");
- lcd.setCursor(0, 1);
- lcd.print("Best:");
- // initialize the pushbutton pins as input:
- pinMode(STARTSTOPBUTTON, INPUT);
- attachInterrupt(digitalPinToInterrupt(STARTSTOPBUTTON), onButtonStartStopEvent, RISING);
- pinMode(REINITZEROBUTTON, INPUT);
- attachInterrupt(digitalPinToInterrupt(REINITZEROBUTTON), onButtonReinitZeroEvent, RISING);
- lBestTime = 0;
- bStartButton = false;
- buttonStartStopState = LOW;
- }
- void onButtonReinitZeroEvent(){
- // reinit timer counters to zero if not counting :-)
- if (! bStartButton){
- lBestTime = lStartTime = lCurrentTime = 0;
- }
- }
- void onButtonStartStopEvent() {
- buttonStartStopState = HIGH;
- }
- void loop() {
- if (buttonStartStopState){
- if (not bStartButton) {
- bStartButton = true;
- lStartTime = millis();
- } // end if not bStartButton
- else {
- bStartButton = false;
- if ((lBestTime == 0) || (lBestTime > lCurrentTime)) {
- lBestTime = lCurrentTime;
- }
- } // end else
- buttonStartStopState = LOW;
- } // end if buttonstartStopState
- if (bStartButton){
- lCurrentTime = millis() - lStartTime;
- }
- lcd.setCursor(7, 0); lcd.print(MM_SS_ccc(lCurrentTime));
- lcd.setCursor(7, 1); lcd.print(MM_SS_ccc(lBestTime));
- }
Add Comment
Please, Sign In to add comment