ARDUINO time library example not working

I'm trying to make a clock on my GLCD and I'm using the arduino library found here (Arduino Playground - HomePage).
How ever i want to try the demo sketch that shows how to sync the clock to to an external source like the serial port . I tried compiling the example in Time/Examples/Processing/SyncArduinoClock and it returns the error on line 14

'import' does not name a type

Can someone tell me what need to be changed to make it work? Thank you in advance.

Here is the unchanged example code. I also included the library from the Arduino site at the bottom

/**

  • SyncArduinoClock.
  • portIndex must be set to the port connected to the Arduino
  • The current time is sent in response to request message from Arduino
  • or by clicking the display window
  • The time message is 11 ASCII text characters; a header (the letter 'T')
  • followed by the ten digit system time (unix time)
    */

import processing.serial.*;

public static final short portIndex = 1; // select the com port, 0 is the first port
public static final char TIME_HEADER = 'T'; //header byte for arduino serial time message
public static final char TIME_REQUEST = 7; // ASCII bell character
public static final char LF = 10; // ASCII linefeed
public static final char CR = 13; // ASCII linefeed
Serial myPort; // Create object from Serial class

void setup() {
size(200, 200);
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this,Serial.list()[portIndex], 9600);
}

void draw()
{
if ( myPort.available() > 0) { // If data is available,
char val = char(myPort.read()); // read it and store it in val
if(val == TIME_REQUEST){
long t = getTimeNow();
sendTimeMessage(TIME_HEADER, t);
}
else
{
if(val == LF)
; //igonore
else if(val == CR)
println();
else
print(val); // echo everying but time request
}
}
}

void mousePressed() {
sendTimeMessage( TIME_HEADER, getTimeNow());
}

void sendTimeMessage(char header, long time) {
String timeStr = String.valueOf(time);
myPort.write(header); // send header and time to arduino
myPort.write(timeStr);
}

long getTimeNow(){
// java time is in ms, we want secs
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
int tzo = cal.get(Calendar.ZONE_OFFSET);
int dst = cal.get(Calendar.DST_OFFSET);
long now = (cal.getTimeInMillis() / 1000) ;
now = now + (tzo/1000) + (dst/1000);
return now;
}

DateTime.zip (9.73 KB)

The code that you posted looks like it is for Processing rather than the Arduino. Where are you having the problem, Processing or Arduino ?

Thank you for the reply. I want the example code for the time library that shows the time being synced with the serial port. So that i can sync the time by connecting it to the serial port.

Have you looked at the examples available in the IDE ? TimeSerial seems to be the one that you want for the Arduino.

The TimeSerial example in the arduino time library returns a error on compile.

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

error is "The keyword BYTE is no longer supported"

I tried changing it to byte, but it gives me the error
expected primary expression before token')'

does anyone know how to fix this library?

does anyone know how to fix this library?

Yes. Delete the BYTE keyword. If necessary, and it's impossible to tell since you only posted a snippet, use a cast in front of the variable to be printed.

  Serial.print((byte)TIME_REQUEST);

What does the full error message say ? Does it give you any indication of how to solve the problem ?
It does here using 1.0.5

I did not know arduino tells you how to fix errors.

I uploaded a picture of the error. I then changed the line to serial.write((byte)TIME_REQUEST) and it compiles, but doesn't sync or show the time in the serial monitor, just says waiting for sync message.

I tried putting the digitalClockDisplay(); funtion in the void loop to see what time it shows and it shows that the clock is working starting from 00:00 0 0 1970 and goes up. Also the set time command works too

Time serial error.jpg

Time.rar (18.1 KB)

I did not know arduino tells you how to fix errors.

For most errors it can't, but in this case you were lucky because the program writers had anticipated a problem when they changed the IDE so were able to provide a specific message.

You only need to changeSerial.print(TIME_REQUEST,BYTE);
toSerial.write(TIME_REQUEST);There is no need to cast the variable to a byte. Serial.write() sends bytes anyway.

As to your current problem, what is running on the PC to provide the time synchronisation that the Arduino is looking for ?

UKHeliBob:

As to your current problem, what is running on the PC to provide the time synchronisation that the Arduino is looking for ?

I don't know. I thought that part was automatic and it was getting it from the computer's clock. What do i need to do to send it a sync time?

What do i need to do to send it a sync time?

Processing running the program that you posted in your original message looks a likely candidate.

In the time library from arduino there is a bunch of sketches.

SyncArduinoClock (the one from my first post)
and
TimeSerial (the one from my last post)

TimeSerial compiles, but doesn't sync the time. SyncArduinoClock doesn't compile and throws the error 'import' does not name a type. I haven't written any code. I was hoping that with this library I could call some function and sync and display the time. Is that what this library was built for? If so which sketch do i use as example to sync and display the time and what are the other ones for? I posted the error in the picture and have a zip of the library. Should i be using this is there something newer that i should be using or is the newest one but needs fixing?

Time.rar (18.1 KB)

We seem to be going round in circles.

SyncArduinoClock is a program that runs on the PC using Processing, which is a programming environment like the Arduino IDE.
TimeSerial is an Arduino program.
In order to synchronise the time on the Arduino with the PC both of them must be running.

I see. That clears things up. So I upload the timeserial to the arduino and then I open the SyncArduinoClock sketch and show the serial monitor. But it keeps showing the time starting from 0:00:00 1 1 1970 is it suppose to do that? It doesn't sync the time from the computer's clock? (I also edited SyncArduinoClock with the correct port )

So is there a way to have a arduino sync without a program running on the computer?

So is there a way to have a arduino sync without a program running on the computer?

No

Do you know why the Sync Arduinoclock/time serial sketch isn't syncing the time ?

Do you know why the Sync Arduinoclock/time serial sketch isn't syncing the time ?

Would you like an answer, or a link to an answer. Post your code if you want an answer.

Make CERTAIN that the port that the Processing app is talking to is the one the Arduino is talking to.

Yigiter007:
So is there a way to have a arduino sync without a program running on the computer?

If your sketch needs to know the time, you can use something like one of these:

They will allow you to keep time without being connected to a computer.

Do you have any idea on how to fix the sketches so that it actually syncs the time?

Do you have any idea on how to fix the sketches so that it actually syncs the time?

Yes, I do. Hope that helps.

If not, then I suggest that you post the Arduino code, and show any serial output, and post the Processing code and show any console output.