Go Down

Topic: Reading Multiple Buttons (Read 10936 times) previous topic - next topic


Hi everyone.

I'm very new to this stuff so please be gentle with me.

I am trying to read the state of two buttons, each on their own pin. I then want to display a short message on an LCD, saying which button has been pressed. I know this is very basic stuff but, i'm just trying to learn how things work at the moment. I managed this straight away with one button by cobbling together bits of code from various sketches but, it doesn't seem to work for two buttons.

My code is as follows:
Code: [Select]


 The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* Pushbutton 1 to +5V & pin 7 ( also a 10K resistor to ground)
* Pushbutton 2 to +5V & pin 8 ( also a 10K resistor to ground)


// include the library code:
#include <LiquidCrystal.h>
const int buttonPin1 = 7;     // the number of the pushbutton pin
const int buttonPin2 = 8;     // the number of the pushbutton pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton1 status
int buttonState2 = 0;         // variable for reading the pushbutton2 status

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {


void loop() {
 lcd.clear();  // Clear the LCD
 // check if the pushbutton is pressed.  
 buttonState1 = digitalRead(buttonPin1);  // read the state of the pushbutton1 value
 buttonState2 = digitalRead(buttonPin2);  // read the state of the pushbutton2 value
// if it is, the buttonState is HIGH:
 if (buttonState1 == HIGH) {     // If button 1 is high  
   lcd.setCursor(0, 0);  // Set the cursor
 lcd.print("BUTTON 1 ON");  // Display the message
 if (buttonState2 == HIGH) {     // If button 2 is high  
   lcd.setCursor(0, 0);  // Set the cursor
 lcd.print("BUTTON 2 ON");  // Display the message
 else {
   lcd.setCursor(0, 0);  // Set the cursor
 lcd.print("ALL BUTTONS OFF");  // Display the message
delay(500);  // Wait 0.5 seconds before re-checking button states

My wiring seems ok but, when I have two buttons in the code only button 2 seems to register.

Thanks in advance for any help.
I don't like to be lazy and I have tried searching for the answer but so far, I have been unlucky.

Thanks again.


You have an if/if/else construct, rather than an if/else if/else construct. The second if statement, that test button 2, should be an else if statement.

You are not using the internal pull-up resistors, so, may we presume that you have external pull-down resistors?
The art of getting good answers lies in asking good questions.


Thank you for the reply.

Yes, as is says in the circuit information at the top of the code, I am using 10K pull-down resistors.

I think I understand what you mean about the IF/ELSE IF/ELSE structure, but i'm not sure how to fix it. I tried to copy the ELSE statement and paste it after the first IF statement but it didn't change anything.

Thanks again for the help so far, much appreciated.



Try swapping the wires to pins 7 and 8. If the problem changes pins, the switches are wired incorrectly. If the problem doesn't follow the pins, it's a coding issue, although I don't see anything (else) wrong with the code.
The art of getting good answers lies in asking good questions.

Go Up