I am working on a school project, basically I have to create a lock system that opens a fictive gate when the correct code is entered. We have been asked to simulate our system before actually building it. So, I made the following circuit in Proteus(Labcenter Electronics' simulation software): Keypad circuit
Sorry, I can't post images.
Here's what my system should do: A variable contains the correct code and the user must type a code on the keypad, and if it is correct, a green LED turns on and the LCD screen displays "Acces Granted!" if the code is wrong, the screen will display "Access refused!". It's a basic system, but I'm trying to make it as simple/short as possible(I have to explain it in an oral presentation, so the simpler the better) and for some reason, I can't get my code to work. Ideally, I'd like to have the LCD display a * for each character typed, liked on a real security system, but I haven't managed to do that either. I've been working on it for hours, trying different variants and etc.. but nothing seems to work the way I want to. Btw, my circuit works perfectly fine and the initialisation part of my code is therefore correct aswell. Can anyone help me figure out what's wrong with my code and how I can make it better?
Here's my code so far:
#include <LiquidCrystal.h>
#include <Keypad.h>
//define LED pins
#define redLED 11
#define greenLED 10
String codeSerrure = "87362"; //correct code that opens the imaginative gate
String enteredCode = "";
int keyPressed;
const byte rows = 4;
const byte cols = 3;
char touches_digicode [rows] [cols] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins [rows] = {25, 26, 27, 28};
byte colPins [cols] = {24, 23, 22};
Keypad leDigicode = Keypad( makeKeymap(touches_digicode), rowPins, colPins, rows, cols);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup(){
lcd.begin(16, 2);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
digitalWrite(redLED, HIGH);
}
void loop(){
lcd.setCursor(0,0);
lcd.print(" Entrez le code");
keyPressed = leDigicode.getKey();
enteredCode += String(keyPressed);
if(enteredCode.length() >= 5){
if(enteredCode == codeSerrure){
digitalWrite(greenLED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Acces autorise!");
delay(4000);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
enteredCode = "";
}
else{
digitalWrite(redLED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Acces refuse!");
delay(4000);
enteredCode = "";
}
}
if(keyPressed == "#"){
lcd.clear();
enteredCode = "";
}
}
The problem is that when I run this code in the simulation software, the LCD displays "Acces refuse!" and the red LED is on. No matter what keys I press (on the keypad) nothing happens. So the problem is that my code jumps straight to the else statement, infering that the password typed is wrong (although no password was actually typed). I think the error is in here:
keyPressed = leDigicode.getKey();
enteredCode += String(keyPressed);
if(enteredCode.length() >= 5){
if(enteredCode == codeSerrure){
Btw, forgive the french words, I am french. Also, I forgot to say, the # key should, clear the entered code. Any explainations, code samples or links would help. Thank you!