Step 1 – Talking to the Arduino

When I got my Arduino, I also got a Proto Shield from Adafruit.  This is an extremely handy shield to get for quickly mucking around with stuff.  It’s especially handy for people like me who suck at soldering.  One problem: it’s a kit and requires that you solder it together.  And of course my soldering skillz bit me in the ass.  While playing around with the board and some code, I simply couldn’t get my LED to light the green pin.  I spent an hour or so debugging my code and wiring, only to find out that I had a poor soldering joint on one of my pins!  Suck.  After resoldering that one pin, I was back in business.

In the last post, I mentioned the “protocol” that I want to use to send color info to the Arduino and make the RGB LED light purty colors.  My goal was to rewrite fade.pde to take in serial input, parse out the color info, and then build a state machine so that the Arduino would be the right color and either throb or be solid.  After a few revisions, here is what I ended up with for my throb() method:

   1: void throb(int redVal, int greenVal, int blueVal)
   2: {
   3:  float maxVal = 255;
   5:  for(i=0; i<255; i++)
   6:  {
   7:    analogWrite(LED_RED, (int)(i*(redVal/maxVal)));
   8:    analogWrite(LED_GREEN, (int)(i*(greenVal/maxVal)));
   9:    analogWrite(LED_BLUE, (int)(i*(blueVal/maxVal)));
  10:    delay(10);
  11:  }
  13:  delay(10); 
  15:  for(i=255; i>0; i--)
  16:  {
  17:    analogWrite(LED_RED, (int)(i*(redVal/maxVal)));
  18:    analogWrite(LED_GREEN, (int)(i*(greenVal/maxVal)));
  19:    analogWrite(LED_BLUE, (int)(i*(blueVal/maxVal)));
  20:    delay(5);
  21:  }  
  22: }


I set up the following #defines for states:

   1: #define STATE_RED  0
   2: #define STATE_BLUE  1
   3: #define STATE_GREEN  2
   4: #define STATE_YELLOW  3
   6: #define OPTION_SOLID 0x0000
   7: #define OPTION_THROB 0x0001
   8: #define OPTION_BLINK 0x0010


And finally, the FSM:

   1: void loop()
   2: {
   3:   if (Serial.available()>0)
   4:   {
   5:     serialVal =;
   7:     // Required to prevent corruption in the buffer. No idea why.
   8:     delay(10);  
  10:     if (serialVal == '#')
  11:     {
  12:       while(idx<8)
  13:       {
  14:        buffer[idx] =;
  15:        idx++;
  16:       }
  18:       idx = 0;
  20:       // hex2dec() stolen from the book Getting Started with Arduino
  21:       rCol = hex2dec(buffer[1]) + hex2dec(buffer[0])*16;
  22:       gCol = hex2dec(buffer[3]) + hex2dec(buffer[2])*16;
  23:       bCol = hex2dec(buffer[5]) + hex2dec(buffer[4])*16;
  24:       option = hex2dec(buffer[7]) + hex2dec(buffer[6])*16;
  26: #ifdef _DEBUG      
  27:       Serial.print("  red=");
  28:       Serial.println(rCol,HEX);
  29:       Serial.print("  green=");
  30:       Serial.println(gCol,HEX);
  31:       Serial.print("  blue=");
  32:       Serial.println(bCol,HEX);
  33:       Serial.print("  option=");
  34:       Serial.println(option,HEX);
  35: #endif
  37:     }
  38:   }
  40:   // Finite state machine to control LED behavior
  41:   switch (option)
  42:   {
  43:     case OPTION_SOLID:
  44:       solid(rCol, gCol, bCol);
  45:       break;
  46:     case OPTION_THROB:
  47:       throb(rCol, gCol, bCol);
  48:       break;
  49:     default:
  50:       option = OPTION_SOLID;
  51:   }
  52: }

I’m pretty happy with it.  And it works great!  Finally.  You see, I had a major problem where the data coming in off the wire was getting corrupted.  Note that I’m testing this through the Arduino IDE’s serial monitor.  Although the initial “#” was properly detected, I would always get complete garbage for the subsequent bytes in the buffer.  (BTW, this explains the stuff within the #ifdef _DEBUG” block.)  After adding a delay(10) after the, it magically started working like a champ.  The kick in the ass is that I couldn’t find another example of anyone requiring this.  Could this be a problem with my USB port?  Is anyone else seeing this behavior.

But there it is – quicker progress than I expected.  The Arduino is now in a state where I can just write an arbitrary app to bitbang a color value and make the LED light up.  I’ll try to post a video when I get a chance.  I’ll also try to put the code up on codeplex or the site.  Next steps: build a small WPF app to sniff the OCS status change event (I’m probably going to start off with the example provided by the Office Communicator SDK Wrapper and go from there).

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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