Hello,
Seems like I have got a software issue. All my LCDs has stopped working and I suspect that there is a software issue that cause this.
Uploaded the same sketch to a controller with a working LCD - after upload the LCD behaved strange.
The LCDs backlight is blinking and only garbage is printed on the screen.
Suggestions how to solve this?
Test code:
#include <MyLcd_I2C.h>
// Variables used by interrupt functions.
// Set to 1 for 2004 display and 0 to 1602 display.
#define LCD2004 0
#define LCD_ADDR 0x27
MyLcd_I2C lcd1(LCD_ADDR, 16, 2);
/*
* Setup function.
*/
void setup()
{
Serial.begin(9600);
// Activate LCD module.
#if LCD2004 == 1
Serial.println("Initializing 2004");
lcd1.begin(20, 4);
#else
Serial.println("Initializing 1602");
lcd1.begin(16, 2);
#endif
//lcd1.setBacklight(HIGH);
#if LCD2004 == 1
lcd1.print("LCD TESTER 2004");
#else
lcd1.print("LCD TESTER 1602");
#endif
delay(2000);
}
/*
* Main loop function.
*/
void loop()
{
static unsigned long int loopCnt = 0, mSec = 0, lastSec= 0, uptime = 0;
static unsigned int h = 0, m = 0, s = 0, n;
static char buffer[32];
static bool light = true;
++loopCnt;
sprintf(buffer, "Loop %ld", loopCnt);
Serial.println(buffer);
// Uptime.
mSec = millis();
uptime += (mSec - lastSec) / 1000;
lastSec = mSec;
// Calculate hours, minutes and seconds uptime.
s = uptime % 60;
h = uptime / 3600;
m = (uptime - (unsigned long int)(h) * 3600) / 60;
// Go to start of 2nd line and show uptime.
sprintf(buffer, "Up: %3d:%02d:%02d", h, m, s);
lcd1.clearLine(0);
lcd1.print(buffer);
#if LCD2004 == 1
lcd1.clearLine(1);
lcd1.print("Random #1: ");
lcd1.print(random(0, 1000));
lcd1.clearLine(2);
lcd1.print("Random #2: ");
lcd1.print(random(0, 1000));
lcd1.clearLine(3);
lcd1.print("Random #3: ");
lcd1.print(random(0, 1000));
#else
lcd1.clearLine(1);
lcd1.print("Random: ");
lcd1.print(random(0, 1000));
#endif
delay(1000);
}
MyLcd_I2C class code:
MyLcd_I2C::MyLcd_I2C(uint8_t lcd_addr, uint8_t lcd_cols, uint8_t lcd_rows)
: LiquidCrystal_I2C(lcd_addr)
{
m_cols = lcd_cols;
m_rows = lcd_rows;
}
void MyLcd_I2C::clearLine(const int lineNumber, const bool firstPos)
{
if (lineNumber < m_rows) {
setCursor(0, lineNumber);
for (int i = 0; m_cols > i; i++) {
write(' ');
}
if (firstPos) {
setCursor(0, lineNumber);
}
}
}
size_t MyLcd_I2C::writeLine(const int lineNumber, const char *string)
{
if (lineNumber < m_rows) {
clearLine(lineNumber);
return print(string);
}
return 0;
}
size_t MyLcd_I2C::writeLine(const int lineNumber, const __FlashStringHelper* string)
{
size_t n = 0;
if (lineNumber < m_rows) {
clearLine(lineNumber);
PGM_P p = reinterpret_cast<PGM_P>(string);
while (true) {
unsigned char c = pgm_read_byte(p++);
if (0 == c) {
break;
}
if (!write(c)) {
break;
}
++n;
}
}
return n;
}
void MyLcd_I2C::at(const int lineNumber, const int cursorPosition, const char *string)
{
if (lineNumber < m_rows) {
setCursor(cursorPosition, lineNumber);
print(string);
}
}
void MyLcd_I2C::at(const int lineNumber, const int cursorPosition, const __FlashStringHelper *string)
{
if (lineNumber < m_rows) {
setCursor(cursorPosition, lineNumber);
print(string);
}
}