Pages: [1]   Go Down
Author Topic: Finding hard to understand de-bouncing code.  (Read 362 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All,

Here is the code(Please be aware that this code comes from the book of Massimo Banzi "Getting Started with Arduino". I kind of understand the concept behind bouncing, basically its about the switch not doing right connection and produce erratic results(e.g: the led not turning off, when I pressed the switch to turn it off). Is that right? Now, How does the delay(10) function prevents bouncing?  smiley-confuse See code below, please:

// Example 03C: Turn on LED when the button is pressed and keep it on after it is released.
// including simple de-bouncing
// Now with another new and improved formula!

const int LED = 13; // the pin for the LED
const int BUTTON = 7;  // the input pin where the pushbutton is connected.

int val = 0;  // val will be used to store the state of the input pin

int old_val= 0; //this variable stores the previous value of "val"

int state = 0; // 0 = LED off while 1 = LED on

void setup(){
  pinMode (LED, OUTPUT); //tell Arduino LED is an output
  pinMode (BUTTON, INPUT); //tell Arduino Button is an input
}

void loop(){
  val = digitalRead(BUTTON); // read input value and store it
 
  // check if the inpout is HIGH (button pressed)
  //change the state ***** This is the part that I dont understand. Why do I have to use the delay function?
  if ((val == HIGH) && (old_val == LOW)){
    state = 1 - state;
    delay(10);
  }
 
  old_val = val; //val is now old, let's store it
 
  if (state == 1){
    digitalWrite (LED,HIGH); // turn LED ON
  } else{
    digitalWrite (LED, LOW);
  }
}


----------------
Many thanks in advance.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How does the delay(10) function prevents bouncing?
Put simply, it doesn't and cannot.
Bounce is a mechanical effect, and will take place whether or not there is a delay.

What it does do is allow the bounce to complete.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Hello Awol! Many thanks, I understand now.  smiley
Logged

Offline Offline
Edison Member
*
Karma: 20
Posts: 2107
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

olivettix, it took me a while, but debouncing is simply for the part where metal to metal contact makes place (lifting/pressing) and as you do that, where the human can barely count faster than 1 a second, the CPU on the other hand can register the button touching thousands of times in that brief moment it makes contact.

so 100ms delay is better than 10ms, but that's up to you.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so 100ms delay is better than 10ms, but that's up to you.

Or even better, debounce without delay().
Logged

Pages: [1]   Go Up
Jump to: