Ciao a tutti, sto iniziando a cimentarmi nella sperimentazione del nunchuck e del suo collegamento ad arduino, io ho acquistato il nunchucky, ovvero questo:
e stavo provando il tutto...le cose che non mi sono chiare sono:
1)Perchè quando sta tutto fermo, in particolare quando è fermo il joystick al centro, mi restituisce dei valori del joystick= 10 e -2 (x e y rispettivamente) ?
- Mi sapete indicare a che servono istruzioni del genere:
Serial.print(buf[5]&1 ? "\t- " : "\t[z] ");
Conosco il loro effetto ma vorrei capire meglio il funzionamento in generale di un'istruzione del genere (proprio dal punto di vista della programmazione)
- Perchè quando appoggio il nunchuck sul tavolo le accelerazioni percepite su x y e z sono diverse da 0 ? non dovrebbero essere = 0 ?
Vi ringrazio e vi terrò aggiornati su qualunque progresso!!!
Il codice che ho utilizzato è questo:
// nunchuck.pde
//
#include <Wire.h>
uint8_t buf[6]; // buffer for the six-bytes packet coming from Nunchuck
int cnt=0; // count of bytes in the buffer
int readcnt=0; // number of times we read a packet from Nunchuck
int ledPin=13; // which pin will host the blinking LED
uint8_t nunchuk_decode(uint8_t x) // decode nunchuck data
{
return (x^0x17)+0x17; // not that an hard encryption...
}
void nunchuck_ack() // acknowledge a Nunchuck packet
{
Wire.beginTransmission(0x52); // send a zero to device 0x52
Wire.send(0);
Wire.endTransmission();
}
void setup() // initialization
{
int n;
digitalWrite(ledPin, HIGH); // turn on LED
for(n=0; n<6; n++) buf[n]=0; // fill buffer with zero values
Serial.begin(115200); // console init
Wire.begin(); // TWI init
Wire.beginTransmission(0x52); // nunchuck init
Wire.send(0x40);
Wire.send(0);
Wire.endTransmission();
digitalWrite(ledPin, LOW); // turn off LED
Serial.print("!-- start\n");
}
void loop() // main loop
{
Wire.requestFrom(0x52, 6); // request data from nunchuck
while(Wire.available()) // read data and light the LED
{
buf[cnt++] = nunchuk_decode(Wire.receive());
digitalWrite(ledPin, HIGH);
}
if(cnt>=6) // an entire Nunchuck packet was read?
{
printdata(); // yes, print it
cnt=0; // clear buffer counter
nunchuck_ack(); // acknowledge received packet
digitalWrite(ledPin, LOW); // turn off the LED
delay(800); // wait 800msec before next loop
}
}
void printdata() // print out the values
{
int n; // note: the 123,131,524,597... depend on my Nunchuck calibration
Serial.print(++readcnt, DEC);
Serial.print("\t"); // joystick x/y values
Serial.print((int)buf[0]-123);
Serial.print("\t");
Serial.print((int)buf[1]-131);
n=(buf[2]<<2)+((buf[5]>>2)&3)-524; // accel X
Serial.print("\t");
Serial.print(n);
n=(buf[3]<<2)+((buf[5]>>4)&3)-597; // accel Y
Serial.print("\t");
Serial.print(n);
n=(buf[4]<<2)+((buf[5]>>6)&3)-668; // accel Z
Serial.print("\t");
Serial.print(n);
Serial.print(buf[5]&1 ? "\t- " : "\t[z] ");
Serial.print(buf[5]&2 ? "-" : "[c]");
Serial.print ("\r\n");
}