Clock and/or time functions

Hi there,

How can I access the clock on sparki, either to get the current time or measure elapsed time? the delay(…) function shows you can access the on board clock. How can I, for example, measure how many seconds have passed since Sparki was turned on?

Have had a bit of a google on the arduino site, but none of the date/time libraries seem to be available and not sure about the lower level stuff.

Correct me if I’m wrong, I do not think there is a real time clock (RTC) function on board the Sparki. You will have to connect an external RTC module to the Expansion connector if a precise clock/timer is needed.

Just to answer your question, here’s a simple timer code using the millis() function in Arduino…of course it can be improve upon. The millis() function is only valid while Sparki is on, once powered off the clock starts at 0:0:0 when the program is resumed.

[code]
/*******************************************
Basic millis() test

Show the time since the program started

********************************************/
#include <Arduino.h> // include the arduino library
#include <Sparki.h> // include the sparki library

int secs, mins, hrs;
void setup()
{
}

void loop()
{
sparki.clearLCD();
secs = millis()/1000;
mins = secs/60;
hrs = secs / 3600;
mins = mins - hrs60;
secs = secs - mins
60;
sparki.print("hours: ");
sparki.println(hrs); // prints hrs since program started
sparki.print("minutes: ");
sparki.println(mins); // prints mins since program started
sparki.print("seconds: ");
sparki.println(secs); // prints secs since program started
// wait a second so as not to send massive amounts of data
delay(1000);
sparki.updateLCD();
}[/code]

here’s a version with time shown in “0:00:00” format…

/*******************************************
 Basic millis() test
 
 Show the time since the program started

********************************************/
#include <Arduino.h> // include the arduino library
#include <Sparki.h> // include the sparki library

int secs, mins, hrs;
void setup()
{ 
}

void loop()
{
    sparki.clearLCD();

    secs = millis()/1000;  // seconds
    mins = secs / 60;
    hrs = secs / 3600;
    secs = secs - mins*60;
    mins = mins - hrs*60;    

    sparki.print("Time:  ");    
    sparki.print(hrs); // prints hrs since program started
    printDigits(mins);
    printDigits(secs);
    sparki.println();
// wait a second so as not to send massive amounts of data
    delay(1000);
    sparki.updateLCD();
}
//
// utility function for digital clock display format- 0:00:00
//
void printDigits(int digits) {
  sparki.print(":");
  if(digits < 10)
    sparki.print('0');
  sparki.print(digits);
}

millis() - aha! Perfect. Thanks ortsac!