Hello,
I am working on an electronic lock project, with four buttons to enter a passcode, as well as an enter button, and a clear button.
I have this code where if a button is pressed, the corresponding number will display on the screen, and I also have a button that will clear the numbers in case somebody typed a wrong number in.
#include <LiquidCrystal.h>
int buttonPin1 = 2; //sets pins buttons are connected to
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int enterbutton = 10; // not in use yet
int clearlockbutton = 13;
LiquidCrystal lcd(12, 11, 9, 8, 7, 6); //sets pins lcd is connected to, running in 4 bit mode, RW pin tied to gnd
void setup() {
pinMode(buttonPin1, INPUT_PULLUP); //assigns button pins as inputs
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(enterbutton, INPUT_PULLUP);
pinMode(clearlockbutton, INPUT_PULLUP);
lcd.begin(16, 2); //screen size
lcd.setCursor(0, 0);
lcd.print("Hello."); //introduction
delay(2000); //delay
lcd.clear(); //clears screen
lcd.print("Password:");
}
void loop() {
int buttonState1 = digitalRead(buttonPin1); //reads buttons
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int buttonState4 = digitalRead(buttonPin4);
int clButtonState = digitalRead(clearlockbutton);
int enterButtonState = digitalRead(enterbutton);
if (buttonState1 == LOW) {
lcd.print("1"); //if button 1 is pressed, print 1.
delay(250);
}
else if (buttonState2 == LOW) {
lcd.print("2"); //if button 2 is pressed, print 2.
delay(250);
}
else if (buttonState3 == LOW) {
lcd.print("3"); //if button 3 is pressed, print 3.
delay(250);
}
else if (buttonState4 == LOW) {
lcd.print("4"); //if button 4 is pressed, print 4.
delay(350);
}
if (clButtonState == LOW) {
lcd.clear();
lcd.print("Password:");
lcd.print(" ");
delay(1000);
lcd.clear();
lcd.print("Password:");
// **INSERT LOCKING CODE HERE**
}
}
Then, I started experimenting on how to store the button values, so if the correct password was typed in, the unlocking mechanism would go to work.
#include <LiquidCrystal.h>
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int enterbutton = 10;
int clearlockbutton = 13;
const int password = 10;
int p1 = 0;
int p2 = 0;
int p3 = 0;
int p4 = 0;
LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
void setup() {
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(enterbutton, INPUT_PULLUP);
pinMode(clearlockbutton, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Hello.");
delay(2000);
lcd.clear();
lcd.print("Password:");
}
void loop() {
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int buttonState4 = digitalRead(buttonPin4);
int clButtonState = digitalRead(clearlockbutton);
int enterButtonState = digitalRead(enterbutton);
if (buttonState1 == LOW) {
lcd.print("1");
delay(250);
int p1 = 1;
}
if (buttonState2 == LOW) {
lcd.print("2");
delay(250);
digitalRead(p1);
if (p1 == 1) {
int p2 = 2;
}
}
if (buttonState3 == LOW) {
lcd.print("3");
delay(250);
digitalRead(p1);
digitalRead(p2);
if (p1 == 1) {
if (p2 == 2) {
int p3 = 3;
}
}
}
if (buttonState4 == LOW) {
lcd.print("4");
delay(250);
digitalRead(p1);
digitalRead(p2);
digitalRead(p3);
if (p1 == 1) {
if (p2 == 2) {
if (p3 == 3) {
int p4 = 4;
}
}
}
}
digitalRead(p1);
digitalRead(p2);
digitalRead(p3);
digitalRead(p4);
if (p1 == 1,
p2 == 2,
p3 == 3,
p4 == 4) {
if(enterButtonState == LOW) {
lcd.clear();
lcd.print("Correct!");
delay(1000);
lcd.print("Password:");
}
}
if(clButtonState == LOW) {
lcd.clear();
lcd.print("Password:");
lcd.print(" ");
delay(1000);
lcd.clear();
lcd.print("Password:");
// **INSERT LOCKING CODE HERE** -- when this button is pressed, it will also lock the door if it is unlocked
}
}
Of course, I haven't added the servo motor yet, but I did have it set to say "correct" if the password was correct. (currently it is '1234'), however nothing happens when I click the 'enter' button. The 1, 2, 3, 4, and clear button functions fine.
Also, here is a photo of my circuit as of now:
What am I doing wrong here? Is it a small coding error, or did I just do this completely wrong? All help would be appreciated. thanks!