The code works in simulator but in my real arduino uno the code is succeeding to compile but the LCD is not displaying anything and it just stay light

The code works in simulator but in my real arduino uno the code is succeeding to compile but the LCD is not displaying anything and it just stay in light I already installed the right library in the simulatir through my uno

this is my work in simulation

this is the code:

#include <Keypad.h>
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Buzzer.h> // Include the Tone library for buzzer

// Define constants for better readability and maintainability
const int PASSWORD_LENGTH = 5;
const int BUZZER_PIN = 10; // Define the buzzer pin
const int BUZZER_FREQUENCY = 1500; // Define the buzzer frequency
const int MAX_WRONG_ATTEMPTS = 5; // Maximum allowed wrong attempts before locking
const int LOCK_DURATION = 30000; // Lock duration in milliseconds (30 seconds)

// Initialize LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Declare variables
char password[PASSWORD_LENGTH];
char initial_password[PASSWORD_LENGTH], new_password[PASSWORD_LENGTH];
int vcc = 11;
int i = 0;
int relay_pin = 11;
char key_pressed = 0;
const byte rows = 4;
const byte columns = 4;
char hexaKeys[rows][columns] = {
    {'1', '2', '3', 'A'},
    {'4', '5', '6', 'B'},
    {'7', '8', '9', 'C'},
    {'*', '0', '#', 'D'}};
