// -----------------------------------------------------
// 1. DÉCLARATION DES BIBLIOTHÈQUES ET CONSTANTES
// -----------------------------------------------------
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Broches des composants physiques
const int PIN_POTAR = A0;
const int PIN_BOUTON = 2;
const int PIN_BUZZER = 7; // NOUVEAU : Broche D7 pour le buzzer
// Broches des LEDs
const int PIN_ORANGE_1 = 10;
const int PIN_ORANGE_2 = 11;
const int PIN_VERTE = 12;
const int PIN_ROUGE = 13;
// Variables de la Machine à États
int etatJeu = 0;
unsigned long tempsPrecedent = 0;
unsigned long tempsDepart = 0;
unsigned long tempsReaction = 0;
int ledActive = 0;
unsigned long delaiPotar = 0;
// -----------------------------------------------------
// 2. FONCTIONS AUXILIAIRES (Inchagées)
// -----------------------------------------------------
void allumerLED(int pin) {
digitalWrite(PIN_ORANGE_1, LOW);
digitalWrite(PIN_ORANGE_2, LOW);
digitalWrite(PIN_VERTE, LOW);
digitalWrite(PIN_ROUGE, LOW);
if (pin != 0) {
digitalWrite(pin, HIGH);
}
}
void reinitialiserJeu() {
etatJeu = 0;
ledActive = 0;
tempsReaction = 0;
allumerLED(PIN_ROUGE);
noTone(PIN_BUZZER); // S'assure que le buzzer est silencieux
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PRET!");
lcd.setCursor(0, 1);
lcd.print("Start: Bouton D2");
}
// -----------------------------------------------------
// 3. SETUP
// -----------------------------------------------------
void setup() {
pinMode(PIN_BOUTON, INPUT_PULLUP);
pinMode(PIN_ORANGE_1, OUTPUT);
pinMode(PIN_ORANGE_2, OUTPUT);
pinMode(PIN_VERTE, OUTPUT);
pinMode(PIN_ROUGE, OUTPUT);
pinMode(PIN_BUZZER, OUTPUT); // NOUVEAU : Configuration de la broche du buzzer
lcd.init();
lcd.backlight();
reinitialiserJeu();
}
// -----------------------------------------------------
// 4. BOUCLE PRINCIPALE (Logique du Son Ajoutée)
// -----------------------------------------------------
void loop() {
unsigned long tempsActuel = millis();
int etatBouton = digitalRead(PIN_BOUTON);
delaiPotar = map(analogRead(PIN_POTAR), 0, 1023, 200, 1500);
// ÉTAT 0 : Attente de Démarrage
if (etatJeu == 0) {
if (etatBouton == LOW) {
delay(50);
while (digitalRead(PIN_BOUTON) == LOW) {}
etatJeu = 1;
tempsPrecedent = tempsActuel;
ledActive = 0;
lcd.clear();
lcd.print("ATTENTION...");
lcd.setCursor(0, 1);
lcd.print("Vitesse: ");
lcd.print(delaiPotar);
}
}
// ÉTAT 1 : Séquence de Défilement (Faux Départ)
else if (etatJeu == 1) {
// Détection de FAUX DÉPART
if (etatBouton == LOW) {
etatJeu = 3;
allumerLED(PIN_ROUGE);
tone(PIN_BUZZER, 200, 500); // SON : Bruit grave et long (200Hz pendant 500ms)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!!! FAUX DEPART !!!");
lcd.setCursor(0, 1);
lcd.print("Appuyez pour Reset");
delay(200);
}
// Changement de LED (Non Bloquant)
if (tempsActuel - tempsPrecedent >= delaiPotar) {
tempsPrecedent = tempsActuel;
ledActive++;
if (ledActive == 1) { allumerLED(PIN_ORANGE_1); }
else if (ledActive == 2) { allumerLED(PIN_ORANGE_2); }
else if (ledActive == 3) {
// --- C'EST LE DÉPART (GO) ---
allumerLED(PIN_VERTE);
tempsDepart = tempsActuel;
etatJeu = 2;
tone(PIN_BUZZER, 1000, 100); // SON : Bruit aigu court (1000Hz pendant 100ms)
lcd.clear();
lcd.print("GO! GO! GO!");
}
else if (ledActive > 3) { etatJeu = 2; }
}
}
// ÉTAT 2 : Chronométrage (Arrêt)
else if (etatJeu == 2) {
// Lecture du Bouton pour Arrêt
if (etatBouton == LOW) {
delay(50);
while (digitalRead(PIN_BOUTON) == LOW) {}
tempsReaction = tempsActuel - tempsDepart;
etatJeu = 4;
allumerLED(0);
// SON : Double Bip pour valider l'arrêt
tone(PIN_BUZZER, 600, 50);
delay(100);
noTone(PIN_BUZZER);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMPS : ");
float tempsSec = (float)tempsReaction / 1000.0;
lcd.print(tempsSec, 3);
lcd.print(" s");
lcd.setCursor(0, 1);
lcd.print("Appuyez pour Rejouer");
}
}
// ÉTAT 3 : Faux Départ (Reset)
else if (etatJeu == 3) {
if (etatBouton == LOW) {
delay(50);
while (digitalRead(PIN_BOUTON) == LOW) {}
reinitialiserJeu();
}
}
// ÉTAT 4 : Résultat Affiché (Reset)
else if (etatJeu == 4) {
if (etatBouton == LOW) {
delay(50);
while (digitalRead(PIN_BOUTON) == LOW) {}
reinitialiserJeu();
}
}
}Pot de 10K
VITESSE DÉPART