Unsure How To Stop Repeated Codes From IR Remote

Hi All,

Long time reader first time poster... :o

I am currently researching and building a project that involves using a remote control to control relays and LED among other things. I have unfortunately reached a problem that Google has been unable to help me with.

The circuit for the IR receiver is wired correctly and I am receiving the HexDem Codes, but my remote control is from an old satellite TV box and when I push a button sends the value over and over. This results in lights and relays turning on and off quickly when what i wanted to do is turn it on or off once.

I am using the IRremote lib and was hoping someone could help me with a code that ignores if the code is repeated for a delay() time frame. I'm still new to this and would appreciate the help.

I am using the IRremote lib and was hoping someone could help me with a code that ignores if the code is repeated for a delay() time frame. I'm still new to this and would appreciate the help.

It would be useful (necessary, even) to see your existing code.

The blink without delay example shows how to do things at intervals without using delay(), like reading the IR receiver.

The state change detection example shows how to compare current data to previous data.

You should be able to combine ideas from the two examples to react quickly to two different values, but ignore the same value if it arrives within n milliseconds of a previous value.

If you don't want to repeat an action unless some other action has happened (ignore button n, once handled, until some other button is pressed), that is even easier.

1 Like

No need for a delay of any kind, and particularly do not be tempted to use the delay() function.

When you first receive the command execute the statements that you want and set a flag variable to true to indicate that it has been done. Only execute the statements when the flag is false. When you stop receiving the command set the flag to false ready for next time.

here is the current code I am using. It is just proof of concept at this stage but the problem still exists as above,

#include <IRremote.h>

int RECV_PIN = 3;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup() {
  irrecv.enableIRIn();
  
  pinMode(12, OUTPUT); //Relay one
  pinMode(10, OUTPUT);

  digitalWrite(12, LOW);
  digitalWrite(10, LOW);

}
 
void loop() {
  if (irrecv.decode(&results)) {
    irrecv.resume();  
      
  }

  if (results.value == 535030800) {
    relay_one();
    results.value = 0x00000000;
  }
  delay(500);

//   if(irrecv.decode(&results)) //this checks to see if a code has been received
//{
//    if(results.value == 535030800) //if the button press equals the hex value 0xC284
//  {
// (results.value == false);
//    delay(1000);
   // }
  //  irrecv.resume(); //receive the next value
//}


  
  if (results.value == 535026720) {
    led_one();
    results.value = 0x00000000;
  }
  //delay(750);

      
} 

void relay_one() {
  static int m = HIGH;
  m = !m;
  digitalWrite(12, m);
} 

void led_one() {
  static int m = HIGH;
  m = !m;
  digitalWrite(10, m);
}

I am rather new to Arduino and coding but from some of the tutorials i have read i was thinking that a sort of debounce function is what I am looking for but don't quite know how to go about it.

Only operate on a new rx code if there has been no code received for a certain amount of time, ex: 1/2 second.

.

Untested

void loop()
{
  static boolean doneRelayOne = false;
  static boolean doneLedOne = false;
  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  
  if (results.value == 535030800 && !doneRelayOne)
  {
    relay_one();
    doneRelayOne = true;
  }
  else if (results.value != 535030800)
  {
    doneRelayOne = false;
  }
  // if (results.value == 535026720 & !doneLedOne)  //corrected this line - thanks LarryD  
  if (results.value == 535026720 && !doneLedOne)  
  {
    led_one();
    doneLedOne = true;
  }
  else if (results.value != 535026720)
  {
    doneLedOne = false;
  }
}

if (results.value == 535026720 & !doneLedOne)
&&
if (results.value == 535026720 && !doneLedOne)

TVM. Code corrected and commented

First of all thanks to UKHeliBob and LarryD for the information and code. I have added them and it is working to a point. the only bug I am still trying to work out is the remote is requiring me to press another button before it will read e same code again. This shouldn't be a problem in the final project though as I will have to use other buttons before having to use the same.

Is there some way around this.

Thanks again, I wasn't sure I was going to fig this one out on my own.

I have added them

Please post your current code

#include <IRremote.h>

int RECV_PIN = 3;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup() {
  irrecv.enableIRIn();
  
  pinMode(12, OUTPUT); //Relay one
  pinMode(10, OUTPUT);

  digitalWrite(12, LOW);
  digitalWrite(10, LOW);

}
 
void loop()
{
  static boolean doneRelayOne = false;
  static boolean doneLedOne = false;
  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
 
  if (results.value == 535030800 && !doneRelayOne)
  {
    relay_one();
    doneRelayOne = true;
  }
  else if (results.value != 535030800)
  {
    doneRelayOne = false;
  }
  // if (results.value == 535026720 & !doneLedOne)  //corrected this line - thanks LarryD 
  if (results.value == 535026720 && !doneLedOne) 
  {
    led_one();
    doneLedOne = true;
  }
  else if (results.value != 535026720)
  {
    doneLedOne = false;
  }
}

void relay_one() {
  static int m = HIGH;
  m = !m;
  digitalWrite(12, m);
} 

void led_one() {
  static int m = HIGH;
  m = !m;
  digitalWrite(10, m);
}