Time library example Time Serial is not working.

Hi all,

I'm trying to print current time as serial data, but I'm wondering why the Time Serial example is not working.

When I ran the code, LED 13 is off and serial data is just a line of "Waiting for sync message".

I got the code from GitHub - PaulStoffregen/Time: Time library for Arduino
(directed from Arduino Playground - HomePage)

Below is the code


/* 
 * TimeSerial.pde
 * example code illustrating Time library set through serial port messages.
 *
 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
 T1357041600  
 *
 * A Processing example sketch to automatically send the messages is included in the download
 * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
 */ 
 
#include <Time.h>  

#define TIME_HEADER  "T"   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  pinMode(13, OUTPUT);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if (Serial.available()) {
    processSyncMessage();
  }
  if (timeStatus()!= timeNotSet) {
    digitalClockDisplay();  
  }
  if (timeStatus() == timeSet) {
    digitalWrite(13, HIGH); // LED on if synced
  } else {
    digitalWrite(13, LOW);  // LED off if needs refresh
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


void processSyncMessage() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if(Serial.find(TIME_HEADER)) {
     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
}

you need to type in the Unix_time like this T1435489800 in your Serial Monitor input

This is the online conversion Online Conversion - Unix time conversion

Thank you for the kind reply. For some reason, overlooked that part.

I was actually trying to put the real time on my sensor reading. For example, my serial data to be printed like

"11:50:35 Sensor 1: ** Sensor 2: ## Sensor 3: %%"
"11:50:36 Sensor 1: && Sensor 2: @@ Sensor 3: **"

But the sensor start reading shortly after the code is uploaded.

To print the time like that, do I need additional hardware? Could you give me any suggestion?

Thank you,

corejae

How can I input the current Unix_Time, because It is not working for my project

How can I input the current Unix_Time

Type it in?

Your question is missing details. Which mechanism are you using? Serial? Buttons? Network? ...?

To get help, post your code. First read point #7 in How to use this forum - please read. so you know how to properly post code.

because It is not working for my project

It isn't working is the worst possible problem description that you can give. What do you want it to do? What does it do?