Nunchuck

So I’ve been playing with this a while and have gotten no where…
Hope you can help. I want to read the measurements of the nunchuck basically. I’m surprised noone has done this for the imp =(. I tried to base it on the example in the wiki (I’m sure you can tell :wink: )

`// Ambient Light Sensor Firmware
// A great example of how to communicate with a peripheral via I2C

server.log(“Imp Light Sensor Started”);

// I2C Interface to TSL2561FN Light Sensor
// Pin 8 is SCL
// Pin 9 is SDA
// set the I2C clock speed. We can do 10 kHz, 50 kHz, 100 kHz, or 400 kHz
hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);
// the slave address for this device is set in hardware. Saving it here is helpful.
local alsAddr = 0x52;

// function to tell the sensor to start taking a reading
// we send this command, then wait a bit, then collect the reading
function initialize() {
// i2c.write takes two arguments: address and string.
// here, we send 16 bits of data: the first byte is a register, the second is a command
// your needs will vary with the device you are trying to communicate with
hardware.i2c89.write(alsAddr, “\x40”);
hardware.i2c89.write(alsAddr, “\x00”);
// 400 ms required to integrate and complete conversion
}

function startAls() {
// i2c.write takes two arguments: address and string.
// here, we send 16 bits of data: the first byte is a register, the second is a command
// your needs will vary with the device you are trying to communicate with
hardware.i2c89.write(alsAddr, “\x00”);
// 400 ms required to integrate and complete conversion

// schedule the imp to go read the sensor in half a second, after it's done taking a reading
imp.wakeup(1, readAls);

}

// function to collect the reading from the sensor.
// this function is long mostly because the sensor wants us to do funny math with the values
function readAls() {
// i2c.read takes three arguments: address, subaddress, and number of bytes to read
// subaddress is often used for a command or a register to read from
// if you don’t need a subaddress, use “” for none
local reg0 = hardware.i2c89.read(0x52, “\x7E”,2);
// local reg1 = hardware.i2c89.read(alsAddr, “1”,6);

server.log((reg0));

// schedule the imp to take another reading in 5 minutes
imp.wakeup(2, startAls);

}

imp.configure(“Imp Light Sensor”, [], []);
initialize()
startAls();
//EOF`

So, generally you need to write the subaddr address and data in a single go.

ie this:
hardware.i2c89.write(alsAddr, "\\x40"); hardware.i2c89.write(alsAddr, "\\x00");

should be this:

hardware.i2c89.write(alsAddr, "\\x40\\x00");

When you log the read you also need to print out the bytes in a readable form:

server.log(format("%02x %02x", reg0[0], reg0[1]));

for the nunchuck your suppose to write 0x40 and 0x00 to the nunchuck to initialize and 0x00 only afterwards to keep getting readings. i’m not sure what the subaddr should be…

yup tried your method… It didn’t work…

No idea what a nunchuck is? From looking at the 2561 datasheet…

Things to check:

  • Is ADDR pin grounded? (address 0x52 is only correct if it is)
  • You are not powering the device up. You need to write “\x40\x03” to power it up. This writes 0x03 to the control register. \x40\x00 powers it down. See p18 of http://www.adafruit.com/datasheets/TSL2561.pdf
  • You are not addressing the registers correctly to read them.
  • To read DATA0: hardware.i2c89.read(alsAddr, “\xAC”, 2)
  • To read DATA1: hardware.i2c89.read(alsAddr, “\xAE”, 2)

Try this.
`// Nunchuck I2C Interface

hardware.configure(I2C_89); // Pin8 is SCL, Pin9 is SDA
i2c <- hardware.i2c89;

i2c.configure(CLOCK_SPEED_400_KHZ); // I2C clock speed (10 kHz, 50 kHz, 100 kHz, 400kHz)

function readBit(byte, bitPos)
{
local bit = byte & (1 << bitPos);
return bit == 0 ? 1 : 0;
}

function decodeByte(byte)
{
byte = (byte ^ 0x17) + 0x17;
return byte;
}

function readNunchuk()
{
// command to initialize the Nunchuk
i2c.write(0xA4, “\x40\x00”);
imp.sleep(0.05); // wait at least 36ms (from Honeywell HIH6130)

// command to store all sensor data into the 6-byte buffer within Nunchuk
i2c.write(0xA4, "\\x00");
imp.sleep(0.05);

// i2c.read takes three arguments: address, subaddress (use "" for none), and number of bytes to read
local buffer = i2c.read(0xA5, "", 6);

if (buffer == null) {
    server.error("Nunchuck read failed");
    return;
}

local joyX = decodeByte(buffer[0]);
local joyY = decodeByte(buffer[1]);
local accelX = decodeByte(buffer[2]) << 2;
local accelY = decodeByte(buffer[3]) << 2;
local accelZ = decodeByte(buffer[4]) << 2;
local buffer5 = decodeByte(buffer[5]);
accelX += ((buffer5) >> 2) & 0x03;
accelY += ((buffer5) >> 4) & 0x03;
accelZ += ((buffer5) >> 6) & 0x03;
local btnZ = readBit(buffer5, 0);
local btnC = readBit(buffer5, 1);

local text = "X:" + joyX + ",Y:" + joyY + ",Z:" + btnZ + ",C:" + btnC +  ",X:" + accelX + ",Y:" + accelY  + ",Z:" +  accelZ;
server.log(text);
imp.wakeup(0.1, readNunchuk);

}

imp.configure(“Nunchuck Test”, [], []);
readNunchuk();

// End of code
`

a wii nunchuck??

It WORKED!!! =) THANKS so much.

What about AGENT code to get from the I2C Nunchuck IMP to another with RGB LED or other output/ Ideas