Using the following code to read the button presses from a remote control. The codes from the remote control are interpreted by a decoder module which passes incoming byte(s) to the Arduino RX pin. Works fine except multiple presses of a button can call the same function multiple times which is something I don't want. I am wondering if there is a way of debouncing or flushing the serial.read
void loop() {
while (Serial.available() > 0) { // is a character available?
incomingByte = Serial.read(); // get the character
if (incomingByte != 0x20) { // Throw out character header 32
// Serial.println(incomingByte);
if (incomingByte ==28)Power();
if (incomingByte ==29) SetFan(LOW);
if ( incomingByte ==16)SetFan(HIGH);
if (incomingByte ==17 )DisplayOnOff();
if (incomingByte ==25 )IncDecSetPoint();
}
}
}
aarg: @Bryanpl, are you referring to the phenomenon that some IR remotes have keys that send repeatedly if the button is held down?
Correct, if the remote button is pressed and held it will send repeatedly. I only want the code to act on one press otherwise flush, discard everything else from the remote until the particular function is finished.
PaulS:
Serial.read() can NOT possibly bounce. Bouncing is a physical characteristic of mechanical switches.
Yes poor choice of words, What I want to do throw away any subsequent serial data received from the remote AFTER a button on the remote is pushed, so that rapid re clicks or if the remote button is held doesn't continually loop through the functions. Brute force I suppose is to use serial.end () and restart serial once the function is finished.
What I want to do throw away any subsequent serial data received from the remote AFTER a button on the remote is pushed, so that rapid re clicks or if the remote button is held doesn't continually loop through the functions. Brute force I suppose is to use serial.end () and restart serial once the function is finished.
How will you determine that a byte represents new data and is to be retained vs. representing a duplicate code that is to be discarded?
The usual approach is to read ALL data as though it was good. Then, compare the current packet to the last packet processed. If not the same, process this packet. If they are the same AND the time between two events is small, discard the packet.
PaulS:
How will you determine that a byte represents new data and is to be retained vs. representing a duplicate code that is to be discarded?
The usual approach is to read ALL data as though it was good. Then, compare the current packet to the last packet processed. If not the same, process this packet. If they are the same AND the time between two events is small, discard the packet.