LCD issues (Sometimes it works well and sometimes not) I made a video off it

Hello,

I hope someone can explain why this is happening to me. I made this for a geocache but it is still not working fine to me. See video to see what’s my problem :frowning:

I use a I2C lCD

Pic 1 bad ( Dropbox - Error )

Pic 2 good ( Dropbox - Error )

#include <Keypad.h> //Library for the matrix keypad available from http://playground.arduino.cc/code/Keypad
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> //You must use F. Malpartida's new LCD library for Serial LCD https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

 
#define I2C_ADDR    0x27  // Define I2C Address for the LCD. This varies by device, see the data sheet for yours
// Set up serial to parallel configuration for LCD
#define BACKLIGHT_PIN 3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
 

// define an LCD instance
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// define keypad mapping
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap. Note the position of # and * alter between models
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
 
// Connect keypad ROW0, ROW1, ROW2 and ROW3 (top to bottom) to these Arduino pins.
byte rowPins[ROWS] = { 
  9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 (left to right) to these Arduino pins.
byte colPins[COLS] = { 
  12, 11, 10 }; 
 
// Create the Keypad instance
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
 
void setup(){
   pinMode(4, OUTPUT); // pin for relais
   pinMode(3, OUTPUT); // pin for green LED
   pinMode(2, OUTPUT); // pin for red LED
   
   
   lcd.begin (16,2); // initialise LCD
   
 
   // Switch on the backlight
   lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
   lcd.setBacklight(HIGH);
   // clear the LCD
   lcd.clear();
   // move the cursor top left
   lcd.home();
   // print a message on the LCD to enter the code
   lcd.print("   ENTER CODE");
   lcd.setCursor(0, 1);
   lcd.print("");
   lcd.setCursor(0, 3);
}
 
void loop(){
 
  String mastercode = "1234"; // the password
  String enteredcode = ""; // the code you enter
  String reason = " INCORRECT CODE   "; // the default reason why entry failed
  // read a 6 digit code (you can have any length)
  for (int i = 0; i < 8; i++){
    char key = kpd.waitForKey();
 
    if(key != NO_KEY){ // if you aren't reading no keypress

      lcd.print("*"); // print a # to the LCD so people don't see the number but you know you pressed a button
      enteredcode += key; // append the keypress to the end of the entered code string
    }
  }
 
  if (enteredcode == mastercode) // if the code is correct
  {
    lcd.setCursor (0, 3);
    lcd.print("                ");
    lcd.setCursor (0, 1);
    lcd.print("  CODE CORRECT    "); // print a success message
    digitalWrite(3, HIGH); // turn on the green LED
    digitalWrite(4, HIGH); // turn on the relais
    delay (6000); // wait 3 seconds while the door is opened
    digitalWrite(3, LOW); // turn off the LED
    digitalWrite(4, LOW); // turn off the relais
    
    
    

    //reset the LCD for the next user
    lcd.clear();
    lcd.home();
    lcd.print("   ENTER CODE");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.setCursor(0, 1);
  }
  else {  // if the code is wrong or cancelled
    // tell the user what happened
    lcd.setCursor (0, 3);
    lcd.print("                    ");
    lcd.setCursor (0, 1);
    lcd.print(reason); // display the reason for failure. Incorrect code by default unless cancel is pressed
    digitalWrite(2, HIGH); // turn on the red LED
    delay (3000); // wait 3 seconds before allowing a retry
    digitalWrite(2, LOW); // turn off LED
    // reset the LCD for the next try
    lcd.clear();
    lcd.home();
    lcd.print("   ENTER CODE");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.setCursor(0, 3);

 
  }
 
 
}

Please "modify" your first post and change the "quote" tags to "code".

I do not have Javascript enabled for dropbox, so I unfortunately cannot see your video.

Hi

You use relay and I2c.

// define an LCD instance
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
....
 pinMode(4, OUTPUT); // pin for relais

I saw the 2 pictures, and sound like a bad initialization of the lcd. Some time you got a full bar on the first line of you 16x2, and sometime is working fine.

I got bad experiences with electromagnetic generated by relays and coiled power supply: It generate scramble and malfunctions. And over the forum you will get a lot of users who experienced same issue. Could be that, could be defective LCD or I2C adapter. I don't thing code is the cause of this random issue

Is there an solution for it? Yes I used a relais and 12V battery.

construction

http://oi62.tinypic.com/2r2u7u0.jpg

Paul__B: Please "modify" your first post and change the "quote" tags to "code".

I do not have Javascript enabled for dropbox, so I unfortunately cannot see your video.

youtube version

http://youtu.be/DXuyVk1MBTQ

If I use my usb cable it works fine. At the moment I use a power supply battery 12v it going wrong.

deswertn:
youtube version

OK, got that. Well demonstrated (albeit only in fullscreen).

It may not be the culprit, but you seem to have a spurious line:

#include <LCD.h>

Garbled line on LCD is usually a result of reset / and or infinite loop. A weak power supply will do the same.

Put some test delays in few places to see if the display is actually holding the data you expect and not resetting.

I prefer to clear the LCD before any single shot / progress report - you may be overriding the character buffer too. But that would show -up as shifted output on unintended line, did not see that.