Go Down

Topic: real time clock (Read 980 times) previous topic - next topic


I can't get the datetime clock to work properly. It was downloaded as a DateTime.zip file and had included examples for a datetime progam and  processing application.
In the datetime program:
DateTime.sync(0) works,
DateTime.now() works, the date/time ticks over every second,
Serial.available returns 0,
getPCtime() returns 0,
in other words the clock doesn't synchronise to the time shown on the PC.

How do you use the processing example in DateTime.zip?
It is a .pde file but won't compile and produces a lot of errors.
I downloaded code::block and tried it as a .cpp file but it won't compile
and produces a lot of errors.
My arduino uses port13.
What am I missing? I seem to be blundering around in the dark. :smiley-eek:

Nick Gammon

Read this before posting a programming question

Lots of errors, huh? Such as? And your code that produces these errors?
Please post technical questions on the forum, not by personal message. Thanks!

More info:


How do you use the processing example in DateTime.zip?

You have installed Processing?



Here is SetArduinoClock.pde as obtained from DateTime.zip:
* SetArduinoClock.
* portIndex must be set to the port connected to the Arduino
* Clicking the window sends the current time to the arduino
* as the string value of the number of seconds since Jan 1 1970

* The clock has two second hands, the white one is the Arduino time
* the red one is the local time, if the time is exactly the same
* you will only see the red hand, although due to round trip serial delays
* the Arduino clock may be one second behind.

import processing.serial.*;

Serial myPort;     // Create object from Serial class
public static final short LF = 10;     // ASCII linefeed
public static final short TIME_HEADER = 255; //header byte for arduino serial time message
public static final short FONT_SIZE = 12;
public static final short portIndex = 0;  // select the com port, 0 is the first port
color PCClock = color(153, 51, 0);
PFont fontA;
long prevTime=0; // holds value of the previous time update

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

  fontA =  createFont("Arial", FONT_SIZE); 
  textSize( FONT_SIZE);
  text("Mouse click to set Arduino time", 5,10);

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;

void mousePressed() { 
  String time = String.valueOf(getTimeNow()); 
  char header = 0xff;
  myPort.write(header);  // send header and time to arduino

void serialEvent(Serial p) {
  String inString = myPort.readStringUntil(LF);
  if(inString != null && inString.length() >= 13 && inString.charAt(0) == TIME_HEADER) {   
    int val = 0;
    long time = 0;
    for(int i = 1; i <= 10; i++)       
      time = time * 10 + (inString.charAt(i) - '0');
  else if(inString != null && inString.length() > 0 )     
    println(inString);  // display serial input that is not a time messgage

void UpdateClock( long t){
  int sec, min, hr;
  Long time = new Long(t);
  sec = time.intValue() % 60;
  min = (time.intValue() / 60) % 60 ;
  hr =  (time.intValue() % 86400) / 3660;   
  UpdateClock(sec, min, hr );   

void UpdateClock( int sec, int min, int hour){       
  // Angles for sin() and cos() start at 3 o'clock;
  // subtract HALF_PI to make them start at the top
  ellipse(100, 100, 160, 160);
  float s = map(sec, 0, 60, 0, TWO_PI) - HALF_PI;
  float m = map(min, 0, 60, 0, TWO_PI) - HALF_PI;
  float h = map(hour % 12, 0, 12, 0, TWO_PI) - HALF_PI;
  line(100, 100, cos(s) * 72 + 100, sin(s) * 72 + 100);
  line(100, 100, cos(m) * 60 + 100, sin(m) * 60 + 100);
  line(100, 100, cos(h) * 50 + 100, sin(h) * 50 + 100); 
  // now show the seconds on the computer running this app
  s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
  stroke(PCClock);  // another second hand in red to indicate Java time
  line(100, 100, cos(s) * 72 + 100, sin(s) * 72 + 100);

void draw() {

There 35 errors: the first one is:
variable or field 'serialEvent' declared void
There 34 others but I can't use copy and paste to get them.I have not been able to compile because of the erros.What does installed the processing mean?


The Processing code you posted compiles perfectly.

You're not trying to compile it in the Arduino IDE are you?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


1. Go to http://processing.org/
2. Click on Download Processing.
3. Click on appropriate OS.
4. Follow instructions at http://processing.org/learning/gettingstarted/


Thank you to dxw00d, AWOL and nick gammon about compiling setclockarduino using processing.org,
thank you very much, it works fine now. For others reading this, when it asks you for your port number in the program (before you compile it), don't put the actual port number in :smiley-roll: but rather the number of your port in the list. e.g 0=port4 and 1=port13 in my case. So I had to put 1 in, instead of 13.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131