Go Down

Topic: Debouncing IR (Read 636 times) previous topic - next topic

cubemike99

Dec 22, 2010, 02:00 am Last Edit: Dec 22, 2010, 02:01 am by cubemike99 Reason: 1
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()?


mspguy

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

retrolefty

#2
Dec 22, 2010, 06:58 am Last Edit: Dec 22, 2010, 06:59 am by retrolefty Reason: 1
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.  ;)

 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

cubemike99

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: [Select]

when code received
x = millis

after doing something

y = millis()

if y-x >= 500

receive next code



Imahilus

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.

Go Up