OK here are the code pieces I am working with:
// Arduino Center//
//
www.facebook.com/ArduinoCenter//
blog.underc0de.org/arduino-wii-nunchuck-servo-motores/// Original Code base credited to Undercode
// Code adapted from Sean Maio Crybabyfx setup
//Include the needed libraries.
#include "Wire.h"
#include "ArduinoNunchuk.h"
#include <Servo.h>
// This is imported from the smartgun setup
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
// pins used for Serial communication with the audio board
// FX board must be grounded on the uart pin.
// I believe these have to be crossed tx on one end and rx
// on the other end.
//
#define SFX_TX 1
#define SFX_RX 0
// initializes the sound board serial connection
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, NULL);
//Creates the objects to control the servos
ArduinoNunchuk nunchuk = ArduinoNunchuk();
Servo servo3;
Servo servo5;
Servo servo9;
//Initializes the variables
int xjoystick;
int yjoystick;
int xtilt;
int ytilt;
//int controlPin1 = 6;
//int pos9 = 85;
//const int leftpin6 = 6;
//int leftpinpressed = 0;
// sets push button input
//int buttonPin = 4;
// from honus 2007 setup
int ledPin1 = 11; // control pin for LED
int ledPin2 = 12; // control pin for laser sight
// for push button
//int val; // variable for reading the pin status
//int buttonState; // variable to hold the last button state
void setup() {
//Initializes the serial servo monitor
Serial.begin(9600);
ss.begin(9600);
delay(800);
//give the audio board time to power up.
// Otherwise bootup sound will be called before audio
// board is ready.
// this plays an initialization sound.
Serial.print("#0\n");
Serial.print("Initialized serial connections\n");
//Initializes nunchuck ans servos
nunchuk.init();
servo3.attach(3);
servo5.attach(5);
servo9.attach(9);
// pinMode(controlPin1, INPUT);
// pinMode(buttonPin, INPUT); // set the button pin as input
// from honus setup sets pins 11, 12 as led output pins
pinMode(ledPin1, OUTPUT); // sets the LED pin as output
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin1, LOW); // sets the LED pin LOW (turns it off)
digitalWrite(ledPin2, LOW);
// for push button
// buttonState = digitalRead(buttonPin);
}
void loop() {
// This turns on the head laser while device is on.
digitalWrite(ledPin2, HIGH); // sets the LED pin HIGH (turns it on)
// this takes the arm servo and raises it
//servo9.write(10); //resets cannon arm
// servo9.write(5);
// delay(50);
// servo9.write(170); //raises cannon arm
// val = digitalRead(buttonPin);
//Guardamos los valores que nos manda el Nunchuk en las variables
xjoystick = nunchuk.analogX;
xjoystick = constrain(xjoystick, 26, 226);
xjoystick = map(xjoystick, 26, 226, 0, 180);
yjoystick = nunchuk.analogY;
yjoystick = constrain(yjoystick, 26, 226);
yjoystick = map(yjoystick, 26, 226, 180, 0);
xtilt = nunchuk.accelX;
xtilt = constrain(xtilt, 320, 720);
xtilt = map(xtilt, 320, 720, 180, 0);
ytilt = nunchuk.accelY;
ytilt = constrain(ytilt, 320, 720);
ytilt = map(ytilt, 320, 720, 0, 180);
// This prints the serial status of the nunchuck.
Serial.print ("Joystick X: ");
Serial.print (xjoystick, DEC);
Serial.print ("\t");
Serial.print ("Joystick Y: ");
Serial.print (yjoystick, DEC);
Serial.print ("\t");
Serial.print ("X: ");
Serial.print (xtilt, DEC);
Serial.print ("\t");
Serial.print ("Y: ");
Serial.print (ytilt, DEC);
Serial.print ("\t");
nunchuk.update();
if (nunchuk.cButton == 1) {
Serial.print("--C-- ");
}
if (nunchuk.zButton == 1) {
Serial.print("--Z-- ");
}
if (nunchuk.cButton == 1 && nunchuk.zButton == 1) {
Serial.print("--Z-C--");
}
Serial.print ("\r\n");
// this takes the joystick and moves servos without button presses
// this method is too draining on the battery
// servo3.write(xjoystick);
// servo5.write(yjoystick);
// delay(15);
// does not work well
// // read input value and store it in val
// if (val != buttonState) { // the button state has changed!
// if (val == LOW) { // check if the button is pressed
// Serial.println("button pressed");
// Serial.print ("button pressed");
// if (servo9.read() == 170) {
// servo9.write(10);
// }
// }
// }
// These are the while loops.
// They are what send commands to lights and
//This moves the servos while button z is pressed
//With the joystick
//once let go it remembers the position until next press
while (nunchuk.zButton == 1) {
// servo3.write(xtilt);
// servo5.write(ytilt);
digitalWrite(ledPin1, HIGH);
Serial.print("#2\n");
delay(500);
digitalWrite(ledPin1, LOW);
// prints sound stored on pin2 named T02.ogg or T02.wav
break;
}
while (nunchuk.cButton == 1 && nunchuk.zButton == 1) {
digitalWrite(ledPin1, HIGH);
Serial.print("#1\n");
delay(500);
digitalWrite(ledPin1, LOW);
// prints sound stored on pin3 named T03.ogg or T03.wav
// splat sound
break;
}
//This moves the servos while button c is pressed
//With the joystick
//once let go it remembers the position until next press
while (nunchuk.cButton == 1) {
// servo9.write(170);
servo3.write(xjoystick);
servo5.write(yjoystick);
// digitalWrite(ledPin1, HIGH);
// Serial.print("#3\n");
// delay(500);
// digitalWrite(ledPin1, LOW);
break;
}
// this method was too draining on battery
// while (nunchuk.cButton == 0 && nunchuk.zButton == 0) {
// servo3.write(xjoystick);
// servo5.write(yjoystick);
// break;
// }
delay(15);
}