Pages: [1]   Go Down
Author Topic: Debouncing IR  (Read 592 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So i have a nice little IR remote setup; its pretty useless but fun. I do have a slight problem though. My remote seems to like to send many copies of the same code(like pressing the button many times) so you end up having to barely push the button for it to register only one button press. Is there a way to debounce it or decrease the IR sampling freqency without using delay()?

« Last Edit: December 21, 2010, 08:01:11 pm by cubemike99 » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you could store the last command seen as well as the last time it was seen (using millis() ) and ignore that command if it hasn't been long enough (say, 500 ms)e
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 362
Posts: 17307
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the nature of most IR remote controls, they auto repeat the same code for as long as you hold the key down, and it's very difficult to make it send just one character no matter how quick a trigger finger you have.  smiley-wink

  You can deal with this behaviour by applying a little logic to your receiver software function. Instead of accepting every character you receive, just check to see if it's different then the last character you received. If it's the same character just ignore it in your code, if it's a different character then treat it as a valid new character. That make sense?

Lefty
« Last Edit: December 22, 2010, 12:59:17 am by retrolefty » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys. Ironically Lefty, the key that I want to be able to press twice in a row is the one that's giving me trouble. How would using millis() to wait x seconds after a code is received to receve the next code look like I was thinking

Code:
when code received
x = millis

after doing something

y = millis()

if y-x >= 500

receive next code


Logged

Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, that is the correct way to use millis (heed the y-x, don't turn those around! y-x will be roll-over safe, while x-y isn't).
I'd implement it like this:

if received command is different from previous, save it and save the millis timestamp.
else if the previously saved millis is more than 500 milliseconds older than current millis, save the new millis.
else don't do the command logic.

Obviously you can tweak the order of checks, I'd probably use a return in the final else so I can have the command code after the checks, without having to worry about the results.
Also, you could put the command handling stuff in its own function and call that from the respective if blocks.
Logged

Pages: [1]   Go Up
Jump to: