Basically I have a keypad, that when a certain code is entered, it will write either "Wrong code" or "Good code" on the LCD.

At the same time a timer will count down until 0 is reached, in which case the whole LCD will display something (undecided/irrelevant).

I have tested the keypad module/part of the file, and it works exactly as expected. Each loop is entered correctly, and I have tested this part several times.

The problem, I believe, lies with the LCD. It will initialize, i.e It will display "Time: Code:"

But then, what comes after feels like it is randomly decided.

Sometimes I can press 1 button, and it will be printed to the serial. Other times I can press more. Sometimes it will lag for every button press, sometimes it will only be the first button press that is slow.

Secondly, if I try to re-run the script nothing will happen. I have another script (see the 2nd code,) that will usually do the trick.

But all of this takes forever. My code is suppossed to run independently, such that I can play multiple code games, will this obstruct it? Any ways to fix it?

Main code:

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

//LCD Setup
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//Keypad Setting

  char resetTrigger = '*';


  String goodCode1 = "3569";
  String goodCode2 = "69696969";
  String enteredCode = "";
  //Len has to be declared before the "checker" loop
  int len = enteredCode.length();


const byte ROWS = 4; /* four rows */
const byte COLS = 4; /* four columns */
/* define the symbols on the buttons of the keypads */
char hexaKeys[ROWS][COLS] = {
byte rowPins[ROWS] = {10, 9, 8, 7}; /* connect to the row pinouts of the keypad */
byte colPins[COLS] = {6, 5, 4, 3}; /* connect to the column pinouts of the keypad */

/* initialize an instance of class NewKeypad */
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
//Sets "Timer: " in the top and "Code: " in the bottom
lcd.print("Code: ");

void loop(){
  char customKey = customKeypad.getKey();
  len = enteredCode.length();

  if (customKey){
    //Reset Trigger 
  if (customKey == resetTrigger){
    //LCD clear code
    enteredCode = "";
    Serial.println("Reset trigger");
    len = enteredCode.length();
  //Char added to code
    Serial.println("Char added to code");
    len = enteredCode.length();
    //Correct code
  if ((enteredCode == goodCode1) or (enteredCode == goodCode2)){
    //LCD Correct Code
    //Good sound
    //Door open
    lcd.print("Correct code");
    Serial.println("Correct code");
  //Wrong code
  if ((len==8) && (enteredCode != goodCode1) && (enteredCode != goodCode2)){
    //LCD clear code
    //Bad sound
    enteredCode = "";
    Serial.println("Wrong code");
    //Changes the LCD to wrong code, waits 2 secs and then back to code
    lcd.print("Wrong code!");
    len = 0;


2nd code (reset code):

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.

void loop()


So now it will work more regularly after changing it from (LiquidCrystal_I2C lcd(0x27,16,2)) -> LiquidCrystal_I2C lcd(0x27,20,4).

I am still having problems with it though.

I have noticed, that sometimes it won't update the screen unless I physically nudge the LCD-screen, which is weird.

Also if it runs for "too long," the screen will stop updating at all.

Maybe a hardware issue?

  • 2
    If nudging helps, it is a clear sign of a hardware issue. Check your wiring and make sure you have a certain quality of components. Sep 2 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.