IR Remote : Random results when message in loop start

Hi all,
I am quite a noob, sorry If I am not respecting some convention or rule I am not aware of.
I have a code that play a SOS morse code that makes 2 leds blink alternatively in a sort of dialogue.
I need to add some control through a ir remote. I have 3 buttons : button1 activate the loop sequence, button2 turn off the leds and should stops the loop, button3 turn on the leds and stops the loop too.
When in the serial monitor, I am getting the right Hex when pushing button2 and button3 but as soon as I push button1 and start the loop sequence I am getting completely random HEx in the serial monitor.... that looks like this :"
"IR Value: 0xBB44FF00
IR Value: 0x0
IR Value: 0xBE41FF00
IR Value: 0x0
IR Value: 0xBF40FF00
IR Value: 0x0
IR Value: 0x0
IR Value: 0x215235BA
IR Value: 0xF24D9E87
IR Value: 0x2C7BC3CC
IR Value: 0x22
IR Value: 0x13981877
IR Value: 0x9482B032
IR Value: 0x7395088C"

here is my code :slight_smile:

#include <IRremote.h>
//
int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;

#define IR_Button_1 0xBF40FF00
#define IR_Button_2 0xBE41FF00
#define IR_Button_3 0xBB44FF00

const int led1 = 6; //variable for LED pin
const int led2 = 11; //variable for LED pin
const byte outPin = 13;

int buzzer = 5;
int unit = 80; //variable for delay of 1 unit in milliseconds
int tonefreq = 100; //freq in Hertz

int ledState1 = HIGH;
int ledState2 = HIGH;

int brightness = 0;    // how bright the LED is
int fadeAmount = 1; 

const int fadeDuration = 1000; // Time duration for the fade (in milliseconds)
const int fadeStep = 5; // The step value to increase/decrease brightness in each iteration

unsigned long startTime; // Variable to store the start time of the fade
int currentBrightness = 0; // Variable to store the current brightness level

int blinkingState = 2;
// -------------------
int message[] = {6,4,4,4,5,4,1,1,1,3,2,2,2,3,1,1,1,4,
                 11,11,11,3,12,12,12,3,11,11,11,4,
                 51,51,51,3,51,52,3,51,51,51,52,3,
                 51,3,4,52,52,52,3,51,51,52,3,51,
                 52,51,3,4,51,51,51,3,2,2,2,3,
                 1,1,2,3,11,12,11,11,3,1,1,1,
                 3,4,100                    
                 }; // dot = 1, dash = 2, inter letter space = 3, inter word space = 4
                 
int messageLength = 81;
int messagePointer = 0;
unsigned long interval = 80; // mS per morse unit time
unsigned long workingInterval = 0;
unsigned long stateTime = 0;
boolean generatingMessage = false;



void setup() {
  pinMode(outPin, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(buzzer, OUTPUT);
  irrecv.enableIRIn();
  irrecv.blink13(true);
  attachInterrupt(digitalPinToInterrupt(RECV_PIN), remote, CHANGE);
  Serial.begin(9600);

}

void loop() {
 message1();
 remote();
}

void remote(){

if (irrecv.decode()){
    Serial.print("IR Value: 0x");
    Serial.println(irrecv.decodedIRData.decodedRawData, HEX);

          switch(irrecv.decodedIRData.decodedRawData){
          case IR_Button_1: //Keypad button "on"
          blinkingState = 0;
          break;
          }

          switch(irrecv.decodedIRData.decodedRawData){
          case IR_Button_2: //Keypad button "off"
          blinkingState = 1;
          break;
          //StopAll();
          //messagePointer = 0;
          }

          switch(irrecv.decodedIRData.decodedRawData){
          case IR_Button_3: //Keypad button "w"
          blinkingState = 2;
          break;
          //StopAll();
          //messagePointer = 0;
          }
        irrecv.resume(); 
    }
}

void message1() {
  switch (blinkingState) {
    case 0: // Blink LEDs based on Morse code
      if (millis() - stateTime > workingInterval) {
        updateMessage();
      }
      break;
    case 1: // Turn off LEDs and buzzer
      StopAll();
      messagePointer = 0;
      break;
    case 2: // Turn on LEDs and turn off the buzzer
      FullOn();
      messagePointer = 0;
      break;
  }
}


void FullOn(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    noTone(buzzer);
}

void StopAll(){
  analogWrite(led1, 0);
  analogWrite(led2, 0);
  noTone(buzzer);
}




void updateMessage(){
   
  // set up time to next call of this function
  if(message[messagePointer] == 1) workingInterval = interval;
  if(message[messagePointer] == 2) workingInterval = interval * 3;
  if(message[messagePointer] == 3) workingInterval = interval * 3;
  if(message[messagePointer] == 4) workingInterval = interval * 7;
  if(message[messagePointer] == 5) workingInterval = interval * 100;
  if(message[messagePointer] == 6) workingInterval = interval * 100;
  if(message[messagePointer] == 100) workingInterval = interval * 2000;
  if(message[messagePointer] == 11) workingInterval = interval;
  if(message[messagePointer] == 12) workingInterval = interval * 3;
  
  if(message[messagePointer] == 51) workingInterval = interval;
  if(message[messagePointer] == 52) workingInterval = interval * 3;  
  
  stateTime = millis();

// toggle output
  
  if(message[messagePointer] == 1 || message[messagePointer] == 2){
   if(digitalRead(led1) == HIGH) {
     digitalWrite(led1, LOW);
     digitalWrite(led2, HIGH);
     tone(buzzer, tonefreq);
   } 
   else {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    noTone(buzzer);
    updatePointer();
   }
  }


else if(message[messagePointer] == 11 || message[messagePointer] == 12){
   if(digitalRead(led2) == HIGH) {
     digitalWrite(led1, HIGH);
     digitalWrite(led2, LOW);
     tone(buzzer, tonefreq);
   } 
   else {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    noTone(buzzer);
    updatePointer();
   }
  }


else if(message[messagePointer] == 51 || message[messagePointer] == 52){
   if(digitalRead(led2) == HIGH) {
     digitalWrite(led1, LOW);
     digitalWrite(led2, LOW);
     tone(buzzer, tonefreq);
   } 
   else {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    noTone(buzzer);
    updatePointer();
   }
  }

else if(message[messagePointer] == 100){
//noTone(buzzer);
     digitalWrite(led1, HIGH);
     digitalWrite(led2, HIGH);
     updatePointer();
   } 
  else {
    if(message[messagePointer] == 5){
    //fadeIn1();
    //updatePointer();
    }

    if(message[messagePointer] == 6){
    //fadeOut1();
    //updatePointer();
    }   
   updatePointer();
  }  
}


void updatePointer(){
  
  messagePointer++;
  if(messagePointer >= messageLength){ // end of message
    generatingMessage = false;
    messagePointer = 0;
  } 
}

thank you in advance

Your code does not compile :
Error msgs:
'fadeIn1' was not declared in this scope
'fadeOut1' was not declared in this scope

PS:
I commented the 2 function calls, and your code works correctly in the simulator.

" IR_Forum - Wokwi ESP32, STM32, Arduino Simulator

my bad, sorry ... I deactivated those functions I was afraid it creates problems cause I couldn't do a proper fade without the delay() function.

yes, that's what I don't understand ... the code works properly till I activate the loop sequence ...
when the loop sequence starts I receive random HEX whatever button I push ...
:frowning:

and therefore I am not able to stop the loop.

What is this " loop sequence " ?

the message[] that is activated by blinkstate = 0;

Have you posted a compilable sketch yet? We need it. Safer, for you to make the edits, than for us to piece it together.

Not really, however I do hope that you have read the forum introductory guide.

I did edited the code... It should be compilable now !

Thanks! Just as a point of order, please don't edit previous posts that have been commented on. Just add them in a new message.

This is a very unconventional use of the IR library. Why did you create your own ISR to do what the library does by itself? Look at the IR library example sketches, they don't have anything like that going on.

A big risk, interrupts are disabled while running an ISR. So how might that affect the library calls you make inside it? Also the Serial calls, they depend on interrupts to run, you call Serial multiple times inside the ISR.

Just stick to the basics and you'll be fine.

it was advised to me to use an interrupt at an early phase of the project. I deleted this line just now . But my problem persist, I am still getting random result when I match with the remote blinkstate = 0. From that point, whatever buttons I push on the remote I am having a different value.

this is what I see in the serial, pushing the same button :
"IR Value: 0x834468F3
IR Value: 0x4015A59
IR Value: 0x80ED7778
IR Value: 0x76807752
IR Value: 0xD87E450D"

I guess you should avoid taking any advice from that source in future...

I deleted this line just now .

What line? If you made any changes, post the entire updated sketch in a new message. Deleting any single individual line in your sketch in this case, must be misguided.

I deleted the line "attachInterrupt(digitalPinToInterrupt(RECV_PIN), remote, CHANGE);" in my own code to verify if it had any impact on my problem. But like I said I doesn't affect in any way the behaviour that I am trying to solve here. Therefore I don't see the necessity to repost a whole new message with this line deleted even if it's a "very unconventional use of the IR library".
I am sorry, I am learning

Fine, good luck with your project. Bye.

Other forum members do see the necessity. Code needs to be presented in full, so it can be discussed intelligently.

If you want help, please read and follow the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.

You are right ! I didn't think about the other forum members ... my bad. My code is already presented in full at the initial post.

The code in the first post is not the code you mention in post #15, because it contains the very problematic line

Please start over in your next post. Post code that has a problem (using code tags), and describe what the code should do, and what it does instead.

Better, start with the library examples, study the documentation, and learn to use the library properly.

ok, I'll be more careful on my next post.
thank you