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);
}
}