const byte brochesLeds[] = {13, 12, 11, 10, 8};
const byte brochesBoutons[] = { 7, 6, 5, 4, 3, 2};
const byte nombreDeLeds = sizeof brochesLeds / sizeof * brochesLeds;
const byte nombreDeBoutons = sizeof brochesBoutons / sizeof * brochesBoutons;
const byte indicesCodeCorrect[nombreDeLeds] = {0, 1, 0, 1, 0}; // l'ordre relatif aux index dans le tableau
const byte longeurDuCode = sizeof indicesCodeCorrect / sizeof * indicesCodeCorrect; // on doit en avoir autant que de LEDs
byte nombreDeTouchesCorrectes = 0;
// ------------------------------------------------
// LES FONCTIONS UTILITAIRES
// ------------------------------------------------
void allumerLed(const byte index) {
digitalWrite(brochesLeds[index], LOW);
}
void allumerTout() {
for (auto &broche : brochesLeds) digitalWrite(broche, LOW);
}
void eteindreTout() {
for (auto &broche : brochesLeds) digitalWrite(broche, HIGH);
}
void raz() { // remise à zéro
eteindreTout();
nombreDeTouchesCorrectes = 0;
}
// ------------------------------------------------
// LA FONCTION APPELÉE QUAND LE BON CODE EST ENTRÉ
// ------------------------------------------------
void actionCodeCorrect() { // le code a été bien rentré
Serial.println("Bravo, vous avez trouvé le code");
for (byte i = 0; i < 5; i++) { // on fait clignotter toutes les LEDs 5 fois
allumerTout();
delay(400);
eteindreTout();
delay(200);
}
// autres actions ici
}
// ------------------------------------------------
// LA GESTION DU CODE SECRET
// ------------------------------------------------
void gestionCode() {
byte nombreDeBoutonsAppuyes = 0;
byte indexBrocheChoisie = 0; // valide uniquement si nombreDeBoutonsAppuyes est >= 1
for (byte i = 0; i < nombreDeBoutons; i++) { // pour chaque broche du tableau des boutons
if (digitalRead(brochesBoutons[i]) == LOW) { // en cas d'appui
indexBrocheChoisie = i; // on mémorise que ce bouton est choisi
nombreDeBoutonsAppuyes++; // on compte le nombre de boutons appuyés
}
}
switch (nombreDeBoutonsAppuyes) {
case 0: break; // aucun appui, on ne fait rien
case 1: // Un bouton a été choisi
delay(20); // anti rebond
Serial.print("Appui Bouton #"); // On affiche l'index
Serial.println(indexBrocheChoisie); // du bouton choisi
if (indicesCodeCorrect[nombreDeTouchesCorrectes] == indexBrocheChoisie) {
Serial.println("Correct");
allumerLed(nombreDeTouchesCorrectes);
nombreDeTouchesCorrectes++;
Serial.print(nombreDeTouchesCorrectes);Serial.print(" / "); Serial.println(longeurDuCode);
if (nombreDeTouchesCorrectes >= longeurDuCode) { // on a trouvé tous les bons boutons
actionCodeCorrect(); // on déclenche une action
raz(); // on se prépare à recommencer
}
} else {
Serial.println("Code faux.Recommencez");
raz(); // on se prépare à recommencer
}
while (digitalRead(brochesBoutons[indexBrocheChoisie]) == LOW); // on attend la relâche du bouton
delay(20); // anti rebond
break;
default: // plus d'un bouton appuyé
delay(20); // anti rebond
Serial.println("Tricheur. On recommence"); // il est interdit d'appuyer sur plus d'un bouton à la fois
raz(); // on se prépare à recommencer
break;
}
}
// ------------------------------------------------
// LE CODE PRINCIPAL
// ------------------------------------------------
void setup() {
Serial.begin(115200); Serial.println();
for (auto &broche : brochesLeds) pinMode(broche, OUTPUT); // on met chaque broche led en sortie
for (auto &broche : brochesBoutons) pinMode(broche, INPUT_PULLUP); // on met chaque broche en entrée avec pullup
raz();
Serial.println("Entrer le code de l'agence");
}
void loop() {
gestionCode();
}