enum Etat {ETEINT, ALLUME};
struct BoutonLed {
const byte brocheLed;
const byte brocheBouton;
const unsigned long dureeEclairage;
Etat etat;
unsigned long debut;
void begin() {
pinMode(brocheLed, OUTPUT); // LED en sortie
pinMode(brocheBouton, INPUT_PULLUP); // bouton en entrée
}
void checkState() {
if (digitalRead(brocheBouton) == LOW) { // si le bouton est appuyé
digitalWrite(brocheLed, HIGH); // on allume la LED
debut = millis(); // et on note le moment
etat = ALLUME; // et l'état
}
if (etat == ALLUME) { // si la LED est allumée
if (millis() - debut >= dureeEclairage) { // depuis trop longtemps
digitalWrite(brocheLed, LOW); // on éteint la LED
etat = ETEINT; // et on note l'état
}
}
}
};
BoutonLed elements[] = {
{ 1, 2, 3000, ETEINT, 0},
{ 6, 7, 5000, ETEINT, 0},
{11, 12, 500, ETEINT, 0},
};
void setup() {
for (auto& unElement : elements) unElement.begin();
}
void loop() {
for (auto& unElement : elements) unElement.checkState();
}