Go Down

Topic: Debouncing 2 push buttons (Read 1 time) previous topic - next topic

Bajdi

Hi, I'm new to Arduino so please spare me :)
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: [Select]
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);
    }
  }
}

 
www.bajdi.com

James C4S


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?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Bajdi

Hmm, of course I want to debounce the push buttons all the time :)
I started of with this code:
Code: [Select]
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?

www.bajdi.com

dxw00d

#3
Dec 27, 2011, 05:51 pm Last Edit: Dec 27, 2011, 05:57 pm by dxw00d Reason: 1
In your original sketch, while(1) is entirely redundant. You have placed an endless loop inside another endless loop.

Code: [Select]
void loop() {
  while(1){
    ...
  }
}

Bajdi

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

Code: [Select]
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);
  }
}
www.bajdi.com

I_think


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.

Go Up