setting multiple buttons using the example debounce sketch

Hi I'm new to the world of Arduino I've gone through tutorials but I'm stuck on how I should declare more than one button switch input and output pin. I would like to ask how I can implement the above and how it should be structured in the sketch. I'm a complete novice but hope to learn a lot more as I go.

So for my project I'm trying to replicate a car indicator system, so one button for left indication and one for right.

I have the buttons and leds setup, Button 1 is on pin 2 and button 2 is on pin 3, Led output on pin 11 and 12.

Thank you.

int inPin = 2; // the number of the input pin
int outPin = 11; // the number of the output pin

int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers

void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(outPin, state);

previous = reading;
}

Hello and welcome,

Basically you have to duplicate the variables and the code that is inside setup() and loop(). Of course you have to change the names of the new variables. You could also use arrays for simplifying your code.

May I suggest a cool little library? GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses

I'm not sure if this is the correct way, but would it go something like this ?

void setup()
{
pinMode(inPin, INPUT);
pinMode(inPin2, INPUT);
pinMode(outPin, OUTPUT);
pinMode(outPin2, OUTPUT);
}

time = millis();

void loop()
{
reading = digitalRead(inPin);
reading = digitalRead(inPin2);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH; }

digitalWrite(outPin, state);
digitalWrite(outPin2, state);

previous = reading;

Not exactly but nice try. You have to copy all variables that are related to one button or one output.

When you do:

reading = digitalRead(inPin);
reading = digitalRead(inPin2);

What do you think will be the value of reading after these two lines of code? Of course, it will be the value of the latest assignment. If you don't understand this, you have to read some tutorials about basic programming.

So you have to duplicate reading as well (and others variables too), and the if block.

Cracked it, thank you so much for your help :slight_smile:

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin

const int buttonPin2 = 3; // the number of the pushbutton pin
const int ledPin2 = 11; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output:
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
}
}

Completed Debounce Sketch

// 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; // the number of the pushbutton pin
const int ledPin2 = 11; // the number of the LED pin

// 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

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
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; // the last time the output pin was toggled
long debounceDelay2 = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin2, OUTPUT);

// set initial LED state
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, ledState2);
}

void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
int reading2 = digitalRead(buttonPin2);

// 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 the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if (reading2 != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if ((millis() - lastDebounceTime2) > debounceDelay2) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
if (reading2 != buttonState2) {
buttonState2 = reading2;

// only toggle the LED if the new button state is HIGH
if (buttonState2 == HIGH) {
ledState2 = !ledState2;
}
}
}

// set the LED:
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, ledState2);

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
lastButtonState2 = reading2;

}

Completed Debounce Sketch

Except it looks silly, numbering only one variable in a set.

The next step is to do the same thing with arrays, instead of numbered (or half-ass numbered) variables.

So for my project I'm trying to replicate a car indicator system, so one button for left indication and one for right.

Which might lead me to chose variable names based on left and right. ie; leftLED rightSwitch. However, I'd do as Paul suggests and use arrays. Whenever you find yourself writing the same code for two different variables you need to think harder.

long debounceDelay = 50;    // the debounce time; increase if the output flickers

This variable didn't need to be duplicated. This is a constant that you can use for both buttons. It should have been prefixed with the const keyword:

const long debounceDelay = 50;

Time-related variables should all be unsigned long, because that's the type millis() returns. So:

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long lastDebounceTime2 = 0;  // the last time the output pin was toggled

const unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

Variables that holds a pin number or a state, doesn't have to be of type int. A pin will never be less than 0 or more than 255, and a state can only have two values, so those variables can fit in a byte (or uint8_t, same thing).

const uint8_t buttonPin = 2;    // the number of the pushbutton pin

This part:

if ((millis() - lastDebounceTime) > debounceDelay) {
  if ((millis() - lastDebounceTime2) > debounceDelay2) {
    ...
  }
}

looks wrong. You want to check buttons separately, but this code will debounce button 2 only if button 1 was debounced (hard to explain because english isn't my language)

This part:

// set the LED:
  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin2, ledState2);

should have been within the if blocks. No need to write the states thousands times per seconds. Just once, when the button was pressed.

Using arrays , for loops, and correct data types will make your code smaller, faster, and easier to read and modify. Try it :wink:

looks wrong. You want to check buttons separately, but this code will debounce button 2 only if button 1 was debounced (hard to explain because english isn't my language)

I know what you mean. He has the handling of his two switches tangled together. It would be better to read and handle one switch then the other, rather than trying to do both at once.

Where I'm from car indicators blink.

Wow thanks for the help on this, and breaking it down with explanation.I'm a complete novice when it comes to coding so I have a lot more reading to do. Blinking was my next task :wink: thank you again for all your help it's very much appreciated.

Hi guys, i just new to arduino and i want to know how to count push button input in 10 seconds. I mean the condition is, if user click multiple time or single time in 10 sec then it will print the input value.

i want to know how to count push button input in 10 seconds

Starting when?

The timer start when the first input of the button is pushed and then it check in 10 sec that if there more input is coming in 10sec or it just only 1 input value of button.

thanks in advance.

Keep a counter; increment it every time a button goes from non-pressed to pressed. After 10 seconds, display the result and clear the counter. Keep in mind that you might need debounce.

Have a look at the state change example that comes with the IDE.

I understand state change example, thanks for help.
But i am still not able to get it that how to implement 10 sec timer and get the button value.
Please its really urgent for me, can you please share the code.
its really appreciated, thanks for help in advance.

But i am still not able to get it that how to implement 10 sec timer and get the button value.

You know that a switch has become pressed. That is the first time that has happened, or it isn't. How can you know that?

Well, if the last time that that happened is 0, you'd know that it was the first time, so you'd set the last time that that happened to now, and you'd set the count to 0.

If not, you just increment the count.

Independently, you see if the last time that happened was not 0. If not, you'd see if it has been more than 10 seconds since then.

If it has, you'd print the number of times the switch was pressed, and set the count to 0.

But, then what? The Arduino will keep running, and the user can keep pressing the switch. What should happen?

Thanks for helping me.
The condition is, i am using esp8266 wifi module with arduino and 4 push button with 1 led.
Now if any user click 1 button then the code will check that is it press only 1 time in 10 sec or is it press many times by any childern in 10 sec. Then if the button is pressed only once in 10 sec then it will 'GET' request to server through esp8266 module.
After 10 sec, again user will able to send the data through button.

#include <SoftwareSerial.h>
SoftwareSerial softSerial(2, 3); // RX, TX

const int  Buzzer =  5; //Buzzer for InputButton
const int INPUTLED = 12; //LED for Input Data
const int  buttonPin1 = 8;    //Excellent
const int  buttonPin2 = 9;    //GOOD
const int  buttonPin3 = 10;   //Poor
const int  buttonPin4 = 11;   //Very Poor

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState1 = 0, buttonState2 = 0, buttonState3 = 0, buttonState4 = 0;      // current state of the button

int buttonSelectedState = 0;

void setup()
{
  uint32_t baud = 115200;
  Serial.begin(115200);
  softSerial.begin(115200);

  pinMode(INPUTLED, OUTPUT);
  digitalWrite(INPUTLED, LOW);
  digitalWrite(Buzzer, LOW);
  pinMode(Buzzer, OUTPUT);

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
 
}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  int numSelected = 0;
  if (buttonState1 == HIGH)
    numSelected = 1;
  else if (buttonState2 == HIGH)
    numSelected = 2;
  else if (buttonState3 == HIGH)
    numSelected = 3;
  else if (buttonState4 == HIGH )
    numSelected = 4;

  if (numSelected == 0)
    buttonSelectedState = 0;
  else {
    if (numSelected != buttonSelectedState) {

      buttonSelectedState = numSelected;
      if (numSelected != 0)
    
      updateTS(buttonSelectedState, 0);
      Serial.println(buttonSelectedState);
      delay(2000);
    }
  }

}

void updateTS(int input_button, int tryCount)
{
  coding.....data send to server
}

You could use a TButton and a TTimer from TDuino to do the 10 seconds thing:

#include <TDuino.h>

#define BUTTON_PIN 4

void timerCallback(byte timerId); //Prototype

TButton button;
TTimer timer(timerCallback);

void timerCallback(byte timerId);
{
  //This is triggered when the button has been pressed for 10 seconds
  digitalWrite(LED_BUILTIN, HIGH);
}

void buttonPress(byte buttonPin, int state)
{
  //Set the timer to trigger once in 10 seconds
  timer.set(10000, 1);
}

void buttonRelease(byte buttonPin, int state)
{
  //Stop the timer
  timer.stop();
  digitalWrite(LED_BUILTIN, LOW);
}

void setup()
{
  //Setup the button
  button.attach(BUTTON_PIN);
  button.onPress(buttonPress);
  button.onRelease(buttonRelease);
}

void loop()
{
  button.loop();
  timer.loop();
}

It is quite easy to modify the code to handle multiple buttons :slight_smile: