////////////////////////////////////////////////////////// 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>
enum :byte { REPOS, ETAT_V, ETAT_VJ, ETAT_VJO, ETAT_VJOR } etatCourant ;
byte pinLED[4] = {11, 10, 9, 8} ;
const byte pinBtn = 12 ;
byte oneButton = 0 ;
unsigned long changeState;
unsigned long antiBounce;
unsigned long displayTime = 3000;
bool forceChangeState = false;
#define BOUTON_APPUYE LOW
#define BOUTON_RELACHE HIGH
void toutEteint() {
for(int i = 0; i < 4; i++) {
digitalWrite(pinLED[i], LOW) ;
}
etatCourant = REPOS;
}
void setup() { // setup
pinMode(pinBtn, INPUT_PULLUP);
Serial.begin(115200);
oneButton = REPOS ;
for ( int a = 0; a < 4; a++ ) {
pinMode(pinLED[a], OUTPUT) ;
}
changeState = millis();
antiBounce = millis();
Serial.println("Start");
}
void loop() { // loop
///////////////////////////////////////////////// test machine à état //////////////////////
if (digitalRead(pinBtn) == BOUTON_APPUYE && millis() - antiBounce > 30) {
antiBounce = millis();
while(digitalRead(pinBtn) == BOUTON_APPUYE || millis() - antiBounce < 30) {}
forceChangeState = true;
antiBounce = millis();
Serial.print("State: ");Serial.println(oneButton);
}
///////////////////////////////////////////////// switch case avec les états possible //////
switch (oneButton) {
case REPOS : // on était au repos et on a un appui, on allume la verte
digitalWrite(pinLED[0], HIGH); // LED verte alimentée
if (millis() - changeState > displayTime || forceChangeState) {
oneButton = ETAT_V; // on note le nouvel état de notre système
changeState = millis();
Serial.print("State: ");Serial.println(oneButton);
}
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
if (millis() - changeState > displayTime || forceChangeState) {
oneButton = ETAT_VJ;// on note le nouvel état de notre système
changeState = millis();
Serial.print("State: ");Serial.println(oneButton);
}
break;
case ETAT_VJ: // vert et jaune allumées, on a un appui, on allume la orange
digitalWrite(pinLED[2], HIGH); // LED orange alimentée
if (millis() - changeState > displayTime || forceChangeState) {
oneButton = ETAT_VJO;// on note le nouvel état de notre système
changeState = millis();
Serial.print("State: ");Serial.println(oneButton);
}
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
if (millis() - changeState > displayTime || forceChangeState) {
oneButton = ETAT_VJOR;// on note le nouvel état de notre système
changeState = millis();
Serial.print("State: ");Serial.println(oneButton);
}
break;
case ETAT_VJOR : // tout était allumé, on a un appui, on retourne au repos
toutEteint() ; // on retourne à l'état initial
if (millis() - changeState > displayTime || forceChangeState) {
oneButton = REPOS ;
changeState = millis();
Serial.print("State: ");Serial.println(oneButton);
}
break ;
// il n'y a pas de default car tous les états sont prévu !
}
forceChangeState = false;
}