Hello! I have been experimenting with a LCD lately and I'm making a text adventure. For my text adventure, I need to know if a button is pressed, so I'm using digitalRead(). Unfortunately, when I put 2 for each button, both buttons give the "You lose" answer. Here is my code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int pinButtonA = 6;
const int pinButtonB = 7;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
pinMode(pinButtonA, INPUT);
pinMode(pinButtonB, INPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("There is a bear. A) Run B) Hit ");
}
void loop() {
int stateButtonA = digitalRead(pinButtonA);
if(stateButtonA == 1) {
lcd.clear();
lcd.print("You got away. YOU WIN!");
} else {
lcd.clear();
lcd.print("There is a bear. A) Run B) Hit ");
}
int stateButtonB = digitalRead(pinButtonB);
if(stateButtonB == 1) {
lcd.clear();
lcd.print("He ate you. YOU LOSE!");
} else {
lcd.clear();
lcd.print("There is a bear. A) Run B) Hit ");
}
delay(500);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
}
Any help would be appreciated. Thanks in advance!
P.S I have no idea of what category this is in so I put programming. Sorry if it isn't a coding issue.
show a shematic how you have connected the buttons and the LCD. A nice drawn circuit on paper and enough real world pictures of your setup is ok for us.
I suspect you haven't studied either how digitalRead() works or how to properly connect a pushbutton to an Arduino pin.
Remember digitalRead() returns either LOW or HIGH based on the voltage on that pin. The simplest way is connect the button to pin and GND, then use INPUT_PULLUP. This means you will always read HIGH value, and LOW when pressed, so reverse the logic and use LOW and HIGH symbols:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int pinButtonA = 6;
const int pinButtonB = 7;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
pinMode(pinButtonA, INPUT_PULLUP);
pinMode(pinButtonB, INPUT_PULLUP);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("There is a bear. A) Run B) Hit ");
}
void loop() {
int stateButtonA = digitalRead(pinButtonA);
if(stateButtonA == LOW) {
lcd.clear();
lcd.print("You got away. YOU WIN!");
} else {
lcd.clear();
lcd.print("There is a bear. A) Run B) Hit ");
}
int stateButtonB = digitalRead(pinButtonB);
if(stateButtonB == LOW) {
lcd.clear();
lcd.print("He ate you. YOU LOSE!");
} else {
lcd.clear();
lcd.print("There is a bear. A) Run B) Hit ");
}
delay(500);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
}
Obviously once you solve this step, you need to extend and complete your program to handle buttons de-bouncing and going on with the adventure interactions (that'll be the most important and hard phase...).
First of all your code is way too complicated for you to be having such basic problems. You are working on 2 buttons when you don't know how to do 1 button.
Start a new sketch. Call it something descriptive like button. Look up examples of codes for buttons and do some research on this forum.
You will find;
the typical hardware is a button with one side to pin, other side to ground and with the code: pinmode input pullup in setup.
You need to detect when a button becomes pushed not when it is pushed. This is called 'edge' or 'state change' detection.
You need to debounce a button otherwise you will read many pushes each time the button is pressed as the microcontroller is fast enough to read the tiny bouncing of the contacts.
Once you have done this you should consider how you might encapsulate your code into a function such as:
void myButtonFunction(){
my button code here;
}
When you have one button working save it again and then hit 'save as' so that you have a second sketch which you can call eg buttons. Now work on incorporating a second button.
A VERY important concept you are missing is the way the loop function works. It loops! It loops fast! study state machines and conditional statements such as if, if/else and switch case. Also study scope and why it matters to protect a variable.
int stateButtonA = digitalRead(pinButtonA);
if(stateButtonA == 1) {
lcd.clear();
lcd.print("You got away. YOU WIN!");
} else {
lcd.clear();
lcd.print("There is a bear. A) Run B) Hit ");
}
The above is some of your code in loop. What do you think happens as the code loops over and over again many times a second?
Complexity is the apparent end result. The process is lots of simple steps. Mash everything together and you have a complex mess. Work iteratively and you have working code just one save back and all the bugs are in the difference between that save and the current sketch you are working on.
After getting rid of else and doing what pmagowan said, I added the code from the button script and it worked like it’s supposed to. Now I’m going to add some more choices. Thankyou