byte row_pins[rows] = {2, 3, 4, 5};
byte column_pins[columns] = {6, 7, 8, 9};
Keypad keypad_key = Keypad(makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
int wrong_attempts = 0; // Counter for wrong attempts
unsigned long lock_start_time = 0; // Timestamp to track lock start time
bool device_locked = false; // Flag to indicate device lock state

// Function prototypes
void setPassword();
void initialpassword();
void buzzOnce();
void buzzEscapeAlarm();
void change();

// Setup function
void setup()
{
    // Initialize LCD
    lcd.begin(16, 2);
    lcd.backlight();

    // Set pin modes
    pinMode(relay_pin, OUTPUT);
    pinMode(vcc, OUTPUT);
    pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output

    // Display startup message
    lcd.print("    WELCOME");
    lcd.setCursor(0, 1); 
    delay(3000);
    lcd.clear();

    // Check if the password is already initialized in EEPROM
    bool password_initialized = true;
    for (int j = 0; j < PASSWORD_LENGTH; j++)
    {
        if (EEPROM.read(j) != j + 49)
        {
            password_initialized = false;
            break;
        }
    }

    if (!password_initialized)
    {
        lcd.print("Set up Password");
        lcd.setCursor(0, 1);
        setPassword();
    }
    else
    {
        // Prompt user to enter password
        lcd.print("Enter Password:");
        lcd.setCursor(0, 1);
        initialpassword();
    }
}

// Main loop function
void loop()
{
    digitalWrite(relay_pin, HIGH);

    // Check if '#' key is pressed to initiate password change
    key_pressed = keypad_key.getKey();
    if (key_pressed == '#' && !device_locked)
        change();

    // Check for keypad input
    if (key_pressed && !device_locked)
    {
        // Mask the password input with '*'
        password[i++] = key_pressed;
        lcd.print('*');
    }

    // Check if password length is reached and device not locked
    if (i == PASSWORD_LENGTH && !device_locked)
    {
        delay(200);

        // Read initial password from EEPROM
        for (int j = 0; j < PASSWORD_LENGTH; j++)
            initial_password[j] = EEPROM.read(j);

        // Check if entered password matches initial password
        if (!(strncmp(password, initial_password, PASSWORD_LENGTH)))
        {
            lcd.clear();
            lcd.print("Pass Accepted");
            digitalWrite(relay_pin, LOW);
            buzzOnce(); // Buzz once for correct password
            delay(2000);
            lcd.setCursor(0, 0);

            lcd.clear();
            lcd.print("Enter Password:");
            lcd.setCursor(0, 1);
            i = 0;
            wrong_attempts = 0; // Reset wrong attempts counter
        }
        else
        {
            // Increment wrong attempts counter
            wrong_attempts++;

            // Check if maximum wrong attempts reached
            if (wrong_attempts >= MAX_WRONG_ATTEMPTS)
            {
                device_locked = true;
                lock_start_time = millis(); // Record lock start time
            }

            digitalWrite(relay_pin, HIGH);
            lcd.clear();
            lcd.print("Wrong Password");
            buzzEscapeAlarm(); // Buzz like an escape alarm for wrong password
            lcd.setCursor(0, 0);

            lcd.clear();
            lcd.print("Enter Password:");
            lcd.setCursor(0, 1);
            i = 0;
        }
    }

    // Check if device is locked
    if (device_locked)
    {
        // Check if lock duration has passed
        if (millis() - lock_start_time >= LOCK_DURATION)
        {
            device_locked = false; // Unlock the device
            lcd.clear();
            lcd.print("Device Unlocked");
            delay(2000);
            lcd.clear();
            lcd.print("Enter Password:");
            lcd.setCursor(0, 1);
        }
        else
        {
            lcd.clear();
            lcd.print("Device in Lock");
            lcd.setCursor(0, 1);
            lcd.print("Wait for 30 secs");
            delay(500);
        }
    }
}

// Function to change password
void change()
{
    int j = 0;
    lcd.clear();
    lcd.print("Current Password");
    lcd.setCursor(0, 1);
    while (j < PASSWORD_LENGTH)
    {
        char key = keypad_key.getKey();
        if (key)
        {
            new_password[j++] = key;
            lcd.print('*'); // Mask the password input with '*'
        }
        key = 0;
    }
    delay(500);

    // Check if entered current password matches initial password
    if ((strncmp(new_password, initial_password, PASSWORD_LENGTH)))
    {
        lcd.clear();
        lcd.print("Wrong Password");
        lcd.setCursor(0, 1);
        lcd.print("Try Again");
        delay(1000);
    }
    else
    {
        j = 0;
        lcd.clear();
        lcd.print("New Password:");
        lcd.setCursor(0, 1);

        while (j < PASSWORD_LENGTH)
        {
            char key = keypad_key.getKey();
            if (key)
            {
                initial_password[j] = key;
                lcd.print('*'); // Mask the password input with '*'
                EEPROM.write(j, key);
                j++;
            }
        }
        lcd.print("Pass Changed");
        delay(1000);
    }

    lcd.clear();
    lcd.print("Enter Password:");
    lcd.setCursor(0, 1);
    key_pressed = 0;
}

// Function to initialize password from EEPROM
void initialpassword()
{
    for (int j = 0; j < PASSWORD_LENGTH; j++)
        EEPROM.write(j, j + 49);
    for (int j = 0; j < PASSWORD_LENGTH; j++)
        initial_password[j] = EEPROM.read(j);
}

// Function to make the buzzer sound once
void buzzOnce()
{
    tone(BUZZER_PIN, 1000, 100); // Buzz at 1000 Hz for 100 ms
}

// Function to make the buzzer sound like an escape alarm
void buzzEscapeAlarm()
{
    for (int i = 0; i < 5; i++)
    {
        tone(BUZZER_PIN, BUZZER_FREQUENCY, 200); // Buzz at specified frequency for 200 ms
        delay(300); // Wait for 300 ms between each buzz
    }
}

// Function to set up a new password
void setPassword()
{
    int j = 0;
    lcd.clear();
    lcd.print("Set Password:");
    lcd.setCursor(0, 1);

    while (j < PASSWORD_LENGTH)
    {
        char key = keypad_key.getKey();
        if (key)
        {
            initial_password[j] = key;
            lcd.print('*'); // Mask the password input with '*'
            EEPROM.write(j, key);
            j++;
        }
    }
    lcd.print("Pass Set");
    delay(1000);
    lcd.clear();
    lcd.print("Enter Password:");
    lcd.setCursor(0, 1);
}

Plss help me in my uno after i compile its all working but the lcd stays in light

Then it sounds like a wiring/power/hardware problem.

Try just the 'Hello World' program on your real hardware and let us know if it works.

Don

Yes the lcd work fine and display text in this code
I dont know if theres a problem in the code and this use another library to my lcdI2c

#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte number_of_rows = 4;
const byte number_of_columns = 4;

char row_pins[number_of_rows] = {2, 3, 4, 5};
char column_pins[number_of_columns] = {6, 7, 8, 9};

char key_array[number_of_rows][number_of_columns] = {  
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

const char *correct_password = "1234"; // Change this to your desired password
char entered_password[5] = ""; // Including space for null terminator

Keypad k = Keypad(makeKeymap(key_array), row_pins, column_pins, number_of_rows, number_of_columns);

const int solenoid_pin = 10; // Connect the solenoid to pin 11

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setBacklight(HIGH);
  pinMode(solenoid_pin, OUTPUT);
  lcd.print("Enter password:");
  lcd.setCursor(0, 1); // Set cursor to the second row
}

void loop() {
  char key_pressed = k.getKey();
  if (key_pressed) {
    if (strlen(entered_password) < 4) {
      entered_password[strlen(entered_password)] = key_pressed;
      lcd.print("*");
    } else {
      // Entered password length is 4, so clear the display and validate the password
      lcd.clear();
      lcd.print("Enter password:");
      lcd.setCursor(0, 1); // Set cursor to the second row
      if (strcmp(entered_password, correct_password) == 0) {
        lcd.print("Correct Password");
        digitalWrite(solenoid_pin, LOW); // Unlock the solenoid
        delay(2000); // Keep the solenoid unlocked for 2 seconds
        digitalWrite(solenoid_pin, HIGH); // Lock the solenoid
      } else {
        lcd.print("Wrong Password");
      }
      // Reset entered_password for next entry
      memset(entered_password, 0, sizeof(entered_password));
      delay(2000); // Display message for 2 seconds before clearing
      lcd.clear();
      lcd.print("Enter password:");
      lcd.setCursor(0, 1); // Set cursor to the second row
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.