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 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;
}
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.
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.
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
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 ?
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?
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?