Arduino 2019-4: Mooc FUN TP2 Feux tricolore avec passage piéton

TP02 Feux tricolore avec passage piéton quand on appuie sur un bouton

 


/* 
TP S02 Feu tricolore + Feu Piétons

voiture:
* Une LED rouge sur la broche 12 
* Une LED orange sur la broche 11 
* Une LED verte sur la broche 10

piéton:
* Une LED rouge sur la broche 9 
* Une LED verte sur la broche 8

* Bouton poussoir branché sur la broche 2 depuis +5V
* Une résistance de 1KΩ branché sur la broche 2 depuis GND

*/

// Initialisation des constantes pour les LED
const int rougeVoiture = 12;     //broche 12 pour la Led rouge voiture
const int orangeVoiture = 11;
const int verteVoiture = 10;

const int rougePieton = 9;
const int vertePieton = 8;

// Numéro broche du bouton poussoir
const int bouton = 2;

// Déclaration des variables :
int etatBouton = 0;

// execution unique
void setup() {
 // broches des LED en sorties :
 pinMode(rougeVoiture, OUTPUT);
 pinMode(orangeVoiture, OUTPUT);
 pinMode(verteVoiture, OUTPUT);

 pinMode(rougePieton, OUTPUT);
 pinMode(vertePieton, OUTPUT);

// broche bouton en entrée :
 pinMode(bouton, INPUT);
}

// boucle infinie
void loop() {
 // normallement le feu piéton est toujours rouge
 digitalWrite(rougePieton, HIGH);

// feu voiture Vert allumé 3s 
 digitalWrite(verteVoiture, HIGH);
 delay(3000);      // 3000 ms = 3s,               
 digitalWrite(verteVoiture, LOW);

// l'état du bouton stocké dans etatBouton :
 etatBouton = digitalRead(bouton);

// le bouton est appuyé si etatBouton = HIGH
 if (etatBouton == HIGH) {
   digitalWrite(orangeVoiture, HIGH);
   delay(1000);
   digitalWrite(orangeVoiture, LOW);

   digitalWrite(rougeVoiture, HIGH);

   // feu piéton passe au vert pendant 5s
   digitalWrite(rougePieton, LOW);
   digitalWrite(vertePieton, HIGH);

   delay(5000);    // 5000 ms = 5s

   // feu piéton redevient rouge
   digitalWrite(rougePieton, HIGH);
   digitalWrite(vertePieton, LOW);

   // feu voiture Rouge est éteint
   // avant retour au déroulement normal
   digitalWrite(rougeVoiture, LOW);
 }
 else {
   // Fonctionnement normal du feu voiture:
   // orange 1s, rouge 3s
   digitalWrite(orangeVoiture, HIGH);
   delay(1000);     // 1000 ms = 1s
   digitalWrite(orangeVoiture, LOW);

   digitalWrite(rougeVoiture, HIGH);
   delay(3000);     // 3000 ms = 3s
   digitalWrite(rougeVoiture, LOW);
 }
}

https://www.tinkercad.com/things/fCoIqxvMqL4-crepp-s2-tp2-feu-tricolorepieton-bouton/editel?sharecode=9Y6MzQiAqWKbwzIagrchhPLcjSWkB-HKVbLqjqJ3shY=

MoocS02_TP02_FeuxBouton.ino