Arduino

All posts tagged Arduino

After seeing this post last year about hacking GE Color Effects LED lights, I knew I had to get my hands on these.  Basically, they are the most awesome holiday lights ever.  They are pretty cool right out of the box, but with the protocol reverse-engineered, the possibilities are pretty much endless.  It didn’t take long for an Arduino port to surface and over the past year people have made good use of the fully addressable nature of these strings. I ran out and picked up some of the last strings available last season just in case GE decided to not make them again and was happy to see they are on sale again this year.

I’ve been playing with them on and off over the past few months and they are almost ready to make their debut for this holiday season.  In the process of creating software to control these light strings, I decided to stick with the Arduino platform since I’m pretty familiar with it now and it was quick and easy to implement.  The original code and the Arduino port worked well, but I needed a more modular implementation of the light driving code.  I ultimately created an Arduino library that simplifies the use of these lights.  It is based on those original code examples yet neatly wrapped into a library. I wanted to share it in case others were interested in playing with these awesome lights.

You can grab the library here: GE Color Effects Arduino Library (or if you are using the 1.0 version of the IDE: GEColorEffects Arduino 1.0) As with any other Arduino library, un-zip the file into your libraries folder in your Arduino installation directory. I have included a simple example sketch as well. See this page if you need additional help. As I mention in the library source comments, you may have to tweak the timing due to the imprecise nature of the DigitalWrite function, but the timing provided works well on my boards.  Have fun!

My BatchPCB order for the V2.0 Arduemetry boards as well as my new XBee GPS module has arrived.  Take a look at the pictures below.  I quickly soldered up the GPS module and it looks and works great.  I think it gets better reception now that it is on its own board with a good ground plane.  Definitely seems better than before when I had just soldered pins to the module and plugged it into a socket on my V1.0 board. Now I just need to find the time to assemble the V2.0 board and test it out.  After that’s up and running I’ll have to think of what to do next with this project – possibly make some more modules (sensors, antenna connectors, etc.) with the XBee footprint.  Any suggestions?

XBee GPS Module PCBs

XBee GPS Module PCBs

XBee GPS Module Bottom

XBee GPS Module Bottom

XBee GPS Module Top

XBee GPS Module Top

Arduemetry V2.0 PCBs

Arduemetry V2.0 PCBs

Here is the code I used on the receive side (Arduino USB board with Adafruit XBee adapter) connected to the steering servo and speed control on an old RC-10T truck:

#include "Servo.h"
#include "NewSoftSerial.h"

int joyx,joyy = 135;
int accx,accy = 125;
int zbut,cbut = 0;
int ledpin = 6;
int wheelServo = 9;
int motorServo = 10;
int lightvalue = 255;
char nunchuckData;
int val = 0;
int tempval = 0;
int wheelPos;
int motorPos;
int buttonState = 0;
int lightMode = 0;

Servo steer;
Servo move;

NewSoftSerial xbee(2,3);  //NewSoftSerial mySerial(int rx, int tx);

void setup()
{
//    Serial.begin(57600);
//    Serial.print("Nunchuck ready\n");
    xbee.begin(57600);

    steer.attach(9);
    move.attach(10);
    pinMode(ledpin, OUTPUT);
}

void loop() {
  if (zbut) {
    if (joyy < 130) {
      motorPos = 92+((135-joyy)/5);
      if (motorPos > 135)
        motorPos = 135;
      move.write(motorPos);
    }
    if (joyy > 140) {
      motorPos = 92-((joyy-135)/5);
      if (motorPos < 45)
        motorPos = 45;
      move.write(motorPos);
    }
  }
  else {
    if ((joyy > 130) && (joyy < 140)) {
      move.write(92);
    }
  }
  if (accx > 125) {
    wheelPos = 90-(accx-125);
    if (wheelPos < 45)
      wheelPos = 45;
    steer.write(wheelPos);
  }
  if (accx < 125) {
    wheelPos = 90+(125-accx);
    if (wheelPos > 135)
      wheelPos = 135;
    steer.write(wheelPos);
  }

  if (cbut != buttonState) {          // the button state has changed!
    if (cbut == 1) {                // check if the button is pressed
      if (lightMode == 0) {          // light is off
        lightMode = 1;               // light is on!
        digitalWrite(ledpin, HIGH);
      } else {
        lightMode = 0;               // light is on!
        digitalWrite(ledpin, LOW);
      }
    }
  }
  buttonState = cbut;               // save the new state in our variable

  while(xbee.available()) {
    tempval = xbee.read();
    if (tempval=='x')    {
     nunchuckData='x';
     val=0;
    }
    else if (tempval=='y')    {
      nunchuckData='y';
      val=0;
    }
    else if (tempval=='X')    {
      nunchuckData='X';
      val=0;
    }
    else if (tempval=='Y')    {
      nunchuckData='Y';
      val=0;
    }
    else if (tempval=='Z')    {
      nunchuckData='Z';
      val=0;
    }
    else if (tempval=='C')    {
      nunchuckData='C';
      val=0;
    }
    else if (tempval >='0' && tempval < = '9')    {
      val=val * 10;
      val=val + (tempval - '0');

      if (nunchuckData=='x'){
        joyx=val;
      }
      else if (nunchuckData=='y'){
        joyy=val;
      }
      else if (nunchuckData=='X'){
        accx=val;
      }
      else if (nunchuckData=='Y'){
        accy=val;
      }
      else if (nunchuckData=='Z'){
        zbut=val;
      }
      else if (nunchuckData=='C'){
        cbut=val;
      }
    }
  }  //end of input in serial buffer
}

The NewSoftSerial library is a great way to use any two Arduino pins as another serial port.

Here is the code that I used on the Arduino Pro Mini in the Wireless Wii Nunchuck:

#include "Wire.h"
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx,accy,zbut,cbut,joyy,joyx;
int ledPin = 13;

void setup()
{
    Serial.begin(57600);
    nunchuck_setpowerpins();
    nunchuck_init(); // send the initilization handshake
    pinMode(ledPin, OUTPUT);
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // set the LED on   

    if( loop_cnt > 50 ) { // every 100 msecs get new data
        accx = 120;
        accy = 120;

        loop_cnt = 0;

        nunchuck_get_data();

        accx  = nunchuck_accelx(); // ranges from approx 70 - 182
        accy  = nunchuck_accely(); // ranges from approx 65 - 173
        zbut = nunchuck_zbutton();
        cbut = nunchuck_cbutton();
        joyy = nunchuck_joyy();
        joyx = nunchuck_joyx();

        Serial.print("x");
        Serial.print((byte)joyx,DEC);
        Serial.print("y");
        Serial.print((byte)joyy,DEC);
        Serial.print("X");
        Serial.print((byte)accx,DEC);
        Serial.print("Y");
        Serial.print((byte)accy,DEC);
        Serial.print("Z");
        Serial.print((byte)zbut,DEC);
        Serial.print("C");
        Serial.println((byte)cbut,DEC);
    }
    loop_cnt++;
    delay(1);
}

And the nunchuck functions code can be found here.