nunchuck sentry gun


so i've finished hardware portion of coil turret.  i have coil mounted on servo, , it's controlled wii nunchuck.  i have capacitor discharging through relay using transistor, , works fine.  i've got servo responding nunchuck (albeit little srewy, hey, nintendo).  my problem i'm totally stuck on how program it, , seems pretty huge problem me.  i think can manage of coded myself, it'll tricky. i'm using following code right now.
code: [select]
#include <wire.h>

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

int servopin = 2;      // control pin servo motor
int pulsewidth = 0;    // amount pulse servo
long lastpulse = 0;    // time in millisecs of last pulse
int refreshtime = 20;  // time in millisecs needed in between pulses
int minpulse = 700;   // minimum pulse width

#define pwbuffsize 4  
int pwbuff[pwbuffsize]; // buffer smoothing pulsewidths
int pwbuffpos = 0;      // position in pwbuff

void setup()
{
 serial.begin(19200);
 wire.begin ();            // join i2c bus address 0x52
 nunchuck_init (); // send initilization handshake
 pinmode(servopin, output);  // set servo pin output pin
 pulsewidth = minpulse;      // set motor position minimum
 serial.print ("finished setup\n");
}

void nunchuck_init()
{
 wire.begintransmission (0x52);      // transmit device 0x52
 wire.send (0x40);            // sends memory address
 wire.send (0x00);            // sends sent zero.  
 wire.endtransmission ();      // stop transmitting
}

void send_zero()
{
 wire.begintransmission (0x52);      // transmit device 0x52
 wire.send (0x00);            // sends 1 byte
 wire.endtransmission ();      // stop transmitting
}

int t = 0;  // when gets 25, read nunchuck
void loop()
{
 t++;
 if( t == 25 ) {
   t = 0;
   wire.requestfrom (0x52, 6);      // request data nunchuck
   while (wire.available ()) {
     // receive byte integer
     outbuf[cnt] = nunchuk_decode_byte (wire.receive ());
     digitalwrite (ledpin, high);      // sets led on
     cnt++;
   }
   // if recieved 6 bytes, go print them
   if (cnt >= 5) {
   //  printnunchuckdata();            // uncomment debug
     // update servo pulsewidth
     float tilt = outbuf[4];            // z-axis, in case ranges ~75 - ~185
     tilt = (tilt - 70) * 1.5;          // convert degrees angle, approximately
     pulsewidth = (tilt * 9) + minpulse; // convert angle microseconds
     pwbuff[pwbuffpos] = pulsewidth;    // save averaging
     if( ++pwbuffpos == pwbuffsize ) pwbuffpos = 0;

     pulsewidth=0;                      // reset can  
     for( int p=0; p<pwbuffsize; p++ )  // smoothing
       pulsewidth += pwbuff[p];         // sum them all
     pulsewidth /= pwbuffsize;          // divide average
     
     // uncomment debug
     //serial.print("tilt: ");   serial.print((int)tilt);
     //serial.print(" pulsewidth: ");  serial.println(pulsewidth);
   }
   cnt = 0;
   send_zero(); // send request next bytes
   
 } // if(t==)

 updateservo();   // update servo position

 delay(1);
}

// called every loop().
// uses global variables servopin, pulsewidth, lastpulse, & refreshtime
void updateservo() {
 // pulse servo again if rhe refresh time (20 ms) have passed:
 if (millis() - lastpulse >= refreshtime) {
   digitalwrite(servopin, high);   // turn motor on
   delaymicroseconds(pulsewidth);  // length of pulse sets motor position
   digitalwrite(servopin, low);    // turn motor off
   lastpulse = millis();           // save time of last pulse
 }
}

// print input data have recieved
// accel data 10 bits long
// read 8 bits, have add
// on last 2 bits.  that why i
// multiply them 2 * 2
int i=0;
void printnunchuckdata()
{
 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 z , c buttons
 // contains least significant bits accelerometer data
 // have 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 (i,dec);
 serial.print ("\t");

 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 (" ");
 serial.print (c_button, dec);

 serial.print ("\r\n");
 i++;
}

// encode data format wiimote drivers except
// needed if use 1 of regular wiimote drivers
char nunchuk_decode_byte (char x)
{
 x = (x ^ 0x17) + 0x17;
 return x;
}


and i'd how know how use c , z buttons.  i'm going make "z" fire relay , "c" turn on laser pointer.  could tell me how make work, or post example code?

much in advance!

edit:  i realized problem =[  when run usb, works fine.  but when run off 9v battery, stalls , servo moves way left , stops.  is battery culprit?

*bump*  can offer words of advice here?


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > nunchuck sentry gun


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums