Problem with lcd.clear();

Hello,

I am having some difficulties with clearing the lcd and moving on with the sketch. I never used lcd.clear before.

The sketch is the "Hello, world!" and counting example in the Arduino LCD library.

I want the sketch to display the "Hello world!" message and count to 10 and then clear the LCD and display "Hello again!" and start counting again.

I can do the "Hello world!" and count part and if I don't insert the lcd.clear command I get the "Hello again!" message after the 10 seconds printed on the second row after the time counter. On the first row I still get "Hello world!"

If I insert lcd.clear all the characters on the screen are blinking fast. I guess the sketch starts, I get the "Hello world!" message, the LCD is cleared and then I get "Hello world!" message again and the lcd is cleared again.

It loops both the message and the lcd.clear command.

Here is the modified sketch

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  
  
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  {lcd.print("hello");
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);}
  if (millis()/1000 >10)
  lcd.clear();
  {lcd.print("hello2");
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);}
}

I have moved the first "hello". In the original sketch with the "hello" in the void setup part of the sketch, lcd.clear was clearing the "hello" word but it kept looping the counter and clearing it. I found it interesting that even if it cleared the lcd, the counter would count the seconds correctly.

Any help would be appreciated.

Any help would be appreciated.

Sure. But, you've to help us, first. Put each { on a new line. Put each } on it's own line. Use the Tools + Auto format menu item to reformat the code. Post the reformatted code. The complete lack of white space around the braces makes you code very hard to read.

I think that you'll find, when you do that, that you have code inside an if block that you don't think is in the if block.

Hello,

Thank you for the tips. I did not know about the Auto format option.

I hope this is better

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  lcd.begin(16, 2);
}

void loop() 
{
  {
    lcd.print("hello");
    lcd.setCursor(0, 1);
    lcd.print(millis()/1000);
  }
  if (millis()/1000 >10)
    lcd.clear();
  {
    lcd.print("hello2");
    lcd.setCursor(0, 1);
    lcd.print(millis()/1000);
  }
}

{
lcd.print("hello");
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}

These curly braces are unnecessary. You should remove them.

  if (millis()/1000 >10)
    lcd.clear();

The body of the if statement is the single method call to clear the screen. Is this what you intended?

This code:

  {
    lcd.print("hello2");
    lcd.setCursor(0, 1);
    lcd.print(millis()/1000);
  }

is unconditionally executed. Is that what you intended? If so, the braces are unnecessary. If this is supposed to be the body of the if statement, some code rearrangement is in order.

The millis() output keeps counting up. After 10 seconds, the statement if(millis()/1000 > 10) will always be true,

I think that what you want to do is measure the difference between now (what millis() returns) and then (some previously stored millis() output) and compare that to 10000. Comparing to 10000 is faster then dividing by 1000 and comparing to 10. You should get in the habit from the beginning doing things as fast as possible, so you don't develop bad habits. You are off on the right path not using delay(), so unlearning that bad habit won't be necessary.