As I was working through your code for this, I noticed the way the LSBs for the accelerometer data were being decoded looked strange (This bug applies to the decoding of all three axes, X being used as an example here.)
Let's say nunchuck_buf[5] contains binary ----10-- (where '-' = don't care). If we shift off the first two bits and mask the rest we get binary 00000010, which should be the correct LSB for x. However, in your code:
if ((nunchuck_buf[5] >> 2) & 1) accel_x_axis += 2;
if ((nunchuck_buf[5] >> 3) & 1) accel_x_axis += 1;
This swaps the bits in the LSB, decoding it as 00000001.
To accomplish this without reversing the LSB bits:
(Bit_Offset_x is broken out for illustration in these examples)
Given how widespread your code is (and it's appreciated!) you'll find variations of this almost everywhere in the Arduino community. However, other platforms that use the Wii nunchuck decode the LSB normally, so I strongly suspect this is simply a longstanding bug.
As I was working through your code for this, I noticed the way the LSBs for the accelerometer data were being decoded looked strange (This bug applies to the decoding of all three axes, X being used as an example here.)
Let's say nunchuck_buf[5] contains binary ----10-- (where '-' = don't care). If we shift off the first two bits and mask the rest we get binary 00000010, which should be the correct LSB for x. However, in your code:
This swaps the bits in the LSB, decoding it as 00000001.
To accomplish this without reversing the LSB bits:
If we use uint16_t for the nunchuck_buf entries you can decode this more concisely:
(Bit_Offset_x is broken out for illustration in these examples)
Given how widespread your code is (and it's appreciated!) you'll find variations of this almost everywhere in the Arduino community. However, other platforms that use the Wii nunchuck decode the LSB normally, so I strongly suspect this is simply a longstanding bug.