I made a quick summary:
1) remove the pull-ups resistor in the Nunchuk :
(source: http://www.mindstormsforum.de/viewtopic.php?t=3828)
2) Connect new pull-ups and power up the Nunchuk (3.3v) :
You can directly connect the 4.5v of the NXT (Green wire) on the Nunchuk, but it will damage your Nunchuk.
3)Program your NXT :
-You need to use the Gloomyandy leJOS upgrade (look above : snapshot2).
-The sensor source code :
- Code: Select all
package v2;
import lejos.nxt.*;
/**
* I2C mastering for the Nunchuk
*
* @version v0.1
* @project Nunchuk leJOS
* @author Si-Mohamed Lamraoui
*/
public class NunchukSensor extends I2CSensor
{
public final static int NUNCHUCK_ADDR = 0x52;
private I2CPort port ;
private byte[] data = new byte[6];
private byte[] init = new byte[2];
/**
* Create a new Nunchuk sensor plug on the NXT port
* @param sensorPort NXT sensor port
*/
public NunchukSensor(I2CPort sensorPort)
{
super(sensorPort);
this.port = sensorPort;
this.setAddress(NUNCHUCK_ADDR);
this.port.setType(SensorConstants.TYPE_LOWSPEED);
this.port.setMode(SensorConstants.MODE_RAW);
}
/**
* Initializes the Nunchuk to communicate in I2C
*/
public void initialize()
{
this.init[0] = 0x00;
this.sendData(0x40, init,1);
}
/**
* Method for retrieving the data from the Nunchuk
* @return value of 8-bit I2C bus port
*/
public byte[] getNunchukData()
{
this.getData(0x00, this.data, 6);
return data;
}
/**
* Ask 6 Bytes of sensor value
*/
public void AskNunchukData()
{
this.sendData(0x00, (byte) 0x00);
}
/**
* Method for decrypt the data from the Nunchuk
* @return value decrypted
*/
public byte decryptData(byte value){
return (byte) ((value ^ 0x17) + 0x17);
}
}
The test source code :
- Code: Select all
package v2;
import javax.microedition.lcdui.Graphics;
import lejos.nxt.*;
/**
* Test the Nunchuk with the LCD screen
*
* @version v0.2
* @project Nunchuk leJOS
* @author Si-Mohamed Lamraoui
*/
public class TestLCD
{
/**
* Main
*/
public static void main()
{
NunchukSensor nunchuk = new NunchukSensor(SensorPort.S4);
Graphics graph = new Graphics();
byte[] data = new byte[6];
graph.drawString(" NUNCHUK NXT v0.1",1,1);
graph.clear();
while(!Button.ESCAPE.isPressed())
{
graph.drawString(" === NUNCHUK === ",0,0);
nunchuk.initialize();
nunchuk.AskNunchukData();
data = nunchuk.getNunchukData();
//Z-Button
if((nunchuk.decryptData(data[5]) & 0x01) > 0)
{
graph.drawString("Z : RELEAZED",0,9);
}
else
{
graph.drawString("Z : PRESSED",0,9);
}
//C-Button
if((nunchuk.decryptData(data[5]) & 0x02) > 0)
{
graph.drawString("C : RELEAZED",0,17);
}
else
{
graph.drawString("C : PRESSED",0,17);
}
//Joystick
graph.drawString("Joystick X:",1,24);
LCD.drawInt(nunchuk.decryptData(data[0]),0,12,3);
graph.drawString("Joystick Y:",1,32);
LCD.drawInt(nunchuk.decryptData(data[1]),0,12,4);
//Accelerometer
graph.drawString("Accel X:",1,40);
LCD.drawInt(nunchuk.decryptData(data[2]),0,12,5);
graph.drawString("Accel Y:",1,48);
LCD.drawInt(nunchuk.decryptData(data[3]),0,12,6);
graph.drawString("Accel Z:",1,56);
LCD.drawInt(nunchuk.decryptData(data[4]),0,12,7);
graph.refresh();
try
{
Thread.sleep(10);
}
catch (InterruptedException e) {}
graph.clear();
}
}
}
HAVE FUN !!!
