Pages: [1]   Go Down
Author Topic: Debouncing 2 push buttons  (Read 661 times)
0 Members and 1 Guest are viewing this topic.
Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm new to Arduino so please spare me smiley
I have made a simple start / stop (2 push buttons) sketch that controls a digital output. When the start push button is pressed the output becomes high and stays that way until the stop push button is pressed. I had a lot of trouble getting that to work but I found an old topic where I found some code that exactly did this. Now I have also tried the debounce sketch and thought I would combine these 2 sketches so that both of my push buttons are debounced.
I've come up with the following code, it compiles fine and works but I'm not sure if the debouncing really works.
This is all pretty new to me, one of the things I don't really understand is the while(1) loop. Can someone explain this to me in a simple way?

Code:
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int ledPin =  13;
long lastDebounceTime1 = 0;     // the last time the output pin was toggled
long lastDebounceTime2 = 0;     // the last time the output pin was toggled
long debounceDelay = 50;       // the debounce time; increase if the output flickers
int lastReading1= LOW;         // the previous reading from the input pin 2
int lastReading2= LOW;         // the previous reading from the input pin 3

void setup() {
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int reading1 = digitalRead(buttonPin2);  // read the state of the button on pin 2 into a local variable:
  int reading2 = digitalRead(buttonPin3);  // read the state of the button on pin 3 into a local variable:
  if (reading1 != lastReading1) {
    // reset the debouncing timer
    lastDebounceTime1 = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading1 = reading1;
  }
  if (reading2 != lastReading2) {
    // reset the debouncing timer
    lastDebounceTime2 = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading2 = reading2;
  }
  while(1){
    if(digitalRead(buttonPin2) && (millis() - lastDebounceTime1) > debounceDelay){
    digitalWrite(ledPin, HIGH);
    }

    if(digitalRead(buttonPin3) && (millis() - lastDebounceTime2) > debounceDelay){
    digitalWrite(ledPin, LOW);
    }
  }
}
 
Logged


Austin, TX
Offline Offline
Faraday Member
**
Karma: 63
Posts: 6049
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've come up with the following code, it compiles fine and works but I'm not sure if the debouncing really works.
This is all pretty new to me, one of the things I don't really understand is the while(1) loop. Can someone explain this to me in a simple way?

You wrote the code, maybe you can explain what you were doing?  I see no reason for the while(1), except that you might have only wanted to de-bounce the buttons once and never do anything else again?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm, of course I want to debounce the push buttons all the time smiley
I started of with this code:
Code:
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int ledPin =  13; 

void setup() {
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(ledPin, OUTPUT);
}
void loop() {
  while(1){
    if(digitalRead(buttonPin2)){
    digitalWrite(ledPin, HIGH);
    }

    if(digitalRead(buttonPin3)){
    digitalWrite(ledPin, LOW);
    }
  }
}

And to this I wanted to add the debouncing. The debounce example sketch in the arduino ide didn't work. But I found this -> http://code.google.com/p/arduino/issues/attachmentText?id=293&aid=-7520718820891736381&name=Debounce_Fixed.pde&token=D8dGzfBbC72e-eXT9qxYRsLZt6s%3A1325004000479
I should do the debouncing in the while loop?

Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In your original sketch, while(1) is entirely redundant. You have placed an endless loop inside another endless loop.

Code:
void loop() {
  while(1){
    ...
  }
}
« Last Edit: December 27, 2011, 11:57:36 am by dxw00d » Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I've deleted the while(1). And I've now come up with this. The code compiles and runs fine.

Code:
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int ledPin =  13;
long lastDebounceTime1 = 0;     // the last time the output pin was toggled
long lastDebounceTime2 = 0;     // the last time the output pin was toggled
long debounceDelay = 50;       // the debounce time; increase if the output flickers
int lastReading1= LOW;         // the previous reading from the input pin 2
int lastReading2= LOW;         // the previous reading from the input pin 3

void setup() {
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int reading1 = digitalRead(buttonPin2);  // read the state of the button on pin 2 into a local variable:
  int reading2 = digitalRead(buttonPin3);  // read the state of the button on pin 3 into a local variable:
 
  if (reading1 != lastReading1) {
    // reset the debouncing timer
    lastDebounceTime1 = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading1 = reading1;
  }
 
  if (reading2 != lastReading2) {
    // reset the debouncing timer
    lastDebounceTime2 = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading2 = reading2;
  }
   
  if(digitalRead(buttonPin2) && (millis() - lastDebounceTime1) > debounceDelay){
    digitalWrite(ledPin, HIGH);
    }

  if(digitalRead(buttonPin3) && (millis() - lastDebounceTime2) > debounceDelay){
    digitalWrite(ledPin, LOW);
  }
}
Logged


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

Mostly I dedicate IDE is whole Ardunio folder for a project. Rename the folder with my project name. play , make any changes needed. i change the necessary changes to the core files. as the need of my project. I edit core for time critical routines. like which i need to run on defined timer. like i edit timer0 for my need removing off its millis and all that stuff. set my timer as my application needs. then write another code for delay where i can do delay(), but most of my functions are just based on counter that runs in timer isr. when the counter is reached. function specific flag is made high. in this way my single counter sets as many function flags. these flags are continue-sly checked in loop() and if it finds set respective function is called. and during execution the flag is cleared too. but I always dedicate one IDE for each project. in other words I only bother to take pains where functions are critical, for rest I use spoon feed by arduino.


above is my reply in other thread. but u can pickup hoe to use timer0 isr for your delay routing
The way I use timers ( you can use millis ) read value and based on current value (using if) you can call a function.
Logged

Pages: [1]   Go Up
Jump to: