Pages: [1]   Go Down
Author Topic: IR Receiver Debounce  (Read 665 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
#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;
}
« Last Edit: December 05, 2012, 10:52:47 am by dbmiller5 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this do what you want?
Code:
#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]
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Germany
Offline Offline
Faraday Member
**
Karma: 57
Posts: 3006
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 620
Posts: 33991
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I press a remote button, I get two codes simultaneously.
No you get one sent immediately after the other.

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I press a remote button, I get two codes simultaneously.
No you get one sent immediately after the other.

Quote
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 620
Posts: 33991
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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);
    }
  }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: