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?
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);
  }
 }
}
E40racer:
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?
Hmm, of course I want to debounce the push buttons all the time
I started of with this 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);
  }
 }
}
Ok, I've deleted the while(1). And I've now come up with this. The code compiles and runs fine.
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);
 }
}
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.