Introduction: Wii Chuck Controlled Air-soft Gun Turret :D




Code  


#include
#include

/*
 * WiiChuck Turret --
*
* 2011 Luke Cudnik
*
*/

#include
#include "nunchuck_funcs.h"

int loop_cnt=0;
Servo s1;
Servo s2;
byte accx,accy,zbut,cbut;
int ledPin = 13;


void setup()
{
pinMode(11, OUTPUT);
Serial.begin(19200);
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake
s1.attach(6);
s2.attach(8);
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_joyx(); // ranges from approx 70 - 182
accy = nunchuck_joyy(); // ranges from approx 65 - 173
zbut = nunchuck_zbutton();
cbut = nunchuck_cbutton();

int ax= map(accx,30,230,1,180);
int ay= map(accy,30,230,1,180);
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("\ax: ");
Serial.println((byte)ax,DEC);
Serial.print("\ay: ");
Serial.println((byte)ay,DEC);
if (cbut ==1){
s1.write(90);
s2.write(90);
}
if (cbut == 0){
if((ax<80) or (ax>100)){
s1.write(ax);
}
else{
s1.write(90);
}
}
if (cbut == 0){
if((ay<80) or (ay>100)){
s2.write(ay);
}
else{
s2.write(90);
}
}
}
if (zbut == 1){
digitalWrite(11, HIGH);
}
if (zbut == 0){
digitalWrite(11, LOW);
}
loop_cnt++;
delay(1);
}