Im have been trying to figure out why this error happens. I am working with tinkercad arudino IDE, which includes all the libraries.
Sorry for this long code.
#include <Servo.h>
#include <Keypad.h>
#include <Adafruit_LiquidCrystal.h>
//Display:
Adafruit_LiquidCrystal lcd_1(0);
//Servo:
Servo vaultservo;
int pos = 0;
//4x4 Keypad: *Quelle
const byte COLS = 4;
const byte ROWS = 4;
char hexaKeys[ROWS][COLS]={
{'A','3','2','1'},
{'B','6','5','4'},
{'C','9','8','7'},
{'D','#','0','*'}
};
byte rowPins[ROWS] = {13,12,11,10};//Definition der Pins für die 4 Zeilen
byte colPins[COLS] = {6,7,8,9}; //Definition der Pins für die 4 Spalten
char key; //Taste ist die Variable für die jeweils gedrückte Taste.
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld" angesprochen werden
//password:
char savedpassword[4] = {'0','0','0','0'};
bool boolSavedPassword = false;
int passwordIndex = 0;
bool open = false;
//LED:
int LEDgreen = 3;
int LEDblue = 4;
int LEDred = 5;
void setup()
{
vaultservo.attach(0);
lcd_1.begin(16, 2);
lcd_1.setBacklight(1);
pinMode(LEDgreen, OUTPUT);
pinMode(LEDblue, OUTPUT);
pinMode(LEDred, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop()
{
LED();
servo();
key = keypad.getKey(); //key entspricht der gedrückten Taste
if(key){ //sollte ein Knopf gedrückt werden
checkKey(key);
}
}
void checkKey(char key){
if (!boolSavedPassword && checkForNumber() ){
Serial.println("Bitte drücken sie '*' um ein Passwort zu speichern");
lcdPrint2("Press '*' to", "save password.");
} else if (boolSavedPassword && key == '#' && !open){
enterCode();
} else if (key == 'A' || key == 'B' || key == 'C' || key == 'D'){
lcdPrint2("Button has no", "meaning.");
} else if (key == '*' && !boolSavedPassword){
savePassword();
} else if (key == '*' && boolSavedPassword){
resetPassword();
} else {
Serial.println("Fehler");
}
}
void savePassword(){
lcdPrint2("Enter 4 digits", "to save password");
while (!boolSavedPassword && passwordIndex <= 3){
key = keypad.getKey();
if (passwordIndex == 3){
boolSavedPassword = true;
}
if (key && checkForNumber){
savedpassword[passwordIndex] = key;
passwordIndex++;
} else {
lcdPrint2("Error, please", "enter digits.");
}
}
passwordIndex = 0;
Serial.println(savedpassword);
lcdPrint2("New password", "now is " + String(savedpassword));
delay(400);
lcdPrint2("Press '#' to", "enter code.");
}
void resetPassword(){
if (open){
key = keypad.getKey();
lcdPrint2("Confirm: '*'","Cancel with any.");
if (key == '*'){
savePassword();
} else if (key){
lcdPrint2("Cancelled", "reset.");
delay(400);
lcdPrint2("Close vault:'#'", "Reset vault:'*'");
}
} else {
lcdPrint2("To reset code", "first open safe.");
delay(300);
lcdPrint2("Press '#' to", "enter code.");
}
}
void enterCode(){
char trypassword[4] = {'0','0','0','0'};
String counter;
bool samePassword = false;
while (passwordIndex <= 3){
counter = "";
key = keypad.getKey();
for(int i=0; i < passwordIndex; i++){
counter += "*";
} lcdPrint2("counter", "nothing");
trypassword[passwordIndex] = key;
passwordIndex +=1;
}
if (checkPassword(trypassword, savedpassword) == true){
lcdPrint2("Correct code", "safe is now open");
delay(400);
lcdPrint2("Close vault:'#'", "Reset vault:'*'");
} else {
lcdPrint2("Wrong code", "try again.");
}
passwordIndex = 0;
}
bool checkForNumber(){
if (key == '1' ||key == '2' ||key == '3' ||key == '4' ||key == '5' ||key == '6' ||key == '7' ||key == '8' ||key == '9'){
return true;
} else {
return false;
}
}
bool checkPassword(char *tryPassw, char *password){
bool samePassword = true;
int index = 0;
while(index <= 3 && samePassword == true){
if (tryPassw[index] != password[index]){
samePassword = false;
}
index++;
}
return samePassword;
}
void LED(){ //schaltet den LED auf grün wenn der Tresor geöffnet ist, ansonsten auf rot wenn der Tresor geschlossen ist.
if (open){
analogWrite(LEDgreen, 200);
} else {
analogWrite(LEDred, 200);
}
}
void servo(){
if (open){
for (pos = 0; pos <= 180; pos += 1) {
vaultservo.write(pos);
//delay(15);
}
} else {
for (pos = 90; pos >= 0; pos -= 1) {
vaultservo.write(pos);
//delay(15);
}
}
}
void lcdPrint(int row, String printString){
if (printString.length() <= 16){
if (row == 1){
lcd_1.setCursor(0, 0);
lcd_1.print(printString);
} else if (row == 2){
lcd_1.setCursor(0, 1);
lcd_1.print(printString);
} else {
Serial.println("Nicht die 1 oder 2 Zeile");
}
} else {
Serial.println("String ist zu lang");
}
}
void lcdPrint2(String row1String, String row2String){
lcd_1.clear();
lcdPrint(1, row1String);
lcdPrint(2, row2String);
}