#include <IRremote.hpp>
enum : uint16_t {
onoff = 0x00A2, menu = 0x00E2,
test = 0x0022, plus = 0x0002, entree = 0x00C2,
retrap = 0x00E0, joue = 0x00A8, avrap = 0x0090,
zero = 0x0068, moins = 0x0098, efface = 0x00B0,
un = 0x0030, deux = 0x0018, trois = 0x007A,
quatre = 0x0010, cinq = 0x0038, six = 0x005A,
sept = 0x0042, huit = 0x004A, neuf = 0x0052,
};
const byte ledPins[] = {4, 6, 8, 10};
const byte maxEtapes = sizeof ledPins / sizeof * ledPins;
enum {REPOS, ACTION} etat = REPOS;
struct Sequence {
const byte nombreEtapes;
const unsigned long dureeEntreEtapes[maxEtapes];
const unsigned long attenteAvantExtinction;
};
byte sequenceEnCours;
byte etapeEnCours;
unsigned long debut;
Sequence lesSequences[] {
{2, {0, 4000, 0, 0}, 2000}, // attente 0s, Led 1, attente 4s, Led 2, attente finale 2s, extinction
{3, {0, 4000, 500, 0}, 5000}, // attente 0s, Led 1, attente 4s, Led 2, attente 0.5s, Led 3, attente finale 5s, extinction
{4, {0, 4000, 4000, 0}, 2000}, // attente 0s, Led 1, attente 4s, Led 2, attente 4s, Led 3, attente 0s, Led 4, attente finale 2s, extinction
};
const byte nbSequences = sizeof lesSequences / sizeof * lesSequences;
void toutEteindre() {
for (byte p : ledPins) digitalWrite(p, LOW);
}
void eteindre(const byte indiceLed) {
digitalWrite(ledPins[indiceLed], LOW);
}
void allumer(const byte indiceLed) {
digitalWrite(ledPins[indiceLed], HIGH);
}
void choisirSequence(const byte indiceSequence) {
if (indiceSequence >= nbSequences) return;
toutEteindre();
sequenceEnCours = indiceSequence;
etapeEnCours = 0;
debut = millis();
etat = ACTION;
}
void setup() {
for (byte p : ledPins) pinMode(p, OUTPUT);
IrReceiver.begin(2, false);
Serial.begin(115200);
}
void loop() {
if (IrReceiver.decode()) {
switch (IrReceiver.decodedIRData.command) {
case un: Serial.println(F("un")); choisirSequence(0); break;
case deux: Serial.println(F("deux")); choisirSequence(1); break;
case trois: Serial.println(F("trois")); choisirSequence(2); break;
default: toutEteindre(); etat = REPOS; break;
}
IrReceiver.resume();
}
switch (etat) {
case REPOS: break;
case ACTION:
if (etapeEnCours >= lesSequences[sequenceEnCours].nombreEtapes) {
if (millis() - debut >= lesSequences[sequenceEnCours].attenteAvantExtinction) {
toutEteindre();
etat = REPOS;
}
} else {
if (millis() - debut >= lesSequences[sequenceEnCours].dureeEntreEtapes[etapeEnCours]) {
allumer(etapeEnCours++);
debut = millis();
}
}
}
}