I've been working on a project in school and I've ran into an issue.
This project includes 4 buttons, an LCD screen, and a servo motor. The idea is that when button 1 is pressed, the number 1 will be displayed. As follows with 2, 3, and 4. Then, if the correct code is entered, a servo motor will turn, to unlock the door.
So far, my lcd screen is set up, as well as my four buttons. The code is written just to display numbers when the buttons are typed. However, when I upload the code, 'Hello.' is printed and cleared after 2 seconds as planned, but then '1234' immediately is displayed afterwards.
#include <LiquidCrystal.h>
int buttonPin1= 6; //sets pins buttons are connected to
int buttonPin2= 7;
int buttonPin3= 8;
int buttonPin4= 9;
const int buttonState1 = 0;
const int buttonState2 = 0;
const int buttonState3 = 0;
const int buttonState4 = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //sets pins lcd is connected to, running in 4 bit mode, RW pin tied to gnd
void setup() {
pinMode(buttonPin1, INPUT); //assigns button pins as inputs
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
lcd.begin(16, 2); //screen size
lcd.print(" Hello."); //introduction
delay(2000); //delay
lcd.clear(); //clears screen
}
void loop() {
int buttonState1 = digitalRead(buttonPin1); //reads buttons
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int buttonState4 = digitalRead(buttonPin4);
lcd.setCursor(0, 0);
if(buttonState1 == HIGH) {
lcd.print("1"); //if button 1 is pressed, print 1.
delay(250);
}
if(buttonState2 == HIGH) {
lcd.print("2"); //if button 2 is pressed, print 2.
delay(250);
}
if(buttonState3 == HIGH) {
lcd.print("3"); //if button 3 is pressed, print 3.
delay(250);
}
if(buttonState4 == HIGH) {
lcd.print("4"); //if button 4 is pressed, print 4.
delay(350);
}
}
Why is that the outcome? Is it a coding error, or a circuit error?
Each button is connected to power, and on the other end, it is connected to ground, and their repective arduino pins. Any help would be greatly appreciated!