Problem with I2C lcd

Hi there!

I wanted to build a clock without RTC module.
The code works well, but I’ve got a problem with the screen:

I use I2C module for LCD’s:

``

This is the code:

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int hour=0;
int minute=0;
int second=0;
char cmd[100];
byte cmdIndex;
Timer secAdd;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.print("Waiting for COM");
  secAdd.begin(1000);  
  while(!Serial){}
  lcd.clear();
}


void exeCmd() {

  if (cmd[0] == 'h') {
    unsigned int val = atof(cmd + 2);
    hour=val;
  }
  if (cmd[0] == 'm') {
    unsigned int val = atof(cmd + 2);
    minute=val;
  }
  if (cmd[0] == 's') {
    unsigned int val = atof(cmd + 2);
    second=val;
  }
}

void lcdPrint() {
 lcd.clear();
 lcd.write("TIME ");
 lcd.write(char(hour));
 lcd.write(":");
 lcd.write(char(minute));
 lcd.write(":");
 lcd.write(char(second));
}

void loop() {
  // put your main code here, to run repeatedly:
 if (Serial.available() > 0)
  {
    char c = (char)Serial.read();
    if (c == '\n') {
      cmd[cmdIndex] = 0;
      exeCmd();
      cmdIndex = 0;
    } else {
      cmd[cmdIndex] = c;
      if (cmdIndex < 99) cmdIndex++;
    }
  }
 if(secAdd.available()){
  second=second+1;
  secAdd.restart();
  lcdPrint(); 
 }
 if(second==60){
  second=0;
  minute=minute+1;
  lcdPrint();
 }
 if(minute==60){
  minute=0;
  hour=hour+1;
  lcdPrint();
 }
 if(hour==24){
  hour=0;
  lcdPrint();
 }

}

Thank you in advance!

 lcd.write(char(hour));

That will write to the LCD the character with the character code hour, not the integer hour.

What happens if you just use:

LCD.print(hour);

In fact I would replace all of the writes with print. Write is to print single bytes like custom character codes.

groundFungus:

 lcd.write(char(hour));

That will write to the LCD the character with the character code hour, not the integer hour.

Nothing changed...

void lcdPrint() {
 lcd.clear();
 lcd.print("TIME ");
 lcd.print(char(hour));
 lcd.print(":");
 lcd.print(char(minute));
 lcd.print(":");
 lcd.print(char(second));
}

It is a good idea to use:

 lcd.print((char)hour);

It is a good idea to use:

lcd.print((char)hour);

No, unless you want to print the character with the character code of the value of hour. Lets say that hour = 5. The character code 5 in the LCD character set is reserved for a custom character. If no custom character is defined the LCD will print whatever (garbage) is in that LCD character ram location. For an experiment, do:

 lcd.print(char(53));

That will print the character with the character code of 53. 53 is the decimal character code for 5. You should see 5 printed on the LCD,

The data sheet for your LCD will show the character set (or sometimes called the font table) for your LCD.

If you want to print hour just use:

lcd.print(hour);

As always, it is doing exactly what you program it to. You tell it to print some arbitrary character, it does just that, not a number. :roll_eyes:

OK got it :slight_smile: !

lcd.print(hour,DEC);

but thanks!