#include <Toggle.h>
const byte pinContactA = 3;
const byte pinContactB = 2;
const unsigned long attenteMax = 1000; // ms (100ms normalement, ici mis à 1s pour tester appui)
Toggle contactA, contactB;
unsigned long startTime;
enum {REPOS, ATTENTE_A, ATTENTE_B, ATTENTE_OUVERTURE} etat = ATTENTE_OUVERTURE;
void setup() {
contactA.begin(pinContactA);
contactB.begin(pinContactB);
Serial.begin(115200);
}
void loop() {
contactA.poll();
contactB.poll();
switch (etat) {
case REPOS:
if (contactA.onPress()) {
startTime = millis();
etat = ATTENTE_B;
} else if (contactB.onPress()) {
startTime = millis();
etat = ATTENTE_A;
}
break;
case ATTENTE_A:
if (millis() - startTime >= attenteMax) {
Serial.println("Erreur, durée écoulée et A non fermé.");
etat = ATTENTE_OUVERTURE;
}
else if (contactA.onPress()) {
unsigned long deltaT = millis() - startTime;
Serial.print("succès, A fermé ");
Serial.print(deltaT);
Serial.println("ms après B. ");
etat = ATTENTE_OUVERTURE;
}
break;
case ATTENTE_B:
if (millis() - startTime >= attenteMax) {
Serial.println("Erreur, durée écoulée et B non fermé.");
etat = ATTENTE_OUVERTURE;
}
else if (contactB.onPress()) {
unsigned long deltaT = millis() - startTime;
Serial.print("succès, B fermé ");
Serial.print(deltaT);
Serial.println("ms après A. ");
etat = ATTENTE_OUVERTURE;
}
break;
case ATTENTE_OUVERTURE:
if (contactA.isReleased() && contactB.isReleased()) {
Serial.println("PRET");
etat = REPOS;
}
break;
}
}
Contact A
Contact B