Here is my sketch.
When the date/time go bad it is no longer connected to my computer, but is already running and working fine.
/*
* A simple sketch that uses WiServer to send a tweet with the current system time every 5 minutes
*/
#include <DateTime.h>
#include <DateTimeStrings.h>
#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(4, 3, 8, 7, 6, 5);
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 255 // Header tag for serial time sync message
char temp_val[3];
char temps[3] ="";
char* weather = " ";
int inPin = 5; // select the input pin for analog temp value 5= out, 4 = in.
int inVal;
int i;
int samples[8];
int t=0;
int ti=0;
char n[6] = " ";
int x=0;
char* place=" ";
int r;
int inVal3=0;
int samples2[8];
int counter=0;
int custom_0[] = {0x0e, 0x0e, 0x04, 0x0e, 0x1b, 0x0a, 0x0e, 0x11};
// Define a custom char in lcd
int defineCharacter(int ascii, int *data) {
int baseAddress = (ascii * 8) + 64;
// baseAddress = 64 | (ascii << 3);
lcd.command(baseAddress);
for (int i = 0; i < 8; i++)
lcd.write(data[i]);
lcd.command(128);
return ascii;
}
void setup() {
Serial.begin(57600);
defineCharacter(0, custom_0);
lcd.begin(16,2);
lcd.print("Robo Temp 3000");
lcd.setCursor(0,1);
lcd.print((char)229);
lcd.print(" ");
lcd.print((char)224);
lcd.print(" ");
lcd.print((char)225);
lcd.print(" ");
lcd.print((char)226);
lcd.print(" ");
lcd.print((char)227);
lcd.print(" ");
lcd.print((char)218);
lcd.print(" ");
lcd.write(0);
delay(2000);
}
void TempWrit()
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print(place);
lcd.print(" temp ");
lcd.print(x);
lcd.print((char)223);
lcd.print(" F.");
lcd.setCursor(0,1);
lcd.print(weather);
delay(2500);
// lcd.setCursor(16,1);
//lcd.autoscroll();
// for (int thisChar = 0; thisChar < 16; thisChar++) {
// lcd.print(" ");
// delay(400);
// }
lcd.clear();
// lcd.noAutoscroll();
}
void WeatherOut()
{
inPin = 5;
for(i = 0;i<=7;i++) // gets 8 samples of temperature
{
samples[i]= ( 5.0 * analogRead(inPin) * 100.0) / 1024.0;
inVal = inVal + samples[i];
}
inVal = inVal/8.0; // better precision
t = ((inVal * 9)/ 5 + 32);
Serial.print("Weather out ");
Serial.println(t);
inVal = 0; //reset variable
x=t;
itoa(t,temp_val,10);
place="Out";
}
void WeatherIn()
{
inPin = 4;
for(i = 0;i<=7;i++) // gets 8 samples of temperature
{
samples[i]= ( 5.0 * analogRead(inPin) * 100.0) / 1024.0;
inVal = inVal + samples[i];
}
inVal = inVal/8.0; // better precision
ti = ((inVal * 9)/ 5 + 32);
inVal = 0; //reset variable
x=ti;
itoa(ti,temp_val,10);
place="In";
}
void GetSun() {
// photocellReading = analogRead(3);
for(i = 0;i<=7;i++) // gets 8 samples of temperature
{
samples2[i]= (analogRead(3));
inVal3 = inVal3 + samples2[i];
}
inVal3 = inVal3/8.0; // better precision with 8 samples
if (inVal3 < 8) {
weather = "Night Time";
} else if (inVal3 < 110) {
weather = "Cloudy";
} else if (inVal3 < 200) {
weather = "Partly Cloudy";
} else if (inVal3 < 280) {
weather="Partly Sunny";
} else {
weather = "Sunny";
}
Serial.print("Sun level ");
Serial.print(inVal3);
Serial.print(" ");
Serial.println(weather);
}
void loop()
{
//update time
unsigned long prevtime;
if( getPCtime()) { // try to get time sync from pc
Serial.print("Clock synced at: ");
Serial.println(DateTime.now(),DEC);
}
if(DateTime.available()) { // update clocks if time has been synced
digitalWrite(13,LOW); // first flash the LED
prevtime = DateTime.now();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;
DateTime.available(); //refresh the Date and time properties
// send our time to any app at the other end of the serial port
Serial.print( TIME_HEADER,BYTE); // this is the header for the current time
Serial.println(DateTime.now());counter++;
}
if (counter>=4)
{
lcd.clear();
lcd.print ("Robo Temp 3000");
lcd.setCursor(0,1);
lcd.print((char)229);
lcd.print(" ");
lcd.print((char)224);
lcd.print(" ");
lcd.print((char)225);
lcd.print(" ");
lcd.print((char)226);
lcd.print(" ");
lcd.print((char)227);
lcd.print(" ");
lcd.print((char)218);
lcd.print(" ");
lcd.write(0);
counter=0;
delay(2000);
lcd.clear();
}
WeatherOut();
GetSun();
TempWrit();
WeatherIn();
weather=" ";
TempWrit();
digitalClockDisplay( ); // update digital clock
}
boolean getPCtime() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
if( Serial.read() == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
char c= Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
DateTime.sync(pctime); // Sync Arduino clock to the time received on the serial port
return true; // return true if time message received on the serial port
}
}
return false; //if no message return false
}
void digitalClockDisplay(){
lcd.clear();// digital clock display of current date and time
Serial.print(DateTime.Hour,DEC);
lcd.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
Serial.print(" ");
lcd.print(" ");
Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
Serial.print(" ");
lcd.print(" ");
Serial.print(DateTimeStrings.monthStr(DateTime.Month));
lcd.setCursor(0,1);
lcd.print(DateTimeStrings.monthStr(DateTime.Month));
Serial.print(" ");
lcd.print(" ");
Serial.println(DateTime.Day,DEC);
lcd.print(DateTime.Day,DEC);
delay(2500);
}
void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
lcd.print(":");
if(digits < 10)
{
Serial.print('0');
lcd.print('0');
}
Serial.print(digits,DEC);
lcd.print(digits,DEC);
}