Wii Nunchuk example
Posted: Fri Oct 21, 2016 12:39 am
Here is some code to read the Wii Nunchuk. Within the code is a link to more detailed info. Here is a link to the instructable that got me started:
http://www.instructables.com/id/ESP8266-and-Visuino-WiFi-Remote-Control-Smart-Car-/step6/Connect-Jumper-wires-to-the-Wii-Nunchuck-Controlle/
Video Demo:
Wiring picture, yes I cut the connector off, it is possible to put wires in the holes behind the tabs but I bent one and said screw it, they're about $5.00 for a new one on-line.
Next I'll be controlling a little car, maybe even this weekend. Nunchuk coding went faster than I thought it would!
Here's the code:
http://www.instructables.com/id/ESP8266-and-Visuino-WiFi-Remote-Control-Smart-Car-/step6/Connect-Jumper-wires-to-the-Wii-Nunchuck-Controlle/
Video Demo:
Wiring picture, yes I cut the connector off, it is possible to put wires in the holes behind the tabs but I bent one and said screw it, they're about $5.00 for a new one on-line.
Next I'll be controlling a little car, maybe even this weekend. Nunchuk coding went faster than I thought it would!
Here's the code:
Code: Select all
'***************************************************************************
'* Wii Nunchuk code *
'* Poorly coded by Russ McIntire 10/21/16 *
'* Lots stolen from on-line sources *
'* http://www.robotshop.com/media/files/PDF/inex-zx-nunchuck-datasheet.pdf *
'* *
'* Added buttons to start, stop and end the program *
'***************************************************************************
memclear
cls
looper = 0
address = 82 'Nunchuck address
i2c.setup(D3,D4)
i2c.begin(address)
i2c.write(64) 'Initialize with a 64 followed by a 0
i2c.write(0)
i2c.end()
wprint "Start, stop and end the wii nunchuk display on the serial port <br>"
button "START", [startloop]
wprint "<br>"
button "STOP", [stoploop]
wprint "<br>"
button "END", [endprog]
timer 10, [loop]
wait
[loop]
if looper = 0 then goto [skip]
gosub [sendzero] 'Send a zero to trigger receive
i2c.requestfrom(address,6) 'Receive 6 bytes
b1 = (val(i2c.read()) xor 23) + 23 'X joystick
b2 = (val(i2c.read()) xor 23) + 23 'Y joystick
b3 = (val(i2c.read()) xor 23) + 23 'x accelerometer bit 9 to bit 2
b4 = (val(i2c.read()) xor 23) + 23 'y accelerometer bit 9 to bit 2
b5 = (val(i2c.read()) xor 23) + 23 'z accelerometer bit 9 to bit 2
b6 = (val(i2c.read()) xor 23) + 23 '2 bits of x y z, and C and Z button
z = b6 and 1 'set z button
c = (b6 >> 1) and 1 'set c button
joyx = b1
joyy = b2
accelx = (b3 << 2) + ((b6 >> 2) and 3) 'add 2 bits to x
accely = (b4 << 2) + ((b6 >> 4) and 3) 'add 2 bits to y
accelz = (b5 << 2) + ((b6 >> 6) and 3) 'add 2 bits to z
serialprintln z & " " & c & " " &joyx & " " & joyy & " " & accelx & " " & accely & " " & accelz
'gosub [sendzero] 'send zero to trigger next read
[skip]
wait
[stoploop]
looper = 0
wait
[startloop]
looper = 1
wait
[endprog]
end
wait
[sendzero]
i2c.begin(address)
i2c.write(0)
i2c.end()
return