Pages: [1]   Go Down
Author Topic: Debounce multiple I/O  (Read 705 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looking to implement this debounce code for a sketch that has multiple digital inputs, and multiple digital outputs. What is the best way to implement this, as this example is for one input and one output? Does anyone know of a sample code or something from the playground where this debounce has been implemented for multiple inputs? If you know, could you please share. Thanks so much.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Looking to implement this debounce code
What debounce code?

Quote
What is the best way to implement this
Only on the inputs that might bounce. If it hasn't changed, it isn't bouncing.

Logged

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

If you detect an input has change then don't look at that input again for 20mS.

Most of the time you don't need to specifically do anything to achieve this, the rest of the code acts as a delay. I very rarely have to take debounce precautions.
Logged

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

Perhaps it's easier to share some code:

In a nutshell, switch closed, it runs two outputs until the switch opens again. There are delays for 1 of the outputs for both turning them on and off.

I would like to debounce S1 and the other switches because it will be a very gradual contact closure and I'm concerned about bouncing. What's the best way to do a double read with a delay between reads on this setup? And should I include it in every switch check/run or just at the beginning and define some variables? Many thanks.

Code:
void loop() {
// Begin Water Tank 1 Logic
  if (digitalRead(S1) == LOW && curS == 0)
  {
    Serial.println("Filling Tank 1");
    digitalWrite(K1, HIGH);
    delay(3000);
    digitalWrite(K4, HIGH);
    curS = S1; 
  }
  else if (digitalRead(S1) == HIGH && curS == S1)
  {
    Serial.println("Tank 1 Full");
    digitalWrite(K4, LOW);
    delay(3000);
    digitalWrite(K1, LOW);
    curS = 0;
  }
Logged

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

Quote
because it will be a very gradual contact closure
No such thing, a contact is either closed or it is not.
Can you be more specific?

Have you seen effects of contact bounce or is this just what you think you need to do?

You have a
Code:
delay(3000);
in the code between detecting a contact closure and looking for the next. That's 3 seconds of debounce time already built into the thing, you need more?
« Last Edit: July 12, 2012, 12:07:24 pm by Grumpy_Mike » Logged

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

Mike,

Thanks for your response. Perhaps a better wording. It's a water level tank sensor, basically a microswitch electrically. I've seen it bounce but only with human interaction, actually closing the switch myself when I initially tested it with a breadboard on the arduino to get the logic I needed. I really have on idea if it will bounce in it's actual application. I would just like to de-bounce it or know how to when I get the system installed should it bounce. I guess maybe you've answered my question; I just wasn't sure if that delay I have in there for another reason is a sufficient way in itself to debounce it? I'm just trying to be thorough is all. Thanks Mike.
Logged

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

Normally a debounce delay would be 20mS, you have a delay of 3000mS, so another 20 is going to make no difference.
Logged

Pages: [1]   Go Up
Jump to: