This tutorial will help you feeling easier with multiple buttons with Arduino. With the old school coding, the button is a very small problem for us but multiple buttons at the same time are hurt to almost everybody. 

Basic of using Button with Arduino

Here we should review the basics of how to use the button correctly with Arduino or other micro-controller. There are two ways to make the button circuit with a micro-controller, first is pulldown, and the second is pull-up which is most popular.
- pull-down resistor pulls the output to low and goes to high when the switch or button is activated.
- pull-up resistor pulls the output to high and goes to low when the switch or button is activated.

Note: A pull-up resistor effectively establishes an additional loop over the critical components, ensuring that the voltage is well-defined even when the switch is open.

Simple button Library


This library was developed for personal use but it works really good for a lot of projects and the developer decided to share it with you.

Overview 

The library doesn't integrate with interrupt events but included debouncing input single and useful functions. You can use either pull-down or pull-up buttons while mixing them together. 

To include the library: 
#inclue <button.h>

To create a new button:
button Button1(2, HIGH); //pull-up button

To initiate the button:
void setup(){
  Button1.init();
}

To detect when the button is pushed:
bool push1 = Button1.push();

To detect the button is pressed:
bool press1 = Button1.press();

To detect the button is released:
bool release1 = Button1.release();

To know how long the button is being held:
long hold1 = Button1.onHold();

For more detail, you can read in Github.

Example


We are going to test the library with 4 separate button mixing of analog/digital pins and pull-down/pull-up resistors.

- Button 1: use a pull-down resistor and connected to digital pin D2.
- Button 2: use a pull-up resistor and connected to digital pin D3.
- Button 3: use a pull-up resistor and connected to analog pin A1.
- Button 4: use a pull-up resistor and connected to analog pin A2.
- LED: connected to digital pin D5.

Starting the code:

#include <button.h> #define LED 5 button Button1(2, LOW); //Pull to low (pull-down) button Button2(3, HIGH); //Pull to high (pull-up) button Button3(A1, HIGH); //Pull to high (pull-up) button Button4(A2, HIGH); //Pull to hgih (pull-up) void setup() { Serial.begin(9600); Button1.init(); Button2.init(); Button3.init(); Button4.init(); pinMode(LED, OUTPUT); } void loop() { /* LED ON for 1s after any button is pushed */ if(Button1.push() || Button2.push() || Button3.push()|| Button4.push()){ digitalWrite(LED, HIGH); delay(1000); digitalWrite(LED, LOW); delay(100); }else digitalWrite(LED, LOW); /* LED ON for 2s after any button is released */ if(Button1.release() || Button2.release() || Button3.release()|| Button4.release()){ digitalWrite(LED, HIGH); delay(2000); digitalWrite(LED, LOW); delay(100); }else digitalWrite(LED, LOW); Serial.println("button1 hold: "+(String)Button1.onHold()); Serial.println("button2 hold: "+(String)Button2.onHold()); Serial.println("button3 hold: "+(String)Button3.onHold()); Serial.println("button4 hold: "+(String)Button4.onHold()); }