I took the original example "TimeSerialDateStrings" and first just added the LCD_display part as follow:
/*
-
TimeSerialDateStrings.pde
-
example code illustrating Time library date strings
-
-
This sketch adds date string functionality to TimeSerial sketch
-
Also shows how to handle different messages
-
-
A message starting with a time header sets the time
-
A Processing example sketch to automatically send the messages is inclided in the download
-
On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
-
-
A message starting with a format header sets the date format
-
send: Fs\n for short date format
-
send: Fl\n for long date format
*/
#include <TimeLib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// single character message tags
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define FORMAT_HEADER 'F' // Header tag indicating a date format message
#define FORMAT_SHORT 's' // short month and day strings
#define FORMAT_LONG 'l' // (lower case l) long month and day strings
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
LiquidCrystal_I2C lcd(0x27,16,4); // set the LCD address to 0x27 or 0x20
static boolean isLongFormat = false;
volatile int Start=0, Up=0, Down=0, Settup=0, OK=0;
void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
lcd.begin(); // Print a message to the LCD.
lcd.backlight();
// setSyncProvider( requestSync); //set function to call when sync required
lcd.setCursor(0,0);
lcd.print("set the Date");
}
void loop(){
if (Serial.available() > 1) { // wait for at least two characters
char c = Serial.read();
if( c == TIME_HEADER) {
processSyncMessage();
}
else if( c== FORMAT_HEADER) {
processFormatMessage();
}
}
if (timeStatus()!= timeNotSet) {
digitalClockDisplay();
}
delay(1000);
}
void digitalClockDisplay() {
// digital clock display of the time
lcd.setCursor(0,2);
Serial.print(hour());
lcd.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
lcd.setCursor(0,1);
if(isLongFormat)
{
Serial.print(dayStr(weekday()));
lcd.print(dayStr(weekday()));
}
else
{
Serial.print(dayShortStr(weekday()));
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
lcd.print(dayShortStr(weekday()));
lcd.print(" ");
lcd.print(day());
lcd.print(" ");
}
if(isLongFormat)
{
Serial.print(monthStr(month()));
lcd.print(monthStr(month()));
}
else
{
Serial.print(monthShortStr(month()));
Serial.print(" ");
Serial.print(year());
Serial.println();
lcd.print(monthShortStr(month()));
lcd.print(" ");
lcd.print(year());
//Serial.println();
}
}
void printDigits(int 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);
lcd.print(digits);
}
void processFormatMessage() {
char c = Serial.read();
if( c == FORMAT_LONG){
isLongFormat = true;
Serial.println(F("Setting long format"));
}
else if( c == FORMAT_SHORT) {
isLongFormat = false;
Serial.println(F("Setting short format"));
}
}
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1546344000; // Jan 1 2019
pctime = Serial.parseInt();
if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
time_t requestSync() {
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
void setTime_HMS()
{
Serial.print("enter the current time in the following format Hour Minutes Seconde");
}
Since I didn't want to set the time via the Serial monitor but with two Up and Dawn Button, I began to comment out and deleted everything I didn't need. I was compiling it at each step and the time was still appearing on the LCD and counting up.
Here is my ProcessSyncMessage() function.
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1546344000; // Jan 1 2019
pctime = DEFAULT_TIME; //Serial.parseInt();
if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
I set pctime to a constant since I wanted to transform it in the given time later.
Here is the function with the issue:
void time_serial(){
/* char c = 'T';// Serial.read();
if( c == TIME_HEADER) {*/
processSyncMessage();
// }
if (timeStatus()!= timeNotSet) {
digitalClockDisplay();
time_t T= now();
lcd.setCursor(0,3);
lcd.print(T);
}
delay(1000);
}
This basically the main function in the original example that I transformed in a void function and call it in the my main function.
if I use the Serial.read() and just send the single character 'T', everything work fine. the set default time will appear and the time will be incremented every second. But if I comment out this line with Serial, the time will not count up anymore. it just stays static at the default time (which I set to 01 Jan 2019).
I knew that the function now() was given a time_t variable but just to see if the value of now will be update every second. this is the case when the serial.read command is there and it won't be update if I comment it out.
any advice?
Tades