Je me lance dans mon premier projet Arduino.
L'objectif est de commander des leds à partir d'une télécommande IR.
Je souhaiterai agrémenter mes maquettes de camions de led pour les phares, clignotants, gyrophare etc etc...
J'ai lu pas mal de post sur ce type de projet qui semble être un classique. Mais j'en ai trouvé aucun qui fonctionne. Et comme j'aime bien comprendre le pourquoi du comment, je préfère écrire mon propre code et faire mon montage électronique.
Cela fait 2 semaines que je travaille sur le code et j'ai réussi a comprendre les commandes de l'infra rouge, le remplacement des delay par des millis et la création d'une machine à état.
Enfin, je comprends... dans l'ensemble. Mais dans le détail, je patauge sur un point.
La gestion des états étends / allumé.
Pour l'instant ce code ce compose de 3 fonctions : Clignotants droite/gauche, warning et la marche arrière.
/*
Programe RC_Kit
Version : 0.1
Parent version : 0.08
Date : 05/08/2024
*/
// Librairie associée
#include <IRremote.hpp>
// Afecation des PIN
const int PIN_RECEIVER = 2; // Pin de la connexion du receveur IR sur D2
const int PIN_CLIGN_G = 11; // Pin Led clignotant GAUCHE sur D3
const int PIN_CLIGN_D = 12; // Pin Led clignotant DROIT sur D4
const int PIN_MARCHE_AR = 8; // Pin Led clignotant DROIT sur D4
// Affectation des codes IR
const int Bouton1 = 0x45;
const int Bouton2 = 0x46;
const int Bouton3 = 0x47;
const int Bouton4 = 0x44;
const int Bouton5 = 0x40;
const int Bouton6 = 0x43;
const int IREclairageExt = 0x7;
const int IRPhare = 0x15;
const int IRPleinPhare = 0x9;
const int IREclairageInt = 0x19;
const int IRSpot = 0x16;
const int IRGyrophare = 0xD;
const int IRStopCligno = 0x18;
const int IRGauche = 0x8;
const int IRWarning = 0x1C;
const int IRDroit = 0x5A;
const int IRMarcheArriere = 0x52;
// variable
bool EtatWarning = 0; // Etat des leds High Low pour les LEDS Warnings
bool EtatMarcheAR = 0;
unsigned long Tempo_Clign = 666;
int irResult; //stockage du code IR
unsigned long currentMillis;
unsigned long previousMillis = 0;
//********************************************************************************
//* CODE AU DEMARRAGE
//********************************************************************************
void setup() {
Serial.begin(9600);
Serial.print("\n");
pinMode(LED_BUILTIN, OUTPUT);
//IrReceiver.begin(PIN_RECEIVER);
IrReceiver.begin(PIN_RECEIVER, ENABLE_LED_FEEDBACK);
Serial.print("IR ok \n");
// Déclaration des Entrées / Sorties
pinMode(PIN_RECEIVER, INPUT);
pinMode(PIN_CLIGN_G, OUTPUT);
pinMode(PIN_CLIGN_D, OUTPUT);
pinMode(PIN_MARCHE_AR, OUTPUT);
}
//********************************************************************************
//* CODE EN BOUCLE
//********************************************************************************
void loop() {
if (IrReceiver.decode()) // Si le recepteur IR recoit quelquechose...
decodeResults(); // Determine quel bouton a été appuyé
IrReceiver.resume();
// GaucheOuDroite(irResult); // Subroutine Clignote de quel côté
Warning(irResult, EtatWarning);
// MarcheArriere(irResult, EtatMarcheAR);
}
//********************************************************************************
//* CODE A LA DEMANDE (SUBROUTINE)
//********************************************************************************
void GaucheOuDroite(int irResult) {
if (irResult == IRGauche || irResult == IRDroit) { // compare the results to LEFT and RIGHT
if (irResult == IRGauche) {
digitalWrite(PIN_CLIGN_D, LOW); // turn off the other LED
Clignote(PIN_CLIGN_G); // blink this LED
}
if (irResult == IRDroit) {
digitalWrite(PIN_CLIGN_G, LOW); // turn off the other LED
Clignote(PIN_CLIGN_D); // blink this LED
}
}
}
void Clignote(int whichSide) {
currentMillis = millis(); // start an event timer
if ((currentMillis - previousMillis) > Tempo_Clign) { // compare "last" timer to "this" timer
previousMillis = currentMillis; // if greater thean "interval" save the "last" timer
digitalWrite(whichSide, !digitalRead(whichSide)); // and blink the correct LED
}
}
void Warning(int irResult, bool EtatWarning) {
if ((irResult == IRWarning) && (EtatWarning == LOW)) {
currentMillis = millis(); // start an event timer
if ((currentMillis - previousMillis) > Tempo_Clign) { // compare "last" timer to "this" timer
previousMillis = currentMillis;
digitalWrite(PIN_CLIGN_G, !digitalRead(PIN_CLIGN_G)); // and blink the correct LED
digitalWrite(PIN_CLIGN_D, !digitalRead(PIN_CLIGN_D)); // and blink the correct LED
}
} else if (EtatWarning == HIGH) {
digitalWrite(PIN_CLIGN_G, LOW);
digitalWrite(PIN_CLIGN_D, LOW);
}
}
void decodeResults() {
irResult = (IrReceiver.decodedIRData.command); // find the code
if (irResult) { // if the code was other than 0x00
// IrReceiver.resume();
Serial.print("irResult: 0x"); // show the received code
Serial.println(irResult, HEX);
//irResult == 0;
}
switch (irResult) {
case IRMarcheArriere:
digitalWrite(PIN_MARCHE_AR, !digitalRead(PIN_MARCHE_AR));
break;
case IRGauche:
GaucheOuDroite(irResult);
break;
case IRDroit:
GaucheOuDroite(irResult);
break;
case IRWarning:
EtatWarning = !EtatWarning;
Serial.println("Etat warning : ");
Serial.println(EtatWarning);
}
delay(500);
}
Ce qui fonctionne
L'IR avec réception et décodage des codes
Mon problème, au démarrage je lance par exemple les warnings, les 2 leds clignotants bien en même temps. J'appuie alors sur le bouton pour la marche arrière, la led de la marche arrière s'allume mais la boucle pour les clignotants est stoppés. Les leds sont en état High ou low en fonction du moment ou j’appuie pour la marche arrière.
Plus clairement, je n'arrive pas à gérer la sérialisation des commandes d'allumage et d'extinction des leds.
Une idée ou je me fourvoie dans le code ?
J'ai déjà essayé sans delay mais le fonctionnement est plus compliqué car cela prend en compte les "rebonds" ou le code du fait de rester appuyer trop longtemps sur les boutons de la télécommande.
J'ai retenté en supprimant le delay et mon problème persiste avec en plus le fait de devoir être adroit dans l'appui des boutons sur la télécommande.
Existe t'il fonction dans la librairie IRRemote qui gère ce point ?
Le bar c'est pour tout ce qui ne touche pas de près ou de loin à Arduino. Et il est beaucoup moins visité.
Fil de discussion déplacé dans la racine du forum Français.
Le Delay permet de supprimer les doublons de code qui vient de la télécommande si l'on reste appuyer trop longtemps. Quand le code IR arrive en continue ça fait alterner certaines fonctions et ne donne pas le résultat escompté.
Merci xfpd.
Je vais regarder ça.
Si je comprends que de manière informatique c'est plus approprié pourquoi rajouter du code par rapport a cette ligne qui fait le travaille ?
Je suis de nouveau en panne séche avec la machine à état.
Je suis persuadé que c'est la bonne solution mais je n'arrive pas à l'appliquer.
J'arrive a mettre le clignotant droit ou gauche, mais je n'arrive pas a faire fonctionner les warnings. Les états se mélangent et les diodes se mettent à clignoter de façon anarchique ou pas du tout.
Voici le schéma logique
Je remets le code car je l'ai un peu modifié pour tenter une nouvelle approche
/*
Programe RC_Kit
Version : 0.1
Parent version : 0.08
Date : 05/08/2024
*/
// Librairie associée
#include <IRremote.hpp>
// Afecation des PIN
const int PIN_RECEIVER = 2; // Pin de la connexion du receveur IR sur D2
const int PIN_CLIGN_G = 11; // Pin Led clignotant GAUCHE sur D3
const int PIN_CLIGN_D = 12; // Pin Led clignotant DROIT sur D4
const int PIN_MARCHE_AR = 8; // Pin Led clignotant DROIT sur D4
// Affectation des codes IR
const int Bouton1 = 0x45;
const int Bouton2 = 0x46;
const int Bouton3 = 0x47;
const int Bouton4 = 0x44;
const int Bouton5 = 0x40;
const int Bouton6 = 0x43;
const int IREclairageExt = 0x7;
const int IRPhare = 0x15;
const int IRPleinPhare = 0x9;
const int IREclairageInt = 0x19;
const int IRSpot = 0x16;
const int IRGyrophare = 0xD;
const int IRStopCligno = 0x18;
const int IRGauche = 0x8;
const int IRWarning = 0x1C;
const int IRDroit = 0x5A;
const int IRMarcheArriere = 0x52;
// variable
bool EtatWarning = 1; // Etat des leds High Low pour les LEDS Warnings
bool EtatClignotantG = 1;
bool EtatClignotantD = 1 ;
bool EtatMarcheAR = 0;
bool EtatLedG = 1;
bool Gau = 0;
bool EtatLedD = 1;
bool Droi = 0;
unsigned long Tempo_Clign = 666;
int irResult; //stockage du code IR
unsigned long currentMillisW;
unsigned long previousMillisW = 0;
unsigned long currentMillisC;
unsigned long previousMillisC = 0;
//********************************************************************************
//* CODE AU DEMARRAGE
//********************************************************************************
void setup() {
Serial.begin(9600);
Serial.print("\n");
pinMode(LED_BUILTIN, OUTPUT);
//IrReceiver.begin(PIN_RECEIVER);
IrReceiver.begin(PIN_RECEIVER, ENABLE_LED_FEEDBACK);
Serial.print("IR ok \n");
// Déclaration des Entrées / Sorties
pinMode(PIN_RECEIVER, INPUT);
pinMode(PIN_CLIGN_G, OUTPUT);
pinMode(PIN_CLIGN_D, OUTPUT);
pinMode(PIN_MARCHE_AR, OUTPUT);
}
//********************************************************************************
//* CODE EN BOUCLE
//********************************************************************************
void loop() {
if (IrReceiver.decode()) // Si le recepteur IR recoit quelquechose...
decodeResults(); // Determine quel bouton a été appuyé
IrReceiver.resume();
Clignotant(EtatLedG, EtatLedD);
}
//********************************************************************************
//* CODE A LA DEMANDE (SUBROUTINE)
//********************************************************************************
void Clignotant (bool EtatLedG, bool EtatLedD) {
if ((EtatLedG == LOW) && (EtatLedD == HIGH)) {
Gau = !Gau;
Droi = LOW;
currentMillisC = millis(); // start an event timer
if ((currentMillisC - previousMillisC) > Tempo_Clign) { // compare "last" timer to "this" timer
previousMillisC = currentMillisC;
digitalWrite(PIN_CLIGN_D, Droi); // and blink the correct LED
digitalWrite(PIN_CLIGN_G, Gau); // and blink the correct LED}
}
} else if ((EtatLedG == HIGH) && (EtatLedD == LOW)) {
Gau = LOW;
Droi = !Droi;
currentMillisC = millis(); // start an event timer
if ((currentMillisC - previousMillisC) > Tempo_Clign) { // compare "last" timer to "this" timer
previousMillisC = currentMillisC;
digitalWrite(PIN_CLIGN_D, Droi); // and blink the correct LED
digitalWrite(PIN_CLIGN_G, Gau); // and blink the correct LED}
}
} else if ((EtatLedG == LOW) && (EtatLedD == LOW)) {
Gau = !Gau;
Droi = !Droi;
currentMillisC = millis(); // start an event timer
if ((currentMillisC - previousMillisC) > Tempo_Clign) { // compare "last" timer to "this" timer
previousMillisC = currentMillisC;
digitalWrite(PIN_CLIGN_D, Droi); // and blink the correct LED
digitalWrite(PIN_CLIGN_G, Gau); // and blink the correct LED}
}
}
}
void decodeResults() {
irResult = (IrReceiver.decodedIRData.command); // find the code
if (irResult) { // if the code was other than 0x00
// IrReceiver.resume();
Serial.print("irResult: 0x"); // show the received code
Serial.println(irResult, HEX);
//irResult == 0;
}
switch (irResult) {
case IRMarcheArriere:
Serial.print("MARCHE ARRIERE");
digitalWrite(PIN_MARCHE_AR, !digitalRead(PIN_MARCHE_AR));
break;
case IRGauche:
EtatClignotantG = !EtatClignotantG;
if (EtatClignotantG == LOW) {
EtatLedD = LOW;
EtatLedG = HIGH;
} else {
EtatLedD = LOW;
EtatLedG = LOW;
}
EtatLedD = HIGH;
Serial.println("Etat Clignotant G : ");
Serial.println(EtatLedG);
break;
case IRDroit:
EtatClignotantD = !EtatClignotantD;
if (EtatClignotantD == LOW) {
EtatLedD = HIGH;
EtatLedG = LOW;
} else {
EtatLedD = LOW;
EtatLedG = LOW;
}
Serial.println("Etat Clignotant D : ");
Serial.println(EtatLedD);
break;
case IRWarning:
EtatWarning = !EtatWarning;
if (EtatWarning == LOW) {
EtatLedD = HIGH;
EtatLedG = HIGH;
} else {
EtatLedD = LOW;
EtatLedG = LOW;
}
break;
Serial.println("Etat warning : ");
Serial.println(EtatWarning);
Serial.println(EtatLedD);
Serial.println(EtatLedG);
break;
default:
Serial.println("Code inconnu !");
break;
}
// delay(50);
}
Comment arrêter un une led dans une fonction pour l'allumer par une autre ?
Vous essayez de faire trop de choses trop tôt. Faites un autre croquis qui effectue l'une de vos tâches, puis ajoutez une autre tâche jusqu'à ce qu'elle soit terminée. Voici une simulation similaire à votre projet, mais très simple... seulement GAUCHE, DROITE et ARRÊT. Comparez les similitudes.
Ton code ne ressemble pas vraiment à une machine à état.
Dans une machine à état, si tu es dans un état clignotement gauche, alors tu fais ce qu'il faut faire, c'est à dire rien, allumé led, eteindre led.
Tu peux aussi enlever le fait de rien faire écrivant sur la broche de la LED, la valeur de EtatLedG, puis tester si il est temps de change son état.
Donc il faut définir tes états en écrivant ce que tu fais dans chaque état, ce qui te fait changer d'état.
voici un code d'exemple sur wokwi qui pourra vous donner des idées (j'ai caché les fils pour que ce soit plus visuel)
le code
/* ============================================
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();
}