LCD text being corrupted by something

When I press '#' on the keypad in this program, it rarely (but sometime does) puts the requested "CLEARED SCREEN" to the LCD. What is wrong here? It is giving me a headache.

It ends up corrupting the text on LCD after a few keypresses.

As this is just the start of a bigger program, in this state, I just expect it to show the typed numbers/letters to screen and the '#' key to show "CLEARED SCREEN". It does type the inputs mostly, but can corrupt screen after a few key presses.

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

LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte ROWS = 4;  //four rows
const byte COLS = 4;  //three columns
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
char str[17] = "                ";
byte rowPins[ROWS] = { 5, 4, 3, 2 };  //connect to the row pinouts of the keypad
byte colPins[COLS] = { 9, 8, 7, 6 };  //connect to the column pinouts of the keypad
int d = 0;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(115200);
  lcd.init();  // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("PLAY SONG NUMBER....");
  lcd.setCursor(0, 1);
  lcd.print("");
  lcd.setCursor(0, 2);
  lcd.print("");
  lcd.setCursor(0, 3);
  lcd.print("");
}

void loop() {

  char key = keypad.getKey();
  if (key) {
  if (key == '#') {
    strcpy(str,"                ");
    lcd.setCursor(0, 3);
    lcd.print("CLEARED   SCREEN");
    delay(50);
    d = 0;
        str[d + 1] = '\0';
  }

  if (key != '#' and d < 16) {
    Serial.println(key);
    str[d] = key;
    str[d + 1] = '\0';
    Serial.println(d);
    lcd.setCursor(0, 3);
    lcd.print(str);
        Serial.println(sizeof(str));
            Serial.println(str);
        d = d + 1;
  }
  }
}

When you say corrupt the screen..
Do you mean it leaves "CLEARED SCREEN" on the screen and types over it??

I saw that, corrected it..
code seems ok, maybe a bad connection??

Uno Keypad LCD music picker..

good luck.. ~q

Thanks for that emulation. It doesn't even put "CLEARED SCREEN" to the LCD with a key press - and that is very annoying, because at the same time, if I have a Serial.println(bla) in the line after
lcd.setCursor(0, 3);lcd.print("CLEARED SCREEN"); It does still Serial Print to PC. It is annoying me.

The cable from the Keypad is around 80 cm long and is connected correctly and the Arduino Leonardo is picking it up correctly, as it does the Serial.println(). Annoying!

So it's really just garbage on the screen??
How long is the cable for the screen??
~q

Thanks for replying.

There are 4 x 15cm cables feeding the screen, then 8 x 80cm to 1m cables from the keypad into the Arduino Leonardo.

so the screen is closer, just 15cm away??
distance should be ok but if trouble is only with screen then obviously there's a connection issue somewhere..

have you done a schematic yet??
might be advisable, more eyes may start looking..

how about a pic??

sorry.. ~q

Doesn't seem to be causing a problem, since the display actually works at times, but you shouldn't call lcd.init() twice in setup().

Is there any way to make that Arduino a Leonardo?

Just to update, I moved the code to an Arduino Uno and it works fine. I'm not sure why the Leonardo behaved strangely.

In the sim, no..
They have atiny, nano, uno and mega..
Strange, I'm still kind of thinking it was a bad connection..
Sometimes, taking things apart, then back together is all that's needed..
You can't see the bad connection..

glad you sorted it out..

~q

No, I changed the cables a couple of times. After it worked on the Uno, tried it again with the Leonardo and it doesn't work the same.

On the Leonardo, digital pins 2 and 3 are the I2C pins, do not use those pins for the keypad.

1 Like

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