Glow LED on correct passcode

Hello all, in the below code my LED on OP PIN 13 doesn’t turn on if not provided delay…

/*
  Matrix Keypad Combination Lock Demo
  keypad-demo-lock.ino
  Combination lock using 4x4 matrix membrane keypad with Arduino
  Results on LCD display
  Drives relay

  DroneBot Workshop 2020
  https://dronebotworkshop.com

  Based upon code from https://www.circuitbasics.com/

*/

// Include Arduino Wire library for I2C
#include <Wire.h>
// Include LCD display library for I2C
#include <LiquidCrystal.h>
// Include Keypad library
#include <Keypad.h>

// Length of password + 1 for null character
#define Password_Length 8
// Character to hold password input
char Data[Password_Length];
// Password
char Master[Password_Length] = "123A456";

// Pin connected to lock relay input
int lockOutput = 13;

// Counter for character entries
byte data_count = 0;

// Character to hold key input
char customKey;

// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;

// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Connections to Arduino
byte rowPins[ROWS] = {28, 30, 32, 34};
byte colPins[COLS] = {36, 38, 40, 42};

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// Create LCD object
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

void setup() {
  // Setup LCD with backlight and initialize
lcd.begin(16,2);

  // Set lockOutput as an OUTPUT pin
  pinMode(lockOutput, OUTPUT);
}

void loop() {

  // Initialize LCD and print
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");

  // Look for keypress
  customKey = customKeypad.getKey();
  if (customKey) {
    // Enter keypress into array and increment counter
    Data[data_count] = customKey;
    lcd.setCursor(data_count,1);
    lcd.print(Data[data_count]);
    data_count++;
  }

  // See if we have reached the password length
  if (data_count == Password_Length - 1) {
    lcd.clear();

    if (!strcmp(Data, Master)) {
      // Password is correct
      lcd.print("Correct");
      // Turn on relay for 5 seconds
        delay(5000);
      digitalWrite(lockOutput, HIGH);
  
    }
    else {
      // Password is incorrect
      lcd.print("Incorrect");
      delay(1000);
    }

    // Clear data and LCD display
    lcd.clear();
    clearData();
  }
}

void clearData() {
  // Go through array and clear data
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
  return;
}

How to turn it ON for unlimited time… Please help

The LED should work without the (evil) delay(...).

The LED never gets turned off so once it is on it should stay on (as long as there is power).

Is this the real code?

no it is from a site called drone workshop it's an tutorial on how to use keypad. Everything works fine but it doesn't glow if there's no delay

If the LED was turned on before the delay and turned off after the delay perhaps I would understand, but as it stands I am puzzled.

yes the led turns off after 5 sec delay but I don't want to do that I want to keep it on forever and when I remove delay from the code the led doesn't get turn on.

So I only see one digitalWrite. It may turn the LED on or it may turn the LED off, I don't know which. If this turns the LED off, where is the part that turns it on? If this turns the LED on, where is the part that turns it off?

    if (!strcmp(Data, Master)) {
      // Password is correct
      lcd.print("Correct");
      // Turn on relay for 5 seconds
        delay(5000);
      digitalWrite(ledOutput, HIGH);
 
    }
    else {
      // Password is incorrect
      lcd.print("Incorrect");
      delay(1000);
    }

    // Clear data and LCD display
    lcd.clear();
    clearData();
  }

u see the ledOutput is set to high but the led doesn't glow if I put delay after this line then it glows till the delay and gets turned off

How is your LED wired ?

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components. Posting images:

As of now, I'm looking to inbuilt LED on mega on pin 13

ramesh4nani:
As of now, I'm looking to inbuilt LED on mega on pin 13

Looks like your LCD library is also using pin 13?

yeah but it works when I use delay but not without delay if i put delay of 5000 it turns off after 5 sec. My question is how to glow it for unlimited time? If lcd pin was the problem it was not supposed to glow even if delay is used.. Why so?

Do not use the pin 13 for your LED, your LCD uses it.

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Posting images:

I have changed the pin but it is still not working.....
It is working when delay is set to 5 sec....
What's wrong how to glow it for unlimited time?

Please use

autoformat
code tags
copy and paste

to post this code.

I had changed the board and pins and now it's working fine.... Thanks I think it wasn't working properly because I was using pin 13 which was also connected to lcd.

I am glad that you got it working.