Instructables

Arduino animatronics- make your awesome costumes more awesome!

FeaturedContest WinnerContest Winner

Step 13: Going wireless with XBee


Example 9- Wireless XBee radios

This allows you to use the Nunchuck control but does it by setting up one controller as a transmitter and another controller as the receiver. I use the Adafruit XBee wireless adapter because it's super easy to use and can work on both 3V and 5V so it's great for most any Arduino wireless project. The servo board is set up for point to point communication and the XBee adapter plugs directly into a socket at the top of the board. This setup would really well for controlling haunted house attractions.

You can get the XBee adapter and radios here-
http://www.adafruit.com/index.php?main_page=product_info&cPath=29&products_id=126

There is a complete XBee tutorial for building the XBee adapter and configuring it here-
http://www.ladyada.net/make/xbee/

Unlike the previous nuchuck control code, this code uses a new nunchuck library that cleans up the code considerably. It is called nunchuck_funcs.h
You can get it here-
http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/comment-page-3/

Simply place the nunchuck_funcs.h library into your Arduino libraries folder and you're good to go.

The wiring for this is exactly like the wiring for example 6 except the nunchuck is connected to the transmitter and the servos and LEDs are connected to the receiver.

Note- you cannot connect the USB to serial adapter while the XBee adapter is plugged into its socket as they use the same data transmission lines. You must program the controller first and then plug in the XBee adapter. If you need to be able to monitor the serial port while using the XBee you can use a couple of wires to connect the XBee RX and TX to digital out pins 2 and 3 as shown in the Adafruit point-to-point tutorial.


Here's the code for the transmitter-

/*
 * Example 9a
 * Nunchuck XBee transmitter
 * This code uses the nunchuck functions library and an XBee radio (w/ Adafruit XBee adapter)
 * to make a Wii nunchuck a wireless input device
 * Honus 2010
 * Nunchuck functions library from Tod E. Kurt, http://todbot.com/blog
*/


#include "Wire.h" // include the Wire library
#include "nunchuck_funcs.h" // include the nunchuck functions library

int loop_cnt=0;

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

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

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

    if( loop_cnt > 100 ) { // every 100 msecs get new data
        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();
        joyx = nunchuck_joyx();
        joyy = nunchuck_joyy();

        Serial.print("joyx:");
        Serial.print((byte)joyx,DEC);
        Serial.print("joyy:");
        Serial.print((byte)joyy,DEC);
        Serial.print("accx:");
        Serial.print((byte)accx,DEC);
        Serial.print("accy:");
        Serial.print((byte)accy,DEC);
        Serial.print("\tzbut:");
        Serial.print((byte)zbut,DEC);
        Serial.print("\tcbut:");
        Serial.println((byte)cbut,DEC);
    }
    loop_cnt++;
    delay(1);
}


Here's the code for the receiver-

/*
 * Example 9b
 * Nunchuck XBee receiver
 * This code receives input from a Wii nunchuck using an XBee radio (w/ Adafruit XBee adapter)
 * and controls four servos and two digital pins to turn on LEDs
 * Honus 2010
*/


#include "Servo.h" // include the Servo library

int joyx,joyy = 135;
int accx,accy = 135;
int zbut,cbut = 0;
int ledPin1 = 13;
int ledPin2 = 12;
int lightvalue1 = 255;
int lightvalue2 = 255;
char nunchuckData;
int val = 0;
int tempval = 0;
int servo1Pos;
int servo2Pos;
int servo3Pos;
int servo4Pos;
int buttonState1 = 0;
int buttonState2 = 0;
int lightMode1 = 0;
int lightMode2 = 0;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int servoPin1 = 9;
int servoPin2 = 8;
int servoPin3 = 7;
int servoPin4 = 6;


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

    servo1.attach(servoPin1);
    servo2.attach(servoPin2);
    servo3.attach(servoPin3);
    servo4.attach(servoPin4);
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
}

void loop() {
 
  if (accy > 125) {
    servo1Pos = 90-(accy-125);
    if (servo2Pos < 45)
      servo1Pos = 45;
    servo1.write(servo1Pos);
  }
  if (accy < 125) {
    servo1Pos = 90+(125-accy);
    if (servo1Pos > 135)
      servo1Pos = 135;
    servo1.write(servo1Pos);
  }
 
 
  if (accx > 125) {
    servo2Pos = 90-(accx-125);
    if (servo2Pos < 45)
      servo2Pos = 45;
    servo2.write(servo2Pos);
  }
  if (accx < 125) {
    servo2Pos = 90+(125-accx);
    if (servo2Pos > 135)
      servo2Pos = 135;
    servo2.write(servo2Pos);
  }
 
   if (joyy > 125) {
    servo3Pos = 90-(joyy-125);
    if (servo3Pos < 45)
      servo3Pos = 45;
    servo3.write(servo3Pos);
  }
   if (joyy < 125) {
    servo3Pos = 90+(125-joyy);
    if (servo3Pos > 135)
      servo3Pos = 135;
    servo3.write(servo3Pos);
  }
 
 
  if (joyx > 125) {
    servo4Pos = 90-(joyx-125);
    if (servo4Pos < 45)
      servo4Pos = 45;
    servo4.write(servo4Pos);
  }
  if (joyx < 125) {
    servo4Pos = 90+(125-joyx);
    if (servo4Pos > 135)
      servo4Pos = 135;
    servo4.write(servo4Pos);
  }
 
  if (cbut != buttonState1) {         // the button state has changed!
    if (cbut == 1) {                  // check if the button is pressed
      if (lightMode1 == 0) {          // light is off
        lightMode1 = 1;               // light is on!
        digitalWrite(ledPin1, HIGH);
      } else {
        lightMode1 = 0;               // light is on!
        digitalWrite(ledPin1, LOW);
      }
    }
  }
 
  if (zbut != buttonState2) {         // the button state has changed!
    if (zbut == 1) {                  // check if the button is pressed
      if (lightMode2 == 0) {          // light is off
        lightMode2 = 1;               // light is on!
        digitalWrite(ledPin2, HIGH);
      } else {
        lightMode2 = 0;               // light is on!
        digitalWrite(ledPin2, LOW);
      }
    }
  }
  buttonState1 = cbut;               // save the new state in our variable
  buttonState2 = zbut;               // save the new state in our variable
 
 
 
  while(Serial.available()) {
    tempval = Serial.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
}

 
Remove these adsRemove these ads by Signing Up