Go Down

Topic: Arduino Clock: Setting Time (Read 10 times) previous topic - next topic

bugzy4real

#20
Apr 11, 2009, 12:04 am Last Edit: Apr 11, 2009, 01:39 am by bugzy4real Reason: 1
mem: Can you tell me with syntax how I can send two numbers (h and m) using headers over the serial port?
Also, where in the arduino code below is the time header retrieved from processing? I'm asking so i know how to modify the code. sorry i'm having you baby-sit me thru the process

//arduino code
#include <DateTime.h>
#include <DateTimeStrings.h>

#define TIME_MSG_LEN  11   // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

void setup(){
 Serial.begin(19200);
}

void  loop(){  
 getPCtime();   // try to get time sync from pc        
 if(DateTime.available()) { // update clocks if time has been synced
   unsigned long prevtime = DateTime.now();
   while( prevtime == DateTime.now() )  // wait for the second to rollover
     ;
   DateTime.available(); //refresh the Date and time properties
   digitalClockDisplay( );   // update digital clock

   // send our time to an app listening on the serial port
   Serial.print( TIME_HEADER,BYTE); // this is the header for the current time
   Serial.println(DateTime.now());      
 }
}

void getPCtime() {
 // if time available from serial port, sync the DateTime library
 while(Serial.available() >=  TIME_MSG_LEN ){  // time message
   if( Serial.read() == TIME_HEADER ) {        
     time_t pctime = 0;
     for(int i=0; i < TIME_MSG_LEN -1; i++){  
       char c= Serial.read();          
       if( c >= '0' && c <= '9')  
         pctime = (10 * pctime) + (c - '0') ; // convert digits to a number            
     }  
     DateTime.sync(pctime);   // Sync DateTime clock to the time received on the serial port
   }  
 }
}

void digitalClockDisplay(){
 // digital clock display of current time
 Serial.print(DateTime.Hour,DEC);  
 printDigits(DateTime.Minute);  
 printDigits(DateTime.Second);
 Serial.print(" ");
 Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
 Serial.print(" ");  
 Serial.print(DateTimeStrings.monthStr(DateTime.Month));  
 Serial.print(" ");
 Serial.println(DateTime.Day, DEC);  
}

void printDigits(byte digits){
 // utility function for digital clock display: prints colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits,DEC);  
}

mem

#21
Apr 11, 2009, 09:09 am Last Edit: Apr 11, 2009, 09:10 am by mem Reason: 1
You can see how to send Data to Arduino by studying the Processing Serial examples. There is Arduino code at the end of the Processing example sketches. In Processing, select  : Files/Examples/Libraries/Serial/


I have added some more comments to help clarify how the Arduino sketch gets the time.
Code: [Select]
void getPCtime() {
// if time available from serial port, sync the DateTime library
// a time message consists of a header followed by ten digits (TIME_MSG_LEN equals 11)
while(Serial.available() >=  TIME_MSG_LEN ){  // time message
// you make sure you have the start of the message by checking the header
  if( Serial.read() == TIME_HEADER ) {        
    // header was found so get the next ten digits and convert to a number
    time_t pctime = 0;
    for(int i=0; i < TIME_MSG_LEN -1; i++){  
      char c= Serial.read();          
      if( c >= '0' && c <= '9')  
        pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
// the ASCII character is converted to a digit by subtracting '0' which has an ASCII value 48.
// So, if ch equals '1', its ASCII value is 49. 49- '0' is the same as 49-48 and they both equal 1,
// which is the numeric value of the character '1'        
    }  
    // the next method sets the clock with the pc time received above
    DateTime.sync(pctime);   // Sync DateTime clock to the time received on the serial port
  }  
}
}

bugzy4real

I think my case is different. So, parsing the data the same way  you do as the time clock shouldn't be the same way. I guess the question now is how do you parse the data since i'm sending two integers over the port. The processing code below is wat i'm using to send the data to the arduino, how would you parse it in arduino? Thanks!

//processing code
port.write(header2);  //writes the header first
port.write(h);
port.write(m);

mem

if the header, h, and m are all chars then you don't need to parse. read a byte in arduino and if its the header you can then read h and m directly.

bugzy4real

#24
Apr 13, 2009, 09:47 pm Last Edit: Apr 13, 2009, 11:25 pm by bugzy4real Reason: 1
Mem: Thanks a lot!!!

Go Up