LCD and LED wont work together

This project is to get a button turn a light off for ten seconds and then turn back on after that ten seconds elapsed, and then have my keypad do the same after entering in the password, but the problem is that when I do run the program, The LCD turns on, says the beginning message, then the LED turns on and gives the LCD a green screen of death. heres my code,

int buttonState = 0;
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Key.h>

LiquidCrystal lcd(13,12,5,4,3,2);
const byte ROWS = 4;
const byte COLS = 4;
int lcdRow = 0;
char keys[ROWS][COLS]={
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS]={A0,A1,A2,A3};
byte colsPins[COLS]={A4,A5,11,10};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colsPins,ROWS,COLS);
String password;

void setup()
{
  pinMode(7, INPUT);
  pinMode(9, OUTPUT);
/////////////////////////////////////////////////////////////////////////
pinMode(2, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.print("Enter Password: ");
  delay(1000);
  lcd.clear();
 }

void loop()
{
  buttonState = digitalRead(7);
  if (buttonState == LOW) {
    digitalWrite(9, HIGH);
  } else {
    digitalWrite(9, LOW);
    delay(10000); // Wait for 10000 millisecond(s)
  ////////////////////////////////////////////////////////////////////////////////////////////////
     char key = keypad.getKey();
  if (key){
    lcd.print(key);
    lcd.setCursor(++lcdRow,0);
    password = password+key;
    if (key=='A'){
      lcd.clear();
      lcd.print("Checking Password");
      lcd.setCursor(0,1);
      lcd.print(password);
      delay(1000);
      if (password=="111A"){
        lcd.clear();
        lcd.print("Accepted");
        delay (1000);
          lcd.clear();
        password = "";
      }
      else {
        lcd.clear();
        lcd.print("Denied");
      }
    }
    if (key=='C'){
      lcd.print("Clearing...");
      delay(1000);
      lcd.clear();
      password = ""; 
    
 
 

   }
  }
}
}








You know this blocks code for 10 sec and 1 sec.

delay(10000); // Wait for 10000 millisecond(s)
. . .
delay(1000);


Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.

Thank you, this is my first time really coding and using any type of forum, I’ll make a better post next time. This really helped​:pray:t5::pray:t5::pray:t5:

If you use Auto Format in the blocks of code in your sketch become more obvious, like thsi

int buttonState = 0;
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Key.h>

LiquidCrystal lcd(13, 12, 5, 4, 3, 2);
const byte ROWS = 4;
const byte COLS = 4;
int lcdRow = 0;
char keys[ROWS][COLS] = {
    { '1', '2', '3', 'A' },
    { '4', '5', '6', 'B' },
    { '7', '8', '9', 'C' },
    { '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { A0, A1, A2, A3 };
byte colsPins[COLS] = { A4, A5, 11, 10 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colsPins, ROWS, COLS);
String password;

void setup()
{
    pinMode(7, INPUT);
    pinMode(9, OUTPUT);
    /////////////////////////////////////////////////////////////////////////
    pinMode(2, OUTPUT);
    Serial.begin(9600);
    lcd.begin(16, 2);
    lcd.print("Enter Password: ");
    delay(1000);
    lcd.clear();
}

void loop()
{
    buttonState = digitalRead(7);
    if (buttonState == LOW)
    {
        digitalWrite(9, HIGH);
    }
    else
    {
        digitalWrite(9, LOW);
        delay(10000);  // Wait for 10000 millisecond(s)
                       ////////////////////////////////////////////////////////////////////////////////////////////////
        char key = keypad.getKey();
        if (key)
        {
            lcd.print(key);
            lcd.setCursor(++lcdRow, 0);
            password = password + key;
            if (key == 'A')
            {
                lcd.clear();
                lcd.print("Checking Password");
                lcd.setCursor(0, 1);
                lcd.print(password);
                delay(1000);
                if (password == "111A")
                {
                    lcd.clear();
                    lcd.print("Accepted");
                    delay(1000);
                    lcd.clear();
                    password = "";
                }
                else
                {
                    lcd.clear();
                    lcd.print("Denied");
                }
            }
            if (key == 'C')
            {
                lcd.print("Clearing...");
                delay(1000);
                lcd.clear();
                password = "";
            }
        }
    }
}

Notice how reading a character from the keypad is always preceded by a 10 second delay(). At the very best it is going to take 40 seconds to enter the password even if we assume that the logic of the sketch is correct, which I doubt

Hi @spain_wo_the_s_06 ,

not sure what you want to do with the switch but here is something for you to check:

https://wokwi.com/projects/391629716735284225

/*
  Forum: https://forum.arduino.cc/t/lcd-and-led-wont-work-together/1232311
  Wokwi: https://wokwi.com/projects/391629716735284225

*/

int buttonState = 0;
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Key.h>

LiquidCrystal lcd(13, 12, 5, 4, 3, 2);

const byte ledPin = 9;
const byte switchPin = 7;


const byte ROWS = 4;
const byte COLS = 4;
int lcdRow = 0;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colsPins[COLS] = {A4, A5, 11, 10};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colsPins, ROWS, COLS);
String password;


void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  /// pinMode(2, OUTPUT); ???????????????????
  Serial.begin(115200);
  lcd.begin(16, 2);
  lcd.print("Enter Password: ");
  delay(1000);
  lcd.clear();
}

void loop()
{
  if (enabled()) {
    CheckPwd();
    digitalWrite(ledPin,HIGH);
  } else {
    digitalWrite(ledPin,LOW);
  }
}

boolean enabled() {
  return (!digitalRead(switchPin));
}

void CheckPwd(){
  char key = keypad.getKey();
  if (key) {
    lcd.print(key);
    password = password + key;
    if (key == 'A') {
      clearPrintWaitDelete("Checking PWD",0, false);
      lcd.setCursor(0, 1);
      lcd.print(password);
      delay(1000);
      if (password == "111A") {
        clearPrintWaitDelete("Accepted",1000, true);
      }
      else {
        clearPrintWaitDelete("Denied",1000, true);
      }
    }
    if (key == 'C') {
      clearPrintWaitDelete("Clearing...",1000, true);
    }
  }
}

void clearPrintWaitDelete(String msg, unsigned long waitTime, boolean delPwd) {
  lcd.clear();
  lcd.print(msg);
  delay(waitTime);
  if (delPwd) {
    lcd.clear();
    password = "";
  }
}

As already mentioned the delay of 10000 ms in loop() does not allow for a proper use of the keypad.

What's your intention with the delay?

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