Arduino – Control via USB and the Approximate Time Clock

My third Arduino project started with a cut and paste of the Physical Pixel tutorial on www.arduino.cc. Several of my Arduino projects will communicate with PCs. I learned how to output from the Arduino to the PC in my second project.

 My second project is fun. I used wired an accelerometer to the Arduino and communicated to a Processing program on a PC. It is rather complicated for me to describe, being a beginner at both Arduino and blogging, so we’ll skip this and talk about project number three for now.

 The Physical Pixel sketch sets the LED at pin 13 high or low, based on the Arduino receiving an H or L over the USB port.  I compiled, clicked on the serial monitor, set it to COM4 and the sketch worked immediately. Such ease, so far, has eluded me during my Arduino/Wiring experimentation.

 Motivated by my immediate success, I started to tweak the code to suit my purposes. For many of my projects, I imagined the Arduino using an “AT” style syntax, like the old Hayes modems or today’s Bluetooth chipsets. However, after reviewing several sketches that polled for input via USB, I saw that they relied on single character commands – as did the Physical Pixel sketch as well.

 Reading between the lines, I’m guessing that the Arduino doesn’t have strong support for string manipulation. Soon I’ll examine the Wiring language reference, as well as both the Messenger library for Arduino by Thomas Ouellet Fredericks, and Tom Igoe’s TextString Library. I am leery about single-character command structures, back in the day I’ve seen plenty of spurious bytes transmit through serial communication channels. If resources allow I’ll create a more robust command structure for use on my projects. If it is interesting, I’ll post it here.

 To get a better feel for the Arduino I started to tweak the Physical Pixel sketch. I made the command set case insensitive. I added an acknowledge feature. Send the Arduino an “A” and it responds with “You Betcha” (I guess I’m not yet over Sara Palin.)

Also, I added a “C” command to read the Arduino clock.  I do not believe that the Atmel 168 has a real time clock; it does have a timer that counts milliseconds. This timer is not accurate like a RTC, but it is “good enough” in many instances. I call this an “Approximate Time Clock”. The Language Reference states: “This number will overflow (go back to zero), after approximately 9 hours and 32 minutes.”  However I’ve been running mine for 15 hours with no overflow (15:19:48.984).

 I hacked a little function to take the millis() count and format it into hh:mm:ss.mil format. This could be useful, for example, if you wanted to display the Arduino run duration on a serial LCD screen, or poll the Arduino for its run duration, as I did in my experiment. I describe the function a hack because it uses global variables and is rife with division operations. There must be a better way. As I wrote this function, I thought back to my days as a C programmer. I wonder how strong typing is in Wiring? Am I creating code with frustrating side effects?

If I continue with this, I could (a) replace the series of if statements with a case statement, (b) add a command to set the time and, (c) output the time of day, rather than elapsed time. So here’s the code:

 

int outputPin = 13;
int val;
long TimeHr;
long TimeMin;
long TimeSec;
long Timems;
 
void CalcTime (long Time)
  {
   TimeSec = Time / 1000;
   Timems = Time % 1000;
   TimeMin = TimeSec /60;
   TimeSec = TimeSec % 60;
   TimeHr = TimeMin / 60;
   TimeMin = TimeMin % 60;
  }
 
void PrintTime(void )
  {
    if (TimeHr < 10) {Serial.print("0");}
    Serial.print(TimeHr); 
    Serial.print(":");
    if (TimeMin < 10) {Serial.print("0");}
    Serial.print(TimeMin);
    Serial.print(":");
    if (TimeSec < 10) {Serial.print("0");} 
    Serial.print(TimeSec);
    Serial.print(".");
    Serial.println(Timems);
 
  }
 
void setup()
{
  Serial.begin(9600);
  pinMode(outputPin, OUTPUT);
}
 
void loop()
{
  if (Serial.available()) {
    val = Serial.read();
    if (val == 'H' or val == 'h') {
      digitalWrite(outputPin, HIGH);
    }
    if (val == 'L' or val == 'l') {
      digitalWrite(outputPin, LOW);
    }
    if (val == 'A' or val == 'a') {
      Serial.println("You Betcha");
    }
    if (val == 'C' or val == 'c') {
      CalcTime(millis());
      PrintTime();
    }
  }
}
Advertisements

~ by ratdad on January 26, 2009.

One Response to “Arduino – Control via USB and the Approximate Time Clock”

  1. I don’t know enough about the compiler’s code generation to know, but what would happen if you replaced longs with floats and the divides with fractional equivalents and multiplied? That is:

    long TimeSec;
    //
    TimeSec = Time / 1000;

    becomes:

    float TimeSec;
    //
    TimeSec = ((float) Time)) * .001;

    I’d be interested in knowing what it does to your speed and code size.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: