IR Receiver Debounce

Hi guys,

I am having a "contact bounce" problem with an IR receiver and a universal remote. Each button press produces 2 codes. My plan was to use one code to turn on and another code to turn it off. I got the input working using Ken Shirriff's IRremote library, and I was able to achieve a latch when a button was pressed. However, since the second code comes so fast after the first code its acting like a contact bounce. A simple delay helps it, but it doesn't fix it. The worst case is that I have to use a JK Flip Flop as a denounce, but id prefer a software fix. I have tried various online "software denounce" sources but nothing seems to work.

CODE: Testing it with an LED:

#include <IRremote.h>

int RECV_PIN = 11;
int LED = 4;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED, OUTPUT);  

}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);
    irrecv.resume(); // Receive the next value
  }  

  
  static bool toggle = false;

  if(toggle)
  {
    if(results.value == 1121798582)
    {
       digitalWrite(LED, HIGH);
    }
  }
  delay(60);
  if(!toggle)
  {
    if(results.value == 4294967295)
    {     
       digitalWrite(LED, LOW);  
    }
  }
  toggle = !toggle;
}

Does this do what you want?

#include <IRremote.h>

const int RECV_PIN = 11;
const int LED = 4;
bool outputState = false;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED, OUTPUT);  
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);
    irrecv.resume(); // Receive the next

    if(outputState == false && results.value == 1121798582)
    {
      outputState = true;
       digitalWrite(LED, outputState);
    }
  
    if(outputState == true && results.value == 4294967295)
    {     
       outputState = false;
       digitalWrite(LED, outputState);  
    }
  }
}

[/quote]

My Remote repeats the same code continuously, as long as a button is pressed,
but sends a different code ( ^ 0x800 ), when the same button is pressed twice.

With this feature it's easy to

  • either only act once on a longer press, by ignoring the same code until a different one shows up,
  • or to act continuously, e.g. in a volume control, when the duration of the button press is reflected by a number of small increments.

I would not call it "debounce"...

johnwasser:
Does this do what you want?

This looks like my code but with optimized if statements, is it not? I loaded it and it does not "denounce". Once the button is pressed, the LED turns on then immediately off again. It gets 1121798582, then 4294967295 milliseconds later.

michael_x:
My Remote repeats the same code continuously, as long as a button is pressed,
but sends a different code ( ^ 0x800 ), when the same button is pressed twice.

With this feature it's easy to

  • either only act once on a longer press, by ignoring the same code until a different one shows up,
  • or to act continuously, e.g. in a volume control, when the duration of the button press is reflected by a number of small increments.

I would not call it "debounce"...

Right, its definitely not a real denounce since it does not have physical contacts. I just used "debounce" to describe the problem since it acts the same. When I press a remote button, I get two codes simultaneously. I want to trigger a relay with it so I wanted the code to take a remote code and latch the relay on. Furthermore I wanted to unlatch the relay so I assumed the second code would work best. The problem is that its nearly impossible to enter the fist code without simultaneously entering the second code.

Thank you, I appreciate both of you for taking the time to respond.

When I press a remote button, I get two codes simultaneously.

No you get one sent immediately after the other.

I want to trigger a relay with it so I wanted the code to take a remote code and latch the relay on. Furthermore I wanted to unlatch the relay

So after you receive the first code and turn your relay on you need to add a delay of about 400mS or so, so that you miss the next code and you have time to take your finger off the button.

Grumpy_Mike:

When I press a remote button, I get two codes simultaneously.

No you get one sent immediately after the other.

I want to trigger a relay with it so I wanted the code to take a remote code and latch the relay on. Furthermore I wanted to unlatch the relay

So after you receive the first code and turn your relay on you need to add a delay of about 400mS or so, so that you miss the next code and you have time to take your finger off the button.

Correct, the two codes are appropriately 50ms apart once I hit a button (I think people understand that its pretty much impossible for the codes to be simultaneous). If you look at my first post you will see that I built in a delay of 60ms to prevent the second code from getting through. This is not an elegant solution to the problem which is why I am here. I am building something for a customer and the button needs to be held down to turn off the device, hence the 60ms delay.

This is not an elegant solution to the problem which is why I am here

Don't knock it. What ever the solution the effect is the same, in the context of your problem I would not worry about it.

dbmiller5:

johnwasser:
Does this do what you want?

This looks like my code but with optimized if statements, is it not? I loaded it and it does not "denounce". Once the button is pressed, the LED turns on then immediately off again. It gets 1121798582, then 4294967295 milliseconds later.

So you want to use one button to toggle the light on and the SAME button to toggle the light off and the one button sends two consecutive codes. Sorry, I thought you had an ON button and an OFF button.

#include <IRremote.h>

const int RECV_PIN = 11;
const int LED = 4;
bool outputState = false;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED, OUTPUT);  
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);
    irrecv.resume(); // Receive the next

    if(results.value == 1121798582)
    {
      outputState = !outputState;
       digitalWrite(LED, outputState);
    }
  }
}

johnwasser:
So you want to use one button to toggle the light on and the SAME button to toggle the light off and the one button sends two consecutive codes. Sorry, I thought you had an ON button and an OFF button.

PERFECT!! I am very grateful for your knowledge. +1 I dont know if I can give karma points, but if I can you will definitely earn the max.

Thank you for the code guys. I am new at this but I was able to modify the sketch to use 8 relays. Much thanks