/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include <IRremote.hpp>
const byte IR_RECEIVE_PIN = 2;
const byte PIN_CLIGN_G = 11; // Pin Led clignotant GAUCHE sur D11
const byte PIN_CLIGN_D = 12; // Pin Led clignotant DROIT sur D12
const byte PIN_MARCHE_AR = 8; // Pin Led clignotant DROIT sur D8
const unsigned long dureeClignotant = 500; // durée en ms
enum : uint64_t {REPOS = 0ull, GAUCHE = 1ull, DROITE = 2ull, WARNING = 4ull, RECUL = 8ull};
uint64_t etat = REPOS;
unsigned long chrono;
void eteindre() {
digitalWrite(PIN_CLIGN_G, LOW);
digitalWrite(PIN_CLIGN_D, LOW);
digitalWrite(PIN_MARCHE_AR, LOW);
}
void gauche() {
// on a appuyé le clignotant gauche
if (etat & DROITE) { // si le droit était activé on désactive
etat &= ~DROITE;
if (!(etat & WARNING)) digitalWrite(PIN_CLIGN_D, LOW); // on éteint que si les warning ne sont pas actifs
}
if (etat & GAUCHE) { // si le gauche était activé on désactive
if (!(etat & WARNING)) digitalWrite(PIN_CLIGN_G, LOW); // on éteint que si les warning ne sont pas actifs
etat &= ~GAUCHE;
} else {
etat |= GAUCHE; // sinon on l'active
}
chrono = millis() - dureeClignotant;
}
void droite() {
// on a appuyé le clignotant droite
if (etat & GAUCHE) { // si le gauhe était activé on désactive
etat &= ~GAUCHE;
if (!(etat & WARNING)) digitalWrite(PIN_CLIGN_G, LOW); // on éteint que si les warning ne sont pas allumés
}
if (etat & DROITE) { // si le droite était activé on désactive
etat &= ~DROITE;
if (!(etat & WARNING)) digitalWrite(PIN_CLIGN_D, LOW); // on éteint que si les warning ne sont pas allumés
} else {
etat |= DROITE; // sinon on l'active
}
chrono = millis() - dureeClignotant;
}
void warning() {
if (etat & WARNING) { // si le WARNING était activé on l'éteint
etat &= ~WARNING;
digitalWrite(PIN_CLIGN_G, LOW);
digitalWrite(PIN_CLIGN_D, LOW);
} else {
etat |= WARNING; // sinon on l'active
}
chrono = millis() - dureeClignotant;
}
void recul() {
if (etat & RECUL) {
digitalWrite(PIN_MARCHE_AR, LOW);
etat &= ~RECUL; // si le RECUL était activé on l'éteint
} else {
// sinon on l'active
digitalWrite(PIN_MARCHE_AR, HIGH);
etat |= RECUL;
}
chrono = millis();
}
void repos() {
eteindre();
etat = REPOS;
}
void gestionCommande() {
if (IrReceiver.decode()) {
switch (IrReceiver.decodedIRData.command) {
case 162: Serial.println("POWER"); repos(); break;
case 226: Serial.println("MENU"); repos(); break;
case 34: Serial.println("TEST ➜ WARNING"); warning(); break;
case 2: Serial.println("PLUS"); repos(); break;
case 194: Serial.println("BACK ➜ RECUL"); recul(); break;
case 224: Serial.println("PREV ➜ GAUCHE"); gauche(); break;
case 168: Serial.println("PLAY"); repos(); break;
case 144: Serial.println("NEXT ➜ DROITE"); droite(); break;
case 104: Serial.println("num: 0"); repos(); break;
case 152: Serial.println("MINUS"); repos(); break;
case 176: Serial.println("key: C"); repos(); break;
case 48: Serial.println("num: 1"); repos(); break;
case 24: Serial.println("num: 2"); repos(); break;
case 122: Serial.println("num: 3"); repos(); break;
case 16: Serial.println("num: 4"); repos(); break;
case 56: Serial.println("num: 5"); repos(); break;
case 90: Serial.println("num: 6"); repos(); break;
case 66: Serial.println("num: 7"); repos(); break;
case 74: Serial.println("num: 8"); repos(); break;
case 82: Serial.println("num: 9"); repos(); break;
default:
Serial.print(IrReceiver.decodedIRData.command);
Serial.println("=> bouton inconnu");
}
IrReceiver.resume();
}
}
void gestionAnimation() {
if (etat & WARNING) { // LES WARNING SONT PRIORITAIRES SUR LES CLIGNOTANTS
if (millis() - chrono >= dureeClignotant) {
digitalWrite(PIN_CLIGN_G, digitalRead(PIN_CLIGN_G) == HIGH ? LOW : HIGH);
digitalWrite(PIN_CLIGN_D, digitalRead(PIN_CLIGN_D) == HIGH ? LOW : HIGH);
chrono = millis();
}
} else {
if (etat & GAUCHE) {
if (millis() - chrono >= dureeClignotant) {
digitalWrite(PIN_CLIGN_G, digitalRead(PIN_CLIGN_G) == HIGH ? LOW : HIGH);
chrono = millis();
}
}
if (etat & DROITE) {
if (millis() - chrono >= dureeClignotant) {
digitalWrite(PIN_CLIGN_D, digitalRead(PIN_CLIGN_D) == HIGH ? LOW : HIGH);
chrono = millis();
}
}
}
}
void setup() {
pinMode(PIN_CLIGN_G, OUTPUT);
pinMode(PIN_CLIGN_D, OUTPUT);
pinMode(PIN_MARCHE_AR, OUTPUT);
Serial.begin(115200); Serial.println();
Serial.println(F("-------------------------"));
Serial.println(F("------- COMMANDES -------"));
Serial.println(F("-------------------------"));
Serial.println(F("TEST\t➜ WARNING"));
Serial.println(F("BACK\t➜ RECUL"));
Serial.println(F("PREV\t➜ CLIGNOTANT GAUCHE"));
Serial.println(F("NEXT\t➜ CLIGNOTANT DROITE"));
Serial.println(F("AUTRE\t➜ ETAT NOMINAL"));
Serial.println(F("-------------------------"));
IrReceiver.begin(IR_RECEIVE_PIN);
}
void loop() {
gestionCommande();
gestionAnimation();
}GAUCHE
DROITE
ARRIERE