/*
WHILE
Jef59
Il n'y a pas de condensateur dispo dans Wokwi pour mettre en place des anti rebonds
J'ai donc ajouter "delay(rebond);//anti rebond soft" là où c'est necessaire
Ces lignes peuvent être decommentées si necessaire
*/
// constantes
const int inPinBP = 2; // BP Vert Demande demarrage (appuyer 2 fois en moins de 2s)
const int inPinFdc = 3; // BP Rouge Fin de Course (appuyer pour fin de course)
const int outPinVe = 4; // Led Verte "Moteur en Marche"
const int outPinRo = 5; // Led Rouge "Moteur Arrêté"
const int outPinJa = 6; // Led Ja "Attente 2éme appui BP VERT"
const int outPinRel = 7; // Relais Marche Moteur
// variables
int temp0 = 0; // ..
int temp1 = 0; // ..
int tempo = 0; // ..
int etatBp = 0; // 1 = BP appuyé
int etatFdc = 0; // 1 = Fin de Course
int etatVe = 0; // 0 = Led Verte éteinte
int etatRo = 1; // 0 = Led Rouge éteinte
int etatJa = 0; // 0 = Led Jaune éteinte
int etatRel = 0; // 0 = Relais inactivé
int flag1 = 0; // ...
int rebond = 20;
//-----------------------------------------------------------
void setup() {
Serial.begin(9600);
// initialliser les entrées
pinMode(inPinBP, INPUT_PULLUP);
pinMode(inPinFdc, INPUT_PULLUP);
// initialiser les sorties
pinMode(outPinVe, OUTPUT);
pinMode(outPinJa, OUTPUT);
pinMode(outPinRo, OUTPUT);
pinMode(outPinRel, OUTPUT);
etatLeds();
}
//-----------------------------------------------------------
void etatLeds(){
digitalWrite(outPinRel, etatRel);
digitalWrite(outPinVe, etatVe);
digitalWrite(outPinRo, etatRo);
digitalWrite(outPinJa, etatJa);
//affichage();
}
//-----------------------------------------------------------
void affichage(){
Serial.print("etatBp = ");
Serial.println(etatBp);
Serial.print("etatFdc = ");
Serial.println(etatFdc);
Serial.print("etatVe = ");
Serial.println(etatVe);
Serial.print("etatRo = ");
Serial.println(etatRo);
Serial.print("etatJa = ");
Serial.println(etatJa);
Serial.print("etatRel = ");
Serial.println(etatRel);
Serial.println("-------------------------------------");
}
//-----------------------------------------------------------
void marcheMoteur(){
//Je mets en route le moteur
etatVe=1;
etatRo=0;
etatJa=0;
etatRel=1;
etatLeds();
//WHILE5: J'attend l'arrivée en Fdc
while(etatFdc==0){
etatFdc=!digitalRead(inPinFdc);
}
//J'éteind le moteur
etatVe=0;
etatRo=1;
etatJa=0;
etatRel=0;
etatLeds();
delay(rebond);//anti rebond soft
}
//-----------------------------------------------------------
void loop() {
//WHILE1: J'attend appui sur BP
while(etatBp==0){
etatBp=!digitalRead(inPinBP);
}
etatBp=1;
delay(rebond);//anti rebond soft
//WHILE2: J'attend qu'on relache BP
while(etatBp==1){
etatBp=!digitalRead(inPinBP);
}
//Je démarre la tempo et je passe en ORANGE
temp0=millis();
etatVe=0;
etatRo=0;
etatJa=1;
etatRel = 0;
etatLeds();
delay(rebond);//anti rebond soft
//WHILE3: J'attends deuxiéme appuie sur BP et je surveille tempo < 2s
while(flag1==0){
temp1=millis();
etatBp=!digitalRead(inPinBP);
tempo=temp1-temp0;
if (tempo>1999){
flag1=1;
}
if (tempo<2001 && etatBp==1){
flag1=1;
}
}
delay(rebond);//anti rebond soft
if (etatBp==1){
//WHILE4: Jattend qu'on relache le BP
while(etatBp==1){
etatBp=!digitalRead(inPinBP);
}
//Je mets en route le moteur
marcheMoteur();
}
delay(rebond);//anti rebond soft
//J'attend qu'on relache le Fdc
while(etatFdc==1){
etatFdc=!digitalRead(inPinFdc);
}
//Je réinitialise ma machine
flag1=0;
etatBp=0;
etatFdc=0;
etatVe=0;
etatRo=1;
etatJa=0;
etatRel=0;
etatLeds();
}