I am using a DS3231 timer circuit and a I2C lcd plus RELAY shield with arduino uno. My target is to turn on the relay according to pre-programmed time and showing the current time in the LCD. The program works fine, except i have used a sub program named as WATCHMAN. The purpose of this sub program is to run the watch and show it in LCD. Problem is each time I plug it off and then after plugging on the whole hardware .. the LCD shows weird character instead of the CLOCK. What is the problem that is screwing up the LCD display?
Code is as below -
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(9, 10);
#include <Wire.h>
#define ADDRESS 0x68
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#define BACKLIGHT_PIN 13
void setup() {
Serial.begin(9600);
Wire.begin();
SIM900A.begin(9600);
delay(15);
lcd.begin(16,2);
delay(15);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode ( BACKLIGHT_PIN, OUTPUT );
digitalWrite ( BACKLIGHT_PIN,HIGH);
}
void loop(){
delay(500);
Watchman();
int yrchk=Year();
int daychk=Date();
int mthchk=Month();
String mth=MonthString ();
int hrchk= (Hour())*100;
int minchk= Min();
int schk=(Sec())/100;
int convtime=hrchk+minchk+schk;
Serial.println(convtime);
if (yrchk>=2018 &&(mth=="January"|| mth=="February"|| mth=="March")){
if (convtime>=1810 && convtime<=2359){
Serial.println("First stage-1 loop is running");
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);} // Turning on the relay pin no 11 & 12
else if ((convtime>=100 && convtime<=540)){digitalWrite(11,HIGH);digitalWrite(12,HIGH); // Turning on the relay pin no 11 & 12
Serial.println("First stage-2 loop is running");}
}
else if (yrchk>=2018 &&(mth=="April"|| mth=="May"|| mth=="June"|| mth=="July"|| mth=="August")){
if (convtime>=1830 && convtime<=2359){
Serial.println("second stage-1 loop is running");
digitalWrite(11,HIGH);digitalWrite(12,HIGH);} // Turning on the relay pin no 11 & 12
else if ((convtime>=100 && convtime<=520)){digitalWrite(11,HIGH);digitalWrite(12,HIGH);
Serial.println("second stage-2 loop is running");}
}
else if (yrchk>=2018 &&(mth=="September"|| mth=="October")){
if (convtime>=1750 && convtime<=2359){
Serial.println("Third stage-1 loop is running");
digitalWrite(11,HIGH); // Turning on the relay pin no 11 & 12
digitalWrite(12,HIGH);}
else if ((convtime>=100 && convtime<=540)){digitalWrite(11,HIGH);digitalWrite(12,HIGH);
Serial.println("Third stage-2 loop is running");}
}
else if (yrchk>=2018 &&(mth=="November"|| mth=="December")){
if (convtime>=1735 && convtime<=2359){
Serial.println("Fourth stage-1 loop is running");
digitalWrite(11,HIGH);digitalWrite(12,HIGH);} // Turning on the relay pin no 11 & 12
else if ((convtime>=100 && convtime<=600)){digitalWrite(11,HIGH);digitalWrite(12,HIGH);
Serial.println("Fourth stage-2 loop is running");}
}
else if (yrchk<2018){
//SIM900A.println("AT+CMGF=1");
//delay(1000);
//SIM900A.println("AT+CMGS=\"+8801714206279\"\r"); // Replace x with mobile number
//delay(1000);
//SIM900A.println("Out of date error in DS3231,send instruction");// The SMS text you want to send
//delay(100);
//SIM900A.println((char)26);// ASCII code of CTRL+Z
//delay(1000);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
}
//void eepromWrite(int address, int location,int data)
//{
//Wire.beginTransmission(address);
//Wire.write(location);
//Wire.write(data);
//Wire.endTransmission();
//}
//byte eepromRead(byte location)
//{
//Wire.beginTransmission(ADDRESS);
//Wire.write(location);
//Wire.endTransmission();
//Wire.requestFrom(ADDRESS,25);
//while(!Wire.available()){}
//return Wire.read();
//}
String MonthString ()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x05);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
int i= Wire.read();
String k;
if (i==1){ k="January";};
if (i==2) {k="February";};
if (i==3) {k="March";};
if (i==4) {k="April";};
if (i==5) {k="May";};
if (i==6) {k="June";};
if (i==7) {k="July";};
if (i==8) {k="August";};
if (i==9) {k="September";};
if (i==10){k="October";};
if (i==11){k="November";};
if (i==12){k="December";};
return k;
}
int Month()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x05);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
int i= Wire.read();
return i;
}
byte Min()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,2);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
byte Hour()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
byte Date()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x04);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
int Year()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x06);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value =(tens * 10) + units;
int k= final_value+2000;
return k;
}
String DayString ()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x03);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
int i= Wire.read();
String k;
if (i==1){ k="Monday";};
if (i==2) {k="Tuesday";};
if (i==3) {k="Wednesday";};
if (i==4) {k="Thursday";};
if (i==5) {k="Friday";};
if (i==6) {k="Saturday";};
if (i==7) {k="Sunday";};
return k;
}
byte Sec()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,2);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
byte Temp()
{
Wire.beginTransmission(ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1);
while(!Wire.available()){}
byte bcd_value = Wire.read(); // lets say bcd_value = 55, or 0x37
byte tens = bcd_value >> 4; // tens = 3 (by shifting down the number four places)
byte units = bcd_value & 0x0F; // units = 7 (the 0x0F filters-out the high digit)
byte final_value = (tens * 10) + units;
return final_value;
}
void Watchman()
{
byte hr=Hour();
byte mn=Min();
byte sc=Sec();
byte tm= Temp();
byte dt=Date();
byte mt=Month();
int yr=Year();
String o;
if(hr>12){o=" PM";}
else{o=" AM";}
if (hr>12){hr=Hour()-12;}
lcd.clear();
lcd.setCursor(4,0);
lcd.print(dt);
lcd.print("/");
lcd.print(mt);
lcd.print("/");
lcd.print(yr);
//Serial.println(yr);
lcd.setCursor(4,1);
lcd.print(hr);
lcd.print(".");
lcd.print(mn);
lcd.print(".");
lcd.print(sc);
lcd.print(o);
//lcd.print(",T=");
//lcd.print(tm);
delay(500);
}
//}
void Serialman()
{
byte hr=Hour();
byte mn=Min();
byte sc=Sec();
byte tm= Temp();
byte dt=Date();
byte mt=Month();
int yr=Year();
String o;
if(hr>12){o=" PM";}
else{o=" AM";}
if (hr>12){hr=Hour()-12;}
Serial.print(dt);
Serial.print("/");
Serial.print(mt);
Serial.print("/");
Serial.println(yr);
Serial.print(hr);
Serial.print(".");
Serial.print(mn);
Serial.print(".");
Serial.print(sc);
Serial.print(o);
Serial.print(",T=");
Serial.print(tm);
Serial.println("");}