Pages: [1]   Go Down
Author Topic: real time clock  (Read 672 times)
0 Members and 1 Guest are viewing this topic.
australia
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question

Lots of errors, huh? Such as? And your code that produces these errors?
Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do you use the processing example in DateTime.zip?
You have installed Processing?

http://processing.org/
Logged

australia
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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(Serial.list());
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this,Serial.list()[portIndex], 19200);

  fontA =  createFont("Arial", FONT_SIZE); 
  textFont(fontA);
  textSize( FONT_SIZE);
  stroke(255);
  smooth();
  background(0);
  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
  myPort.write(time);   
}

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');
    UpdateClock(time);
  }         
  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){       
  fill(80);
  noStroke();
  // 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;
  stroke(255);
  strokeWeight(1);
  line(100, 100, cos(s) * 72 + 100, sin(s) * 72 + 100);
  strokeWeight(2);
  line(100, 100, cos(m) * 60 + 100, sin(m) * 60 + 100);
  strokeWeight(4);
  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;
  strokeWeight(1);
  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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Processing code you posted compiles perfectly.

You're not trying to compile it in the Arduino IDE are you?
Logged

"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.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

australia
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: