Installing "Time" Library?

Dear Arduinoers

as a new comer to the fascinating world of Arduino, this is my first post to the forum since I would like to request some help.

As I am trying to play with my arduino (I just want to have it print the current date & time), I wanted to install the Arduino "Time" library (Arduino Playground - Time). However, when I try to run the example sketch I keep getting errors, and was wondering if someone has seen this, and/or knows if perhaps I am doing something wrong...

So, I followed the instructions in the web page of installing the directories in the "Time.zip" files in the corresponding arduino library folders. Then, when I try to run the example "TimeSerial" sketch, I get the following errors:

Error 1: "The 'BYTE' keyword is no longer supported. As of Arduino 1.0, the 'BYTE' keyword is no longer supported. Please use Serial.write() instead."

ok... no problem... so I go ahead and replace where it says "BYTE" with "Serial.write()". Then I try running the program again, and I get another error:

Error 2: Then I get the following error message:

TimeSerial.cpp: In function 'time_t requestSync()':
TimeSerial.pde:-1: error: no matching function for call to 'HardwareSerial::write()'
C:\arduino-1.0\hardware\arduino\cores\arduino/HardwareSerial.h:58: note: candidates are: virtual size_t HardwareSerial::write(uint8_t)
C:\arduino-1.0\hardware\arduino\cores\arduino/Print.h:50: note: virtual size_t Print::write(const uint8_t*, size_t)
C:\arduino-1.0\hardware\arduino\cores\arduino/Print.h:49: note: size_t Print::write(const char*)

Anyone has encountered this problem too? I am running the Arduino 1.0 software that is available for download from the official arduino web site.

Any insights/suggestions would be very helpful...

Thanks for your help!!!

Lucs

To save us recreating it, can you post your current sketch please? Use code tags.

You can do that by hitting the # button above the posting area.

Hi Nick

certainly! Thanks for your willingness to help... here's the original example (unmodified) sketch as provided in the time library:

/* 
 * 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 2010
 T1262347200  
 *
 * A Processing example sketch to automatically send the messages is inclided in the download
 */ 
 
#include <Time.h>  

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#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);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
  if(timeStatus()!= timeNotSet)   
  {
    digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh  
    digitalClockDisplay();  
  }
  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() {
  // 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
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }  
  }
}

time_t requestSync()
{
  Serial.print(TIME_REQUEST,BYTE);  
  return 0; // the time will be sent later in response to serial mesg
}

Change that final function to read:

time_t requestSync()
{
  Serial.write(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}

Thank you, Nick! Interesting that all it took to get the script running is your change from "serial.print" to "serial.write"...

one last question... if I wanted to have the PC automatically tell the arduino to sync with the PC's time whenever I upload the sketch to the arduino, can I do this using this sketch? And if so, would the arduino be able to keep this new time info even if I was to disconnect it from the PC?

You can incorporate the compile time into the sketch, although having it know whether to use it or not might be tricky. I suggest looking for one of the cheap clock add-on boards that have a battery backup. Search for DS1307 on this site or the Web.

sounds good. Thanks!

Hi! I am a student of British Colombia Institute of Technology Mechanical Technology program. I am currently doing a project on a automated hydroponics appliance and I am using arduino as my controller.

I want to know if I can request the PC time once and use it for the rest of my program. Can I just use the request part of the sample sketch in the void setup() function to request the PC time? The prototype that my team is working on will have the arduino coupled to a Raspberry Pi at all times. So even if the power goes down, the time reference should remain constant as the Rasp Pi has actual storage memory. Also, there will be quite a few serial devices that we're using, so Serial RTC is really not an option.