// adapt to your hardware config #define POWER_VIA_PORT_C2_C3 1 // can use port pins port C2 and C3 as power supply of the Nunchuck //comment this out if you are powering the wireless receiver directly from 5V supply #include #include #include #include "Nunchuk.h" Nunchuk chuk; int i; //declare i as an integer int j; //declare j as an integer #define LEDx 11 //pin 11 varies according to x accel pin11 is a pwm variable voltage output #define LEDy 10 //defines pin 10 as varying according to y accel pin 10 is a pwm variable voltage output #define ledc 8 //defines pin 8 as output for c button (0V or 5V output) #define ledz 9 //defines pin 9 as output for z button (0V or 5V output) void setup() { pinMode(LEDx, OUTPUT); //tell arduino it is an output pinMode(LEDy, OUTPUT); //tell arduino it is an output pinMode(ledc, OUTPUT); //tell arduino it is an output pinMode(ledz, OUTPUT); //tell arduino it is an output //Serial.begin(115200); Wire.begin(); // use 400 kHz for the nunchuck, because it doesn't work reliable with 100 kHz TWBR = ((CPU_FREQ / 400000L) - 16) / 2; // power supply of the Nunchuck via port C2 and C3 (for the wiichuck adapter directly plugged on Arduino) #ifdef POWER_VIA_PORT_C2_C3 PORTC &=~ _BV(PORTC2); PORTC |= _BV(PORTC3); DDRC |= _BV(PORTC2) | _BV(PORTC3); // make outputs delay(100); // wait for things to stabilize #endif delay(100); } void loop() { if(!chuk.ok()) { //Serial.println("Initing nunchuk, it's broken!"); chuk.init(); } if(chuk.read()) { /* Serial.print(chuk.joyX(), DEC); Serial.write(' '); Serial.print(chuk.joyY(), DEC); Serial.write(' '); Serial.print(chuk.buttonZ(), DEC); Serial.write(' '); Serial.print(chuk.buttonC(), DEC); Serial.write(' '); Serial.print(chuk.accX(), DEC); Serial.write(' '); Serial.print(chuk.accY(), DEC); Serial.write(' '); Serial.print(chuk.accZ(), DEC); Serial.println(); */ j = chuk.joyY() + 127; //joy scale is -127 to +127 so now converted to 0 - 255 if (j<1) j=1; if (j>253) j=253; //Serial.print("Joy Y = "); //Serial.println(j); analogWrite(LEDy, j); //sets LED on pin 10 used to control the Trim function brightness according to forward/back of joystick if (chuk.buttonZ() == 1 && chuk.buttonC() == 0) { //if pressing z button only, steer on left/right joystick digitalWrite (ledz, HIGH); // sets the LED high on pin 9 i = chuk.joyX() + 127; //joy scale is -127 to +127 so mid point is now 127 if (i<1) i=1; if (i>253) i=253; analogWrite(LEDx, i); //sets LED on pin 11 steering PWM brightness //Serial.println("pressing z not pressing c"); //Serial.print("joyX="); //Serial.println(i); } if (chuk.buttonZ() == 1 && chuk.buttonC() == 1) { //if pressing both buttons, steer using accelerometer digitalWrite (ledz, HIGH); // sets the LED high motor kill switch control on pin 9 i= (chuk.accX() - 512) / 2 + 128; //0-1023 reading rescaled to 0 to 255 if (i>253) i=253; if (i<1) i=1; analogWrite(LEDx, i); //sets LED on pin 10 brightness //Serial.print("pressing both buttons"); //Serial.print("accelX= "); //Serial.println(i); } if (chuk.buttonZ() == 0 && chuk.buttonC() == 1) { //if c button only pressed then steer on accelerometer digitalWrite (ledz, HIGH); // sets the LED high on pin 9 i= (chuk.accX() - 512) / 2 + 128; //re-scale of 0 to 255 if (i>253) i=253; if (i<1) i=1; analogWrite(LEDx, i); //sets LED on pin 11 steering brightness //Serial.print("pressing C only"); //Serial.print("accelX= "); //Serial.println(i); } if (chuk.buttonZ() == 0 && chuk.buttonC() == 0) { //if no button pressed then dead ahead and control signal to AVR off i.e. stop motor digitalWrite (ledz, LOW); // sets the LED low on pin 9 analogWrite(LEDx, 128); //sets LED on pin 11 steering half PWM i.e. dead ahead if skateboard analogWrite(LEDy, 128); //sets LED on pin 10 half PWM i.e. mid point for the trim function if skateboard //Serial.println("not pressing either button"); } } else { Serial.println("chuck.read() failed"); } delay(100); }