PaulS:
int n1btn, n2btn, n3btn, n4btn, setbtn, clsbtn; //Variablen für Buttons deklarieren
int ln1btn, ln2btn, ln3btn, ln4btn; //Variablen für last Button State
Arrays are your friends, not you enemies.while(n1btn==LOW && n2btn==LOW && n3btn==LOW && n4btn==LOW && setbtn==LOW && clsbtn==LOW){
lcd.setCursor(0,5);
lcd.print("Hello");
lcd.setCursor(1,1);
lcd.print("Press any key");
}n1btn, n2btn, n3btn, n4btn, setbtn, and clsbtn are not bound to the digitalRead() function. If all of the switches are not pressed when the Arduino boots up, you will enter this while loop with no way to exit it. How ARE your switches wired? You are not using the internal pullup resistors, so you must have external resistors. How are they wired?while(n1btn==LOW && n2btn==LOW && n3btn==LOW && n4btn==LOW){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set password");
}Another infinite loop with no way to exit. You need to scrap this code, and start over. Treat each switch COMPLETELY independently. There are two parts to your project - setting the initial code and entering the code to re-open the door. You need to look at the state change detection example, to learn how to detect that a switch HAS BECOME, not IS, pressed. When you are in the "Enter a new door lock code" mode, each time a switch has become pressed, you record the number of the switch in an array and increment the index into the array. When the 4th switch has been pressed, you exit the "Enter a new door lock code" and enter the "Guess the door combination" mode. In that mode, each time a switch has become pressed, you record the number of the switch in a different array. When the 4th switch has been pressed, you compare the contents of the two arrays, and open the door, or not, depending on whether the values match, or not. An oops key and a "shit, I want to start over" key are good things to have. And to handle.
Hey Paul,
thanks for your answer!
I just really didn't see, that you cannot exit the while loops, so I updated that. I also added an "codeindex" integer to count the digits to exit the "Set Code" after four digits are entered.
But can you explain me how you would do it and paste an example code? I'm just a newbie and I don't know what an array is.
And to clarify a bit: I've got the SET and CLEAR buttons (setbtn & clsbtn in the code). If you press the CLEAR button while setting the password, it should go back to the beginning and delete the WHOLE password, not just the last digit that was entered.
I think, if I got it how to set a password and read it to "unlock" the safe, I'm able to do it on my own with the different states.
And as it's a school project, I also got a state diagram and attached it as a png (unfortunatly, its commented in german).
And finally, here's the whole code:
#include <LiquidCrystal.h> //Bibliothek für das Display inkludieren
LiquidCrystal lcd(12,11,5,4,3,2); //Pins des Displays einlesen
int n1btn, n2btn, n3btn, n4btn, setbtn, clsbtn; //Variablen für Buttons deklarieren
int ln1btn, ln2btn, ln3btn, ln4btn; //Variablen für last Button State
int codeset = 0; //Variable zur Überprüfung, ob das Gerät zum ersten Mal gestartet wird
int password, codeindex;
void setup() {
lcd.begin(16,2); //Pins des Displays konfigurieren
pinMode(0,INPUT); //Pins der Buttons konfigurieren
pinMode(1,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(9,INPUT);
ln1btn = n1btn;
ln2btn = n2btn;
ln3btn = n3btn;
ln4btn = n4btn;
codeindex = 1;
}
void loop() {
n1btn = digitalRead(6); //Buttons einlesen in die Variablen
n2btn = digitalRead(7);
n3btn = digitalRead(8);
n4btn = digitalRead(9);
clsbtn = digitalRead(0);
setbtn = digitalRead(1);
if(codeset==0){ //Falls das Gerät zum ersten Mal gestartet wird, folgendes ausführen
while(n1btn==LOW && n2btn==LOW && n3btn==LOW && n4btn==LOW && setbtn==LOW && clsbtn==LOW){
n1btn = digitalRead(6); //Buttons einlesen in die Variablen
n2btn = digitalRead(7);
n3btn = digitalRead(8);
n4btn = digitalRead(9);
clsbtn = digitalRead(0);
setbtn = digitalRead(1);
lcd.setCursor(0,5);
lcd.print("Hello");
lcd.setCursor(1,1);
lcd.print("Press any key");
}
while(n1btn==LOW && n2btn==LOW && n3btn==LOW && n4btn==LOW){
n1btn = digitalRead(6); //Buttons einlesen in die Variablen
n2btn = digitalRead(7);
n3btn = digitalRead(8);
n4btn = digitalRead(9);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set password");
}
if(n1btn==LOW){
//How do i set a code in here?
}
codeset = 1; //Nach dem ersten Start anpassen
}else{
}
}
Greetings,
Sandro
