#include <IRremote.h>
const byte irPin = 2;
IRrecv recepteurIR(irPin);
// la LED RGB, cathode commune. on utilise des pins PWM
// la bibliothèque utilise le timer2 ce qui fait qu'on ne peu pas utilser le PWM sur les pins 3 et 11
const byte pinRouge = 10;
const byte pinVerte = 9;
const byte pinBleue = 6;
enum Commande {AUCUNE, ETEINDRE, ROUGE, VERT, BLEU, ANIMATION} commandeRecue = AUCUNE;
enum Etat {STATIQUE, DYNAMIQUE} etat = STATIQUE;
const unsigned long tempo = 500; // animation à 2Hz (500ms)
unsigned long dernierChangementCouleur;
struct IRKey {
uint8_t codeTouche;
const char * etiquette;
Commande commande;
};
IRKey lesTouches[] = {
{162, "POWER", ETEINDRE},
{226, "MENU", AUCUNE},
{ 34, "TEST", AUCUNE},
{ 2, "PLUS", AUCUNE},
{194, "BACK", AUCUNE},
{224, "PREV.", AUCUNE},
{168, "PLAY", AUCUNE},
{144, "NEXT", AUCUNE},
{104, "num: 0", ROUGE},
{152, "MINUS", AUCUNE},
{176, "key: C", AUCUNE},
{ 48, "num: 1", VERT},
{ 24, "num: 2", BLEU},
{122, "num: 3", ANIMATION},
{ 16, "num: 4", AUCUNE},
{ 56, "num: 5", AUCUNE},
{ 90, "num: 6", AUCUNE},
{ 66, "num: 7", AUCUNE},
{ 74, "num: 8", AUCUNE},
{ 82, "num: 9", AUCUNE},
};
void gestionCommande() {
commandeRecue = AUCUNE;
if (recepteurIR.decode()) {
for (auto& touche : lesTouches) {
if (touche.codeTouche == recepteurIR.decodedIRData.command) {
commandeRecue = touche.commande;
break;
}
}
recepteurIR.resume();
}
}
void gestionAnimation() {
switch (commandeRecue) {
case AUCUNE: break;
case ETEINDRE:
digitalWrite(pinRouge, LOW);
digitalWrite(pinVerte, LOW);
digitalWrite(pinBleue, LOW);
etat = STATIQUE;
break;
case ROUGE:
digitalWrite(pinRouge, HIGH);
digitalWrite(pinVerte, LOW);
digitalWrite(pinBleue, LOW);
etat = STATIQUE;
break;
case VERT:
digitalWrite(pinRouge, LOW);
digitalWrite(pinVerte, HIGH);
digitalWrite(pinBleue, LOW);
etat = STATIQUE;
break;
case BLEU:
digitalWrite(pinRouge, LOW);
digitalWrite(pinVerte, LOW);
digitalWrite(pinBleue, HIGH);
etat = STATIQUE;
break;
case ANIMATION:
dernierChangementCouleur = millis() - tempo;
etat = DYNAMIQUE;
break;
}
if ((etat == DYNAMIQUE) && (millis() - dernierChangementCouleur >= tempo)) {
// on passe à la couleur suivante, ici aléatoirement
analogWrite(pinRouge, random(256));
analogWrite(pinVerte, random(256));
analogWrite(pinBleue, random(256));
dernierChangementCouleur = millis();
}
}
void setup() {
pinMode(pinRouge, OUTPUT);
pinMode(pinVerte, OUTPUT);
pinMode(pinBleue, OUTPUT);
Serial.begin(115200);
recepteurIR.enableIRIn();
}
void loop() {
gestionCommande();
gestionAnimation();
}