-->
Page 1 of 1

Wii Nunchuk example

PostPosted: Fri Oct 21, 2016 12:39 am
by russm
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.
Image

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


Re: Wii Nunchuk example

PostPosted: Fri Oct 21, 2016 4:44 pm
by Mmiscool
That is an awesome demo.

Thanks for sharing.

Re: Wii Nunchuk example

PostPosted: Fri Oct 21, 2016 8:31 pm
by russm
Least I can do. When I make progress on the receiver (car) end I'll post more. I saw a wireless Wii nunchuk. Thought about using that but it would eliminate the need for another ESP! :)

Hopefully we'll see a few other fun projects using the accelerometer in the Wii, it's so cheap and easy to use how can they be ignored, quite small if you disassemble the nunchuk too.

Need to figure out how to talk to two of them eventually... Oh, forgot to mention, I updated the code operation shown in the original video. Added a couple start/stop/end buttons, everything still displays on the serial display.

Re: Wii Nunchuk example

PostPosted: Mon Oct 31, 2016 8:17 pm
by russm
I haven't forgotten about an update. I have code working to decode and send via udp the Wii XY and buttons to another ESP fully decoded. I haven't written the joystick tank code (2 motors) to run the nodeMCU motor controller shield but I've done it before and it's not too hard. Also may write code to control a Sabertooth 10A ESC which currently runs a large Stewart tank for fun. Sabertooth has joystick mixing mode built in. Yes I have one and keep looking at it as a great platform to shoot paintballs from. Racing this coming weekend so probably not making much progress but I'll be thinking about it... Probably not... :D