////////////////////////////////////////////////////////// code machine à états ///////////////////////
////////////////////////////////////////////////////////// Btn sur la D12 /////////////////////////////
// tuto de @J-M-L : https://forum.arduino.cc/t/programmation-automate-fini-machine-a-etat/452532 //////
// pour les tableaux : https://plaisirarduino.fr/tableau-de-donnees/
// switch case sur locoduino : https://www.locoduino.org/spip.php?article23
// #include <Arduino.h>
#define BOUTON_APPUYE LOW
#define BOUTON_RELACHE HIGH
enum { REPOS, ETAT_V, ETAT_VJ, ETAT_VJO, ETAT_VJOR } etatCourant ;
const byte pinLED[4] = {11, 10, 9, 8} ;
const byte pinBtn = 12 ;
void toutEteint() {
for(int i = 0; i < 4; i++) {
digitalWrite(pinLED[i], LOW) ;
}
etatCourant = REPOS;
}
void setup() { // setup
Serial.begin(115200);
pinMode(pinBtn, INPUT_PULLUP);
for ( int a = 0; a < 4; a++ ) {
pinMode(pinLED[a], OUTPUT) ;
}
etatCourant = REPOS ;
}
void loop() { // loop
if (digitalRead(pinBtn) == BOUTON_APPUYE) {
///////////////////////////////////////////////// switch case avec les états possible //////
switch (etatCourant ) {
case REPOS : // on était au repos et on a un appui, on allume la verte
digitalWrite(pinLED[0], HIGH);
etatCourant = ETAT_V;
break;
case ETAT_V : // on était led verte allumée et on a un appui, on allume la jaune
digitalWrite(pinLED[1], HIGH); // LED jaune alimentée
etatCourant = ETAT_VJ;
break;
case ETAT_VJ: // vert et jaune allumées, on a un appui, on allume la orange
digitalWrite(pinLED[2], HIGH); // LED orange alimenté
etatCourant = ETAT_VJO;// on note le nouvel état de notre syst
break;
case ETAT_VJO :// vert, orange et jaune allumées, on a un appui, on allume la rouge
digitalWrite(pinLED[3], HIGH); // LED rouge alimentée
etatCourant = ETAT_VJOR;// on note le nouvel état de notre système
break;
case ETAT_VJOR : // tout était allumé, on a un appui, on retourne au repos
toutEteint() ; // on retourne à l'état initial
etatCourant = REPOS ;
break ;
// il n'y a pas de default car tous les états sont prévu !
}
delay(200);
while (digitalRead(pinBtn) == BOUTON_APPUYE){;} //si le BP à été appuyé
delay(200);
}
}