ecco il codice che ho usato :
#include <LiquidCrystal.h>
//
// nunchuck.pde
//
#include <Wire.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
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
double maxx=0,maxy=0,maxz=0;
double x,y,z,temperatura;
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
{
Serial.begin(9600);
int n;
digitalWrite(ledPin, HIGH); // turn on LED
for(n=0; n<6; n++) buf[n]=0; // fill buffer with zero values
Wire.begin(); // TWI init
Wire.beginTransmission(0x52); // nunchuck init
Wire.send(0x40);
Wire.send(0);
Wire.endTransmission();
digitalWrite(ledPin, LOW); // turn off LED
for(int a =2;a<13;a++)
{
pinMode(a,OUTPUT);
if(a>1&&a<8)
{
digitalWrite(a,HIGH);
}
else
digitalWrite(a,LOW);
}
}
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(10); // wait 10msec before next loop
}
}
void printdata() //mostra i valori su lcd
{
int n; //variabile usata per i pulsanti
temperatura = (5.0 * analogRead(0) * 100.0)/1024.0;
x=(buf[2]<<2)+((buf[5]>>2)&3); // accel x
y=(buf[3]<<2)+((buf[5]>>4)&3); // accel Y acquisisco i valori
z=(buf[4]<<2)+((buf[5]>>6)&3); // accel z
z-=500;
x-=500;//centro lecordinate
y-=500;
z=z2/512;
x=x2/512;//converto il valore numerico in valore G
y=y*2/512;
if(maxy<y)
{
maxy=y;
}
if(maxx<x)
{
maxx=x;
}
if(maxz<z)
{
maxz=z;
}
n=(buf[5]&2 );
if(n==0)
{
mostraminmax();
}
else
{
mostravalori();
}
delay(100);
}
void mostraminmax()
{
lcd.clear();
lcd.print("MY= ");
lcd.print(maxy);
lcd.setCursor(9, 0);
lcd.print("MZ= ");
lcd.print(maxz);
lcd.setCursor(0, 1);
lcd.print("MX= ");
lcd.print(maxx);
}
void mostravalori()
{
lcd.clear();
lcd.print("Y: ");
lcd.print(y);
lcd.setCursor(9, 0);
lcd.print("Z: ");
lcd.print(z);
lcd.setCursor(0, 1);
lcd.print("X: ");
lcd.print(x);
lcd.setCursor(9, 1);
}
qui è prensente anche la parte per visualizzare i valori della forza, mostrare i valori massimi alla pressione del pulsante sul nunchuk
e rilevo la tempreatura tramite un lm 35 e la mostro sul display pero quaesta ultima parte la sto scrivendo adesso
spero che ti sia utile