todbot / wiichuck_adapter Goto Github PK
View Code? Open in Web Editor NEWVery simple adpater for hooking a Wii Nunchuck to your Arduino or other microcontroller
Home Page: http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
Very simple adpater for hooking a Wii Nunchuck to your Arduino or other microcontroller
Home Page: http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
Very simple adpater for hooking a Wii Nunchuck to your Arduino or other microcontroller. Example Arduino code is is in firmware/WiichuckDemo. More details here: http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
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 = 2;
LSB_x = (nunchuck_buf[5] >> Bit_Offset_x) & 3;
If we use uint16_t for the nunchuck_buf entries you can decode this more concisely:
Bit_Offset_x = 2;
accel_x_axis = ((nunchuck_buf[5] >> Bit_Offset_x) & 3) |
(nunchuck_buf[2] << 2);
(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.
I have a non-original nunchuck purchased off ebay, and when I am running the example sketch WiichuckDemo
the values below are returned all the time. I woder if anyone managed to solve this problem?
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
accx: 255 accy: 255 zbut: 0 cbut: 0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.