Hi, I thought I'd share the fruits of this afternoon's labor...
Here is a sketch for controlling a 2 servo robot like the parallax Boe-bot with the wii nunchuck. There a few other examples of this on the internet , but I couldnt get any of the sketches to compile.
This is the first version (that I know of) that uses todbot's wiichuck library to read the nunchuck.
// 2 continuous servos >> arduino pins #9&10
// nunchuck >>arduino analog pins # 2-5 (see wiichuck adaptor
//pinout)
// 93 is idle for my paralax servos. You may need to change all of
//the 93's to whatever works for your servos
#include <Servo.h>
#include <Wire.h>
#include "nunchuck_funcs.h"
int loop_cnt=0;
byte accx,accy,zbut,cbut, joyx, joyy;
int ledPin = 13;
Servo Lservo;
Servo Rservo;
void setup()
{
Rservo.attach(10);
Rservo.write(93);
Lservo.attach(9);
Lservo.write(93);
Serial.begin(19200);
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake
Serial.print("WiiChuckDemo ready\n");
}
void loop()
{
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("accx: "); Serial.print((byte)accx,DEC);
Serial.print("\taccy: "); Serial.print((byte)accy,DEC);
Serial.print("\tzbut: "); Serial.print((byte)zbut,DEC);
Serial.print("\tcbut: "); Serial.println((byte)cbut,DEC);
Serial.print("\tjoyx: "); Serial.println((byte)joyx,DEC);
Serial.print("\tjoyy: "); Serial.println((byte)joyy,DEC);
}
if (accy > 145 && accx <= 145 && accx >= 130){ // forward
Rservo.write(93 - (accy - 144)/3);
Lservo.write(93 + (accy - 144)/3);
}
if (accy < 130 && accx <= 145 && accx >= 130){ // back
Rservo.write(93 + (130 - accy)/3 );
Lservo.write(93 - (130- accy)/3);
}
if (accy <= 145 && accy >= 130 && accx <= 145 && accx >= 130){
Rservo.write(93); //stop
Lservo.write(93);
}
if (accy > 145 && accx < 130){ //forward right
Rservo.write(93 - (accy - 144)/3);
Lservo.write(93 + (accy - 144)/3 -(130- accx)/3 );
}
if (accy > 145 && accx > 145 ){ //forward left
Lservo.write(93 + (accy - 145)/3);
Rservo.write(93 - (accy - 144)/3 + (accx-145)/3);
}
if (accy <= 145 && accy >= 130 && accx < 130){ //turn right
Rservo.write(93 - (130 - accx)/3 );
Lservo.write(93 - (130 - accx)/3);
}
if (accy <= 145 && accy >= 130 && accx > 145){ // turn left
Rservo.write(93 + (accx - 145)/3);
Lservo.write(93 +(accx - 145)/3);
}
if (accy < 130 && accx > 145){ // back right
Rservo.write(93 + (130 - accy)/3 - (accx -145)/3);
Lservo.write(93 - (130- accy)/3);
}
if (accy < 130 && accx < 130){ // back left
Rservo.write(93 + (130 - accy)/3 );
Lservo.write(93 - (130- accy)/3 + (130 - accx)/3);
}
loop_cnt++;
delay(1);
}
Basically, the arduino reads the nunchuck and calculates servo pulses relative to the x and y axis . I started with Todbot's Wiichuck demo to read the nunckuck and print the values to serial ( this helps if you need to calibrate anthing). I also added a read of the analog joystick just cuz it wasnt in the original demo sketch.
Im sure that this sketch can be improved... adding subroutines and more elegant math, etc...but it works. Feel free to use it as you wish and please share what you learned.
When you are tired of following 3ft behind your robot,teathered by the nunchuck wire (which shouldnt be too long,) check out this guy's RC versionhttp://www.societyofrobots.com/robotforum/index.php?topic=2633.0
You will have to become a member to view the attached code and I had trouble using his "Basic" library. caveat emptor