How can I stop messages from overlapping on LCD?

I am trying to save money on my Solar system by using the least amount of batteries possible. The system is for powering a freezer by day and by the Grid (when I want).
My program using an Arduino uno will operate in 2 modes: Mode 1 (Solar only); the load will be connected only if it is sunny. Mode 2 (Solar or Grid); the load will be connected at night if the Grid is present but disconnected if no Grid available.
The first push button will toggle the modes and the second will simulate the presence of the Grid.
My problem is the messages on the lcd are overlapping (Please see diagram). The “Mode2 Grid on and Night” message leaves behind the “t” when the second condition is true.
Any other observation/corrections on the code will be greatly appreciated.
Thanks for your anticipated response.

#include <LiquidCrystal.h>
const int Switch = 7;
int state = 0;
int Loadstate = 0;
int led = 13;
int ldr = A0;
int Griddetect = 10;
int GriddetectState;
int cuttoff = 560;
int programState = 8;
int shortDelayTime = 200;
int medianDelayTime = 2000;

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


void setup()
{
  pinMode(Switch, INPUT);
  pinMode(led, OUTPUT);
  pinMode(Griddetect, INPUT);
  pinMode(ldr, INPUT);
  pinMode(programState, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Welcome to Solar Mgr");
  delay(3000);
  lcd.clear();
}
void loop()
{
  int ldrValue = analogRead(ldr);
  GriddetectState = digitalRead(Griddetect);
  while (state == 0 && digitalRead(Switch) == HIGH) {
    state = 1;
    Loadstate = !Loadstate; lcd.clear();
  }
  while (state == 1 && digitalRead(Switch) == LOW) {
    state = 0; lcd.clear();
  }
  if (Loadstate == LOW) {
    lcd.setCursor(0, 0);
    lcd.print(" Mode1 ON");
    delay(shortDelayTime);
    if (ldrValue > cuttoff) {
      digitalWrite(led, HIGH);
      Serial.println("LOW: Sunny only");
      lcd.setCursor(0, 1);
      lcd.print("Sunny");
      Serial.println(ldrValue);
    }
    else {
      digitalWrite(led, LOW);
      Serial.println("LOW: Night only");
      lcd.setCursor(0, 1);
      lcd.print("Night");
      Serial.println(ldrValue);
    }
    digitalWrite(programState, LOW);
    delay(shortDelayTime);
  }


  if (Loadstate == HIGH) {
    lcd.setCursor(0, 0);
    lcd.print("Mode2 ON ");
    if (ldrValue <= cuttoff && GriddetectState == true) {

      digitalWrite(led, HIGH);
      Serial.println("Grid On & Night");
      lcd.setCursor(0, 1);
      lcd.print("Grid ON & Night");
    }
    else if (ldrValue > cuttoff && GriddetectState == true) {
      digitalWrite(led, HIGH);
      Serial.println("Grid On & sunny");
      lcd.setCursor(0, 1);
      lcd.print("Grid ON & sunny");
    }
    else if (ldrValue > cuttoff && GriddetectState == false) {
      digitalWrite(led, HIGH);
      Serial.println(" Sunny" );
      lcd.setCursor(0, 1);
      lcd.print("No Grid & Sunny");
    }
    else {
      digitalWrite(led, LOW);
      Serial.println("HIGH: Grid off & Night" );
      lcd.setCursor(0, 1);
      lcd.print("Grid off & Night");

    }
    digitalWrite(programState, HIGH);
    delay(shortDelayTime);
  }

}

lcd messages.JPG

lcd msg 2.JPG

gridoffnight.JPG

lcd messages.JPG

lcd msg 2.JPG

gridoffnight.JPG

How about clearing the display before updating the display?

or, in this specific case, ensure that the phrase you write is long enough to clear the relics from the last write operation.

change:
lcd.print("No Grid & Sunny") ;

to:
lcd.print("No Grid & Sunny ") ; // note the trailing space !

See what happens when you change;

lcd.print("No Grid & Sunny");

to

lcd.print("No Grid & Sunny ");

Idahowalker:
How about clearing the display before updating the display?

I tried to do that but the screen started flickering.

6v6gt:
or, in this specific case, ensure that the phrase you write is long enough to clear the relics from the last write operation.

change:
lcd.print("No Grid & Sunny") ;

to:
lcd.print("No Grid & Sunny ") ; // note the trailing space !

This did the magic! Thank you very much.

srnet:
See what happens when you change;

lcd.print("No Grid & Sunny");

to

lcd.print("No Grid & Sunny ");





Thanks. :)

Create a function that accepts the text you want to display and returns a version of that text that is padded with spaces out to the number of characters in your display. That will avoid the flickering that comes with clearing the display between each write to the display.

actually u san use array in a form of raw n column so that it writes up until second of last box, then the last is enter eg string[10] -it will write upto 9 boxes then last is enter.

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