After including all of the libraries we need, we define the
2
constant. I
C is a master/slave protocol; in our case, the Arduino will be the
master, and the Nunchuk will be the slave. The Nunchuk registers itself at
the data bus using a certain ID (0x52), so we can address it when we need
something.
In
initialize
, we establish the connection between the Arduino and the Nunchuk
by sending a handshake. In line 7, we call
2
joins the I
C bus as a master. (If you pass
slave having this ID.) Then we ll begin a new transmission to the device
identified by
NUNCHUCK_DEVICE_ID
We send two bytes (0x40 and 0x00) to the Nunchuk, and then we end the
transmission. This is the whole handshake procedure, and now we can ask
the Nunchuk for its current status by calling
we see the message flow between an Arduino and a Nunchuk.
Arduino
Handshake (0x40, 0x00)
Request new data (0x00)
update
first pauses for a millisecond to let things settle. Then we request six
bytes from the Nunchuk, calling
the bytes, but we have to read them in a loop and fill our buffer.
returns the number of bytes available on the data bus, and
the current byte. We cannot use the bytes we get from the Nunchuk directly,
because the controller obfuscates them. "Decrypting" them is easy, as you
can see in
.
decode_byte
Finally, we call
request_data
a single zero byte to the Nunchuk, which means "prepare the next six bytes."
Before we actually use our
documentation of the Wire library. In the Arduino IDE s menu, choose Help
> Reference and click the Libraries link.
Wire
s
begin
: our Nunchuk.
update
6 data bytes
6 data bytes
Wire.requestFrom
to tell the Nunchuk to prepare new data. It transmits
Nunchuk
class in the next section, take a look at the
www.it-ebooks.info
Building a Nunchuk Class
NUNCHUK_DEVICE_ID
begin
method, so the Arduino
an ID, it joins the bus as a
. In the following figure,
Nunchuk
}
repeat
. This doesn t actually return
Wire.available
Wire.read
151
returns
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?