/*
Chronometre
1 BP Start /Stop broche D3 et GND
1 BP RESET broche D2 et GND
1 OLED 0.96 SSD1306 128x64 pixels
Connexion des broches LCD:
Oled.SDA -> Uno.SDA - A4
Oled.SCL -> Uno.SCL - A5
Oled.GND -> Uno.GND
Oled.VCC -> Uno.5V
*/
// Librairies
#include <Wire.h> // Gestion I2C pour Oled SDA/SCL Adresse 0x3c
#include <Adafruit_GFX.h> // Affichage graphique
#include <Adafruit_SSD1306.h> // Driver Oled 0.96" - SSD1306
// Configuration OLED
#define SCREEN_WIDTH 128 // Largeur 128 pixels
#define SCREEN_HEIGHT 64 // Hauteur 64 pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // SDA=A4 SCL=A5 - Initialisation Oled 0.96
//La ligne ci-dessus peut etre remplacé par: Adafruit_SSD1306 monOled(128, 64, &Wire, -1);
int Bp_Start_Stop = 3; // Bp_Start_Stop sur broche D3
int Bp_Reset = 2; // Bp_Reset sur broche D2
bool Etat_Bp_Start_Stop;
bool Etat_Bp_Reset;
bool Etat_bouton_appuyee; // HIGH si un ou deux boutons appuyés
bool Etat_Chrono_On_Off; // HIGH (ou True) en marche
int Milliseconde = 0;
int Seconde = 0;
int Minute = 0;
int Heure = 0;
char str[20]; // chaine de 20 caractères pour affichage Temps
void setup() {
Serial.begin(9600);
pinMode(Bp_Start_Stop, INPUT_PULLUP);
pinMode(Bp_Reset, INPUT_PULLUP);
// Initialisation de l'écran OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("Échec de l'initialisation de l'écran OLED !");
for (;;); // Boucle infinie
display.display(); // Affichage sur Oled 0.96
display.clearDisplay(); // Effacement de l'afficheur
}
// Affichage Oled 0.96
display.clearDisplay(); // Effacement de l'afficheur
display.setTextColor(WHITE); // Couleur du texte en BLANC
display.setCursor(0, 0); // Position du curseur - colonne 0 et ligne 0
display.setTextSize(1); // Caractere taille 1
display.print("---- Chronometre ----"); // Affichage "---- Chronometre----"
display.setCursor(35, 40); // Position du curseur - colonne 35 et ligne 40
display.print("Press Start"); // Affichage "Press Start"
display.setCursor(35, 50); // Position du curseur - colonne 35 et ligne 50
display.print("Bouton Rouge"); // Affichage "Bouton Rouge"
display.display(); // Affichage sur Oled 0.96
//delay(500); // PAS NECESSAIRE
// ===========Init Interruption ============================
TCCR1A = 0; // set entire TCCR1A register to 0 //set timer1 interrupt at 1kHz // 1 ms
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // set timer count for 1khz increments
OCR1A = 1999; // = (16*10^6) / (1000*8) - 1
//had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
// turn on CTC mode
TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11); // enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
// ==========================================================
}
void loop()
{
Etat_Bp_Start_Stop = digitalRead(Bp_Start_Stop); // lire Bp_Start_Stop
Etat_Bp_Reset = digitalRead(Bp_Reset); // lire Bp_Reset
if (Etat_Bp_Start_Stop == LOW && Etat_bouton_appuyee == LOW) {
Etat_bouton_appuyee = HIGH;
Etat_Chrono_On_Off = ! Etat_Chrono_On_Off; // marche/arret Chronometre
}
if (Etat_Bp_Reset == LOW && Etat_bouton_appuyee == LOW) {
Etat_bouton_appuyee = HIGH;
Heure = Minute = Seconde = Milliseconde = 0; // Raz générale Temps
}
if (Etat_Bp_Start_Stop == HIGH && Etat_Bp_Reset == HIGH) {
Etat_bouton_appuyee = LOW; // boutons non appuyés
}
// ===============AFFICHAGE TEMPS ====================================================
// Utilisation sprintf() pour composer le string str
// sprintf stocke la chaine de caracteres de str[20] definie à 20 caracteres
// %2u = affichage deux decimales, %3u = affichage trois decimales
// h, mn, s = affichage des caracteres h, mn et s
// Heure, Minute, Seconde, Milliseconde = affichage des valeurs contenues dans la variables
// les espaces sont comptabilisés dans la chaine str[20]
// soit 19 caracteres affichés
sprintf(str, "%2u h %2u mn %2u s %3u", Heure, Minute, Seconde, Milliseconde);
// Mise en forme de l'affichage Oled
display.clearDisplay(); // Effacement de l'afficheur
display.setTextColor(WHITE); // Couleur du texte en BLANC
display.setTextSize(1); // Taille caractere
display.setCursor(0, 0); // Position du curseur - colonne 0 et ligne 0
display.print("---- Chronometre ----"); // Affichage "---- Chronometre ----"
display.setCursor(5, 20); // Position du curseur - colonne 5 et ligne 20
display.print(str); // Affichage du string str: "0 h 0mn 0 s 0"
display.setCursor(35, 40); // Position du curseur - colonne 35 et ligne 40
display.print("Press Start"); // Affichage "Press Start"
display.setCursor(35, 50); // Position du curseur - colonne 35 et ligne 50
display.print("Bouton Rouge"); // Affichage "Bouton Rouge"
display.display(); // AFFICHAGE
// =====================================================================================
}
// ===== Routine d'interruption =====
ISR(TIMER1_COMPA_vect) {
if (Etat_Chrono_On_Off == true) {
Milliseconde = Milliseconde + 1;
}
if (Milliseconde > 999) {
Milliseconde = 0; Seconde = Seconde + 1;
if (Seconde > 59) {
Seconde = 0;
Minute = Minute + 1;
}
if (Minute > 59) {
Minute = 0;
Heure = Heure + 1;
}
}
}
// ===================================Start /
Stop
RESET
OLED