Need Easy help...Wii nunchuck+motor shield code

Of course, I’m a newbie. Currently, I’m building a kinetic art piece which will be run by the Diecimila, interfaced with a wii nunchuck, and controlling the Adafruit servo/stepper/Dc motor shield. Right now I have the board programmed to control 1 DC motor, but I am having a hard time figuring out how to combine sketches so that I can use the wii and the shield to its maximum ability. Basically, I just know how to wire things up, and I’m learning the code language. I am just not that good yet at figuring out how to navigate through all of the codes. I can handle loading 1 sketch at a time, but combining them has proved to be more complicated than I have time for. Here is a website for the shield:

http://www.ladyada.net/make/mshield/

All motors will be supplied with external voltage through the shield’s ext. volt. terminal blocks. I’d like to at least run a stepper, and a servo… possibly a little dc motor too. Can anyone help? I’m sure that there are plenty of people who’ve already done this and can supply just a sample code. Any help would be much appreciated. Thank you so much for your time. When the piece is finally finished, I will invite anyone who wants to come to the opening (I’m an artist/art professor). You can contact me through here or daricgill@aol.com. :smiley:

What do you have now for communicating with the wii? To combine sketches you need to get the data into the arduino and then use decision code to activate your output. If you could include your code perhaps the people here could help you combine it into a working sketch.

THANKS so much for the quick response! Zach Smith from the RepRap/Make: groups told me that people would respond on this forum, but I had no idea that it would be with this speed. Thank you!

Well, I have not loaded sketches up other than the dc motor code listed on the ladyada’s site. But here is what I thought about using…Windmeadow Labs has a code for the Wii nunchuck interfacing on their website (http://www.windmeadow.com/node/42). Between the Arduino website and Ladyada, I’ve been able to understand roughly the basic codes that allocate a pin and timing to a particular motor. From what I understand, the sample codes for stepper motors and dc motors really just assign pins, speed, and time. So, really I think that I just need to alter the Windmeadow Labs’ Wii code to assign and control the shields pins. Am I right? Codes are after this description and labeled accordingly. Because I’m using the shield, not just inserting the wii and motors into the Arduino, I having a hard time figuring out where to insert the code.

Code for Wii Nunchuck:

#include <Wire.h>
#include <string.h>

#undef int
#include <stdio.h>

uint8_t outbuf[6]; // array to store arduino output
int cnt = 0;
int ledPin = 13;

void
setup ()
{
beginSerial (19200);
Serial.print (“Finished setup\n”);
Wire.begin (); // join i2c bus with address 0x52
nunchuck_init (); // send the initilization handshake
}

void
nunchuck_init ()
{
Wire.beginTransmission (0x52); // transmit to device 0x52
Wire.send (0x40); // sends memory address
Wire.send (0x00); // sends sent a zero.
Wire.endTransmission (); // stop transmitting
}

void
send_zero ()
{
Wire.beginTransmission (0x52); // transmit to device 0x52
Wire.send (0x00); // sends one byte
Wire.endTransmission (); // stop transmitting
}

void
loop ()
{
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ())
{
outbuf[cnt] = nunchuk_decode_byte (Wire.receive ()); // receive byte as an integer
digitalWrite (ledPin, HIGH); // sets the LED on
cnt++;
}

// If we recieved the 6 bytes, then go print them
if (cnt >= 5)
{
print ();
}

cnt = 0;
send_zero (); // send the request for next bytes
delay (100);
}

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits. That is why I
// multiply them by 2 * 2
void
print ()
{
int joy_x_axis = outbuf[0];
int joy_y_axis = outbuf[1];
int accel_x_axis = outbuf[2] * 2 * 2;
int accel_y_axis = outbuf[3] * 2 * 2;
int accel_z_axis = outbuf[4] * 2 * 2;

int z_button = 0;
int c_button = 0;

// byte outbuf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
if ((outbuf[5] >> 0) & 1)
{
z_button = 1;
}
if ((outbuf[5] >> 1) & 1)
{
c_button = 1;
}

if ((outbuf[5] >> 2) & 1)
{
accel_x_axis += 2;
}
if ((outbuf[5] >> 3) & 1)
{
accel_x_axis += 1;
}

if ((outbuf[5] >> 4) & 1)
{
accel_y_axis += 2;
}
if ((outbuf[5] >> 5) & 1)
{
accel_y_axis += 1;
}

if ((outbuf[5] >> 6) & 1)
{
accel_z_axis += 2;
}
if ((outbuf[5] >> 7) & 1)
{
accel_z_axis += 1;
}

Serial.print (joy_x_axis, DEC);
Serial.print ("\t");

Serial.print (joy_y_axis, DEC);
Serial.print ("\t");

Serial.print (accel_x_axis, DEC);
Serial.print ("\t");

Serial.print (accel_y_axis, DEC);
Serial.print ("\t");

Serial.print (accel_z_axis, DEC);
Serial.print ("\t");

Serial.print (z_button, DEC);
Serial.print ("\t");

Serial.print (c_button, DEC);
Serial.print ("\t");

Serial.print ("\r\n");
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char
nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}

Code for Stepper Motor Test:

#include <AFMotor.h>

AF_Stepper motor(48, 2);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Stepper test!”);

motor.setSpeed(10); // 10 rpm

motor.step(100, FORWARD, SINGLE);
motor.release();
delay(1000);
}

void loop() {
motor.step(100, FORWARD, SINGLE);
motor.step(100, BACKWARD, SINGLE);

motor.step(100, FORWARD, DOUBLE);
motor.step(100, BACKWARD, DOUBLE);

motor.step(100, FORWARD, INTERLEAVE);
motor.step(100, BACKWARD, INTERLEAVE);

motor.step(100, FORWARD, MICROSTEP);
motor.step(100, BACKWARD, MICROSTEP);
}

Code for Servo Motor Test:

#include <ServoTimer1.h>

ServoTimer1 servo1;
ServoTimer1 servo2;

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Servo test!”);

servo2.attach(9);
servo1.attach(10);
}

void loop() {
Serial.print(“tick”);
servo1.write(180);
servo2.write(0);
delay(1000);

Serial.print(“tock”);
servo1.write(0);
servo2.write(180);
delay(1000);
}

OK, in the wii code you have

  Serial.print (joy_x_axis, DEC); 
  Serial.print ("\t"); 
 
  Serial.print (joy_y_axis, DEC); 
  Serial.print ("\t"); 
 
  Serial.print (accel_x_axis, DEC); 
  Serial.print ("\t"); 
 
  Serial.print (accel_y_axis, DEC); 
  Serial.print ("\t"); 
 
  Serial.print (accel_z_axis, DEC); 
  Serial.print ("\t"); 
 
  Serial.print (z_button, DEC); 
  Serial.print ("\t"); 
 
  Serial.print (c_button, DEC); 
  Serial.print ("\t");

So the things you are printing now need to control something.
Take the #includes from all the programs and put them in one and combine the setup code for the devices. Then you have the values from the numchuck and you need to deceide what you want each response to do with the other devices. Hope this gives you some ideas.