I am creating a program for Arduino Uno. However, the code does not work. I tried to piece together the code from various instructions. I am a complete beginner. Please, give me an advice.
password = "12345 or 123A589 or 1593574682... etc.) // password entered by the user with any number of characters. But at least 4 characters
Serial.begin(9600);//Display the values and keys used on the serial monitor.
LiquidCrystal_I2C lcd(0x27, 16, 2);
Used 4x4 keyboard
Pin A0 with ranges is used to read values.
use in code.
if (value >= 66 - 2 && value <= 66 + 2) pressed = '1';
else if (value >= 255 - 2 && value <= 255 + 2) pressed = '2';// And other values
The task of the program will be to control the servo (open, close) using a password.
When servo position - open .LCD display says "Open, close with #". //When servo position - open, press # to perform position - close.
If the position of the servo is - closed - Its writing on the LCD "Enter code, card (position 0,0) , "Code:------------" (LCD position 0,1). / To open a password must be used.
Ukázka kódu
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // address, columns, rows
int openPos = 45;
int closedPos = 135;
String password = "11111"; // user-entered password
bool isOpen = false; // keep track of servo position
bool isEnteringPassword = false; // keep track of user input state
String enteredPassword = ""; // keep track of entered password
void setup() {
Serial.begin(9600);
servo.attach(3);
servo.write(closedPos);
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter code");
lcd.setCursor(0, 1);
lcd.print("Code:------------");
}
void checkPassword(char key) {
if (isEnteringPassword) {
if (key == '#') {
if (enteredPassword.length() >= 4 && enteredPassword == password) {
isEnteringPassword = false;
enteredPassword = "";
lcd.clear();
if (isOpen) {
servo.write(closedPos);
isOpen = false;
lcd.setCursor(0, 0);
lcd.print("Closed");
} else {
servo.write(openPos);
isOpen = true;
lcd.setCursor(0, 0);
lcd.print("Open, close #");
}
} else {
enteredPassword = "";
lcd.setCursor(0, 1);
lcd.print("Code:------------");
lcd.setCursor(6, 0);
lcd.print("Incorrect");
}
} else if (key == '') {
enteredPassword = "";
isEnteringPassword = false;
lcd.clear();
lcd.setCursor(0, 0);
if (isOpen) {
lcd.print("Open, close #");
} else {
lcd.print("Enter code, card");
lcd.setCursor(0, 1);
lcd.print("Code:------------");
}
} else if (enteredPassword.length() < password.length()) {
enteredPassword += key;
lcd.setCursor(5 + enteredPassword.length(), 1);
lcd.print(key);
}
} else {
if (key == '') {
isEnteringPassword = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password");
lcd.setCursor(0, 1);
lcd.print("Code:------------");
} else {
lcd.setCursor(0, 0);
if (isOpen) {
lcd.print("Open, close #");
} else {
lcd.print("Enter code, card");
lcd.setCursor(0, 1);
lcd.print("Code:------------");
}
}
}
}
void loop() {
int val = analogRead(A0);
if (val >= 66 - 2 && val <= 66 + 2) {
char key = '1';
checkPassword(key);
}
else if (val >= 255 - 2 && val <= 255 + 2) {
char key = '2';
checkPassword(key);
}
// add more key checks for the other values
// Check if the door is open and if it should be closed
if (isOpen && enteredPassword == "") {
servo.write(closedPos);
isOpen = false;
lcd.setCursor(0, 0);
lcd.print("Closed");
}
delay(100);
}
`