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:
/*
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.
Colaboy