Black box error

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

What do you mean by a "black box error" ?

Sorry for not explaining, The top row of my lcd display is showing a bunch of black boxes and not displaying content

Which arduino are you using that has a pin 24?

Arduino Mega

Do you have the keypad and the lcd both wired to pins 2, 3, and 5?

Sorry for not checking i will change it and let u know in 5 mins

Hi this is my new code and it still does not work

#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(4, 10, 11, 12, 13, 14); // rs, en, d4, d5, d6, d7

// Passwords and input code
String inputCode = "";
const String pass1 = "3141";
const String pass2 = "1009";
const String pass3 = "1729";

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() {
  // Check if the button is pressed to start the process
  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");
  }

  if (startProcess) {
    char key = keypad.getKey();

    if (key != NO_KEY) {
      Serial.print("Key Pressed: ");
      Serial.println(key);
      inputCode += key;
      delay(50);  // Debounce delay
      lcd.setCursor(0, 1);
      lcd.print(inputCode);
    }

    // Check if the entered code is 4 characters long
    if (inputCode.length() == 4) {
      Serial.println("Checking password");
      if (inputCode == pass1 || inputCode == pass2 || inputCode == pass3) {
        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
        inputCode = "";
        startProcess = false;  // Reset process
      } else {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("WRONG PASSWORD");
        Serial.println("WRONG PASSWORD");
        delay(2000);
        lcd.clear();
        inputCode = "";
        startProcess = false;  // Reset process
      }
    }

    // Check if the entered code is too long
    if (inputCode.length() >= 5) {
      inputCode = "";
      lcd.clear();
      lcd.print("Number too long");
      Serial.println("Number too long");
      delay(2000);
      lcd.clear();
      startProcess = false;  // Reset process
    }
  }
}

Check your LCD brightness setting.
Post the wiring schematic of your project to make it easier for us to help you.

Your code is working correctly.
See here in the simulator.
I simulated in HW only the LCD.

Hi, I am on my school campus and I can't get or make a schematic for 2 months and the simulations work
Thank you I will follow it