/*
Solar to Grid Auto Change Over
https://forum.arduino.cc/t/how-can-i-stop-messages-from-overlapping-on-lcd/697672
See Diagram
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.
*/
#include <LiquidCrystal_I2C.h>
void createCustomCharacters();
void printFrame();
//defining House Icon
byte housechar1[8]={B00000, B00001, B00011, B00011, //Row 0, Col 0
B00111, B01111, B01111, B11111,};
byte housechar2[8]={B11111, B11111, B11100, B11100, //Row 1, Col 0
B11100, B11100, B11100, B11100,};
byte housechar3[8]={B00000, B10010, B11010, B11010, //ROW 0, Col 1
B11110, B11110, B11110, B11111,};
byte housechar4[8]={B11111, B11111, B11111, B10001, //Row 1, Col 1
B10001, B10001, B11111, B11111,};
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int Switch = 7; // Mode 1 and Mode 2 Selection
int state = 0;
int Loadstate = 0;
int led = 13;
int ldr = A0;
int Griddetect = 10;
int GriddetectState;
int cuttoff = 560; // Value of LDR ???
int programState = 8;
int shortDelayTime = 200;
int medianDelayTime = 2000;
void setup()
{
pinMode(Switch, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(Griddetect, INPUT_PULLUP);
pinMode(ldr, INPUT_PULLUP);
pinMode(programState, OUTPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
delay(1000);
lcd.createChar(1,housechar1);
lcd.createChar(2,housechar2);
lcd.createChar(3,housechar3);
lcd.createChar(4,housechar4);
lcd.setCursor(0,0);
lcd.write(1);
lcd.setCursor(0,1);
lcd.write(2);
lcd.setCursor(1,0);
lcd.write(3);
lcd.setCursor(1,1);
lcd.write(4);
lcd.setCursor(3,0);
lcd.print("Electro ");
lcd.setCursor(3,1);
lcd.print("Devices AJMER ");
delay(10000);
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);
}
}