Hi guys,
I am experiencing a black box error in my project I have checked my connections 4 times and am still experiencing the same error. Attached below is my code
#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
// Keypad configuration
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 2, 3, 24, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Servo configuration
Servo myservo;
// Analog input and button configuration
const int analogPin = A0; // Analog input pin
const int buttonPin = 53; // Button pin
bool startProcess = false;
// LCD configuration
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // rs, en, d4, d5, d6, d7
// Passwords and input code
const char* pass1 = "3141";
const char* pass2 = "1009";
const char* pass3 = "1729";
char inputCode[5]; // Character array to store input code
int inputCodeIndex = 0;
void setup() {
Serial.begin(9600);
// Ensure the LCD is initialized correctly
lcd.begin(16, 2);
myservo.attach(22);
myservo.write(0); // Initial position
pinMode(buttonPin, INPUT_PULLUP);
pinMode(analogPin, INPUT); // Set analog pin as input
lcd.print("System Ready");
Serial.println("System Ready"); // Debugging output
}
void loop() {
checkButtonPress();
if (startProcess) {
char key = keypad.getKey();
if (key!= NO_KEY) {
processKeyInput(key);
}
}
}
void checkButtonPress() {
if (digitalRead(buttonPin) == LOW &&!startProcess) {
startProcess = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Passcode :"); // Move servo to open position
delay(1000); // Ensure enough delay for the servo to reach the position
lcd.setCursor(0, 1);
lcd.noAutoscroll();
Serial.println("Start Process: Enter password");
}
}
void processKeyInput(char key) {
Serial.print("Key Pressed: ");
Serial.println(key);
inputCode[inputCodeIndex++] = key;
delay(50); // Debounce delay
lcd.setCursor(0, 1);
lcd.print(inputCode);
if (inputCodeIndex == 4) {
checkPassword();
}
}
void checkPassword() {
Serial.println("Checking password");
if (strcmp(inputCode, pass1) == 0 || strcmp(inputCode, pass2) == 0 || strcmp(inputCode, pass3) == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
Serial.println("ACCESS GRANTED");
myservo.write(180); // Move servo to 180 degrees
delay(3000); // Ensure enough delay for the servo to reach the position
myservo.write(0); // Move servo back to 0 degrees
delay(1000); // Ensure enough delay for the servo to reach the position
inputCodeIndex = 0; // Reset input code index
startProcess = false; // Reset process
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD");
Serial.println("WRONG PASSWORD");
delay(2000);
lcd.clear();
inputCodeIndex = 0; // Reset input code index
startProcess = false; // Reset process
}
}
Thank you