Found a script on git for two button modify it for three buttons. Also, with some trail and error guessing were to place the curly brackets. It's working now but not sure about the logic and flow.
It seams that the first "if" statement when its true, the buttons 1,2, and 3 are checked.
The second "if statement statement when true, checks buttons 1,2.
When the third is true, button 1 is checked.
This seems odd to me. Is my logic correct? I am unsure about the flow of the program.
if ((millis() - lastDebounceTime) > debounceDelay) { //Delay has debounce, so take current as actual
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if ((millis() - lastDebounceTime3) > debounceDelay3) {
if (reading != buttonState) // if the button state has changed:
{
buttonState = reading;
if (buttonState == HIGH) // only toggle the LED if the new button state is High
{
ledState = !ledState;
}} }
if (reading2 != buttonState2)
{
buttonState2 = reading2;
if (buttonState2 == HIGH)
{
ledState2 = !ledState2;
}} }
if (reading3 != buttonState3)
{
buttonState3 = reading3;
if (buttonState3 == HIGH)
{
ledState3 = !ledState3;
}} }
// set the LED:
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, ledState2);
digitalWrite(ledPin3, ledState3);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
lastButtonState2 = reading2;
lastButtonState3 = reading3;
}
You only need a single debounce interval for all the buttons - however many there are. The purpose of the debounce interval is simply to allow time for any switch bounce to dissipate. 50 millisecs is usually enough.
Your indenting is really random - makes the program logic hard to read. Move each '}' to a separate line, don't put more than one together. Use ctrl-T in the IDE to auto format it.
Thank You, MacOS is command-T. Then is there another way to make compact and efficient program?
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin
const int buttonPin2 = 3;
const int ledPin2 = 11;
const int buttonPin3 = 4;
const int ledPin3 = 10;
// Variables will change:
int ledState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int ledState2 = LOW; // the current state of the output pin
int buttonState2; // the current reading from the input pin
int lastButtonState2 = LOW; // the previous reading from the input pin
int ledState3 = LOW;
int buttonState3;
int lastButtonState3;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
long lastDebounceTime2 = 0;
long debounceDelay2 = 50;
long lastDebounceTime3 = 0;
long debounceDelay3 = 50;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin3, INPUT);
pinMode(ledPin3, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, ledState2);
digitalWrite(ledPin3, ledState3);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
int reading2 = digitalRead(buttonPin2);
int reading3 = digitalRead(buttonPin3);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
if (reading != lastButtonState)
{
lastDebounceTime = millis(); // reset the debouncing timer
}
if (reading2 != lastButtonState2)
{
lastDebounceTime2 = millis(); // reset the debouncing timer
}
if (reading3 != lastButtonState3)
{
lastDebounceTime3 = millis(); //reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) { //Delay has debounce, so take current as actual
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if ((millis() - lastDebounceTime3) > debounceDelay3) {
if (reading != buttonState) // if the button state has changed:
{
buttonState = reading;
if (buttonState == HIGH) // only toggle the LED if the new button state is High
{
ledState = !ledState;
}
}
}
if (reading2 != buttonState2)
{
buttonState2 = reading2;
if (buttonState2 == HIGH)
{
ledState2 = !ledState2;
}
}
}
if (reading3 != buttonState3)
{
buttonState3 = reading3;
if (buttonState3 == HIGH)
{
ledState3 = !ledState3;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, ledState2);
digitalWrite(ledPin3, ledState3);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
lastButtonState2 = reading2;
lastButtonState3 = reading3;
}/[CODE]