Arduino Forum

Using Arduino => Introductory Tutorials => Topic started by: IoT_hobbyist on Jan 06, 2020, 08:02 am

Title: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Jan 06, 2020, 08:02 am
Beginners usually run into the following troubles:


With the ezButton library (https://github.com/ArduinoGetStarted/button), the beginners do NOT need to worry above problems.

ezButton features:


Installation Instruction and Library Reference (https://arduinogetstarted.com/tutorials/arduino-button-library)


Example code:
Multiple Buttons: debouncing + state + counting + detecting the pressing and releasing events

Code: see how to post code (https://arduinogetstarted.com/tools/arduino-code-highlighter)
#include <ezButton.h>

ezButton button1(6);  // create Button object that attach to pin 6;
ezButton button2(7);  // create Button object that attach to pin 7;

void setup() {
   Serial.begin(9600);
   button1.setDebounceTime(50); // set debounce time to 50 milliseconds
   button2.setDebounceTime(50); // set debounce time to 50 milliseconds
   button1.setCountMode(COUNT_FALLING);
   button2.setCountMode(COUNT_FALLING);
}

void loop() {
   button1.loop(); // MUST call the loop() function first
   button2.loop(); // MUST call the loop() function first

   int btn1State = button1.getState();
   int btn2State = button2.getState();
   Serial.print("button 1 state: ");
   Serial.println(btn1State);
   Serial.print("button 2 state: ");
   Serial.println(btn2State);

   unsigned long btn1Count = button1.getCount();
   unsigned long btn2Count = button2.getCount();
   Serial.print("button 1 count: ");
   Serial.println(btn1Count);
   Serial.print("button 2 count: ");
   Serial.println(btn2Count);

   if (button1.isPressed())
       Serial.println("The button 1 is pressed");

   if (button1.isReleased())
       Serial.println("The button 1 is released");

   if (button2.isPressed())
       Serial.println("The button 2 is pressed");

   if (button2.isReleased())
       Serial.println("The button 2 is released");
}


When using many buttons, we can puts buttons into an array

Example of 5 buttons using array.
Code: see how to post code (https://arduinogetstarted.com/tools/arduino-code-highlighter)
#include <ezButton.h>

const int BUTTON_NUM = 5;

const int BUTTON_1_PIN = 2;
const int BUTTON_2_PIN = 3;
const int BUTTON_3_PIN = 4;
const int BUTTON_4_PIN = 5;
const int BUTTON_5_PIN = 6;

ezButton buttonArray[] = {
  ezButton(BUTTON_1_PIN),
  ezButton(BUTTON_2_PIN),
  ezButton(BUTTON_3_PIN),
  ezButton(BUTTON_4_PIN),
  ezButton(BUTTON_5_PIN)
};

void setup() {
  Serial.begin(9600);

  for (byte i = 0; i < BUTTON_NUM; i++) {
    buttonArray.setDebounceTime(50); // set debounce time to 50 milliseconds
  }
}

void loop() {
  for (byte i = 0; i < BUTTON_NUM; i++)
    buttonArray.loop(); // MUST call the loop() function first

  for (byte i = 0; i < BUTTON_NUM; i++) {
    if (buttonArray.isPressed()) {
      Serial.print("The button ");
      Serial.print(i + 1);
      Serial.println(" is pressed");
    }

    if (buttonArray.isReleased()) {
      Serial.print("The button ");
      Serial.print(i + 1);
      Serial.println(" is released");
    }
  }
}


The library is also useful in the case of using a single button.

If you have any comment or suggestion, please feel free to comment here.
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: RDF_MIDI on Mar 01, 2020, 05:20 am
I've seen several tutorials on how to read a group of switches, and output something that indicates which switch has been pushed.

I'm trying to write a sketch that will allow me to read 3 switches as a binary value of 0-7, and output different text to an LCD for each of the 8 results.

Is there a tutorial for such a project?
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Mar 01, 2020, 08:25 am
It can be as simple as this code:

Code: [Select]
btn1State = button1.getState();
btn2State = button2.getState();
btn3State = button3.getState();

binaryState = btn1State | (btn2State << 1) | (btn3State << 2)
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Mar 01, 2020, 12:07 pm
Code: [Select]
binaryState = btn1State | (btn2State << 1) | (btn3State << 2)

This line might be more intuitive for the OP:

Code: [Select]
theNumber = (1 * digitalRead(lowPin)) + (2 * digitalRead(midPin)) + (4 * digitalRead(highPin));


Great! Thank you for the additional tip.

Since beginners may get into trouble because of the floating input and chattering issue. I recommend using the library instead of digitalRead() function. The library used the internal pull-up resistor to avoid the floating value and implemented the debouncing to eliminate the chattering phenomenon.

Your code can be modified to:
Code: [Select]

btn1State = button1.getState();
btn2State = button2.getState();
btn3State = button3.getState();

binaryState = (1* btn1State)  +  (2 * btn2State) + (4 * btn3State)



Title: Re: For beginners: The simple way to program for multiple buttons
Post by: RDF_MIDI on Mar 02, 2020, 02:55 am
Great! Thank you for the additional tip.

Since beginners may get into trouble because of the floating input and chattering issue. I recommend using the library instead of digitalRead() function. The library used the internal pull-up resistor to avoid the floating value and implemented the debouncing to eliminate the chattering phenomenon.

Your code can be modified to:
Code: [Select]

btn1State = button1.getState();
btn2State = button2.getState();
btn3State = button3.getState();

binaryState = (1* btn1State)  +  (2 * btn2State) + (4 * btn3State)



Thank you so much for the reply!

As a newb to programming, this is a great explanation of reading the switches as a group, rather than individual switches. That's been a major stumbling block to this project so far.

I've been able to find the setup (using the buttons library and the above explanation), and display the result (using the "case" reference and experimenting with null inputs for the LCD), but the gap between the two is how to read (you recommended not using the digitalRead function?) and manage the data. I'm kinda stumped at this point.

I hate to sound like I'm asking the experts to solve my equation for me. I'd really like to figure it out myself. What I am asking the experts is if they can point me to the reference to a statement or group of statements that I can research to figure out the answer.

Thanks again!

Rob
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Mar 02, 2020, 03:24 am
@RDF_MIDI, you can figure it out by yourself by referring to these tutorials:

Title: Re: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Apr 08, 2020, 01:06 am
Thank you for this information for the new members in the forum, I am also a new member in this forum.
You are welcome!
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: jackthomson42 on May 04, 2020, 07:44 am
Hmmm nice info, button debouncing has always been a headache for me. I will check out your code.
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: josh54 on Jun 23, 2020, 02:27 pm
Thank you, nice tips :)
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: micwall on Jul 14, 2020, 03:40 pm
I was just looking for something to simplify button reading, and this looks like it would be great! I will look into this!
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: canguroso on Jul 21, 2020, 11:07 am
This is great mate! but im currently in some odd problem, when i list more than 10 buttons it gives me compiling errors...

What (else) im doing wrong? jaja
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Jul 22, 2020, 01:39 am
This is great mate! but im currently in some odd problem, when i list more than 10 buttons it gives me compiling errors...

What (else) im doing wrong? jaja
What was shown in error log?
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Aug 27, 2020, 03:13 pm
I liked the detailed explanation, thank you very much for that and respect!
I am very happy to hear that.
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: louise-t on Sep 10, 2020, 10:50 am
Hello
Thanks for posting so good for new members.
Title: Re: For beginners: The simple way to program for multiple buttons
Post by: IoT_hobbyist on Sep 11, 2020, 01:34 am
Hello
Thanks for posting so good for new members.
I am happy to hear that. Thank you for your feedback.