Saturday, July 3, 2010

Flash with Dual Intec Nunchuks

Having lost interest in processing and having a bit of experience with Flash, I decided to research methods of integrating my dual nunchuk configuration with Flash via the USB/Serial communication. After much heart ache, I finally found a site created by Kasper Kamperman that had details on how to connect the two. His site contains a download for the FLA and AS files, which are a main component for this project. It is important to note that this code is designed for Flash 10/Actionscript 3.

The second component is Serproxy, the download of which is located at the bottom of the Arduino software page. Serproxy converts the serial connection to the Arduino into a network connection so that flash can access the data. Further info on Serproxy can be found at the Arduino Playground Serial-to-Proxy page .

Serproxy requires a modification to its CFG file via any text editor in order to direct Serproxy to the correct port. The instructions on how to do so can be seen within the CFG file near the bottom of the code (use something other than notepad). Also, ensure comm_baud matches your baud setting for your serial communications.


Kamperman's code takes data, separated by commas and ended by "\r", and places it in an array called "receivedValues", where the received data is separated and stored in the array in a chronological fashion. This data refreshes every time the Arduino completes transmission. To use this with my previous code, one must change the "\n" to "\r" in my main Arduino code. Or you could download the new one.

Friday, May 21, 2010

Dual Intec Wireless Nunchuk Communication with Arduino

I saw an instructable some time ago featuring the Wii Nunchuk integrated with an arduino Microcontroller.  Having already experimented with a 3 axis accelerometer from SeeedStudio with my DFRobot Atmega1280-Mega microcontroller, I figured I would give it a shot with a pair of Intec Nunchuks I found on sale at The Source (also available here online).

Program:
Download

Arduino: 
After some research, I managed to find a program that worked for the type of nunchuk I had, as well as a helpful Google Project entry to go with it.  The creator of the program and the entry, Radek Karpowicz, would seem to be a quite skilled programmer and was generous enough to licence his program under the Creative Commons 3.0 BY-SA license.  This allows me to edit and redistribute his program, of course, giving him credit for his work.  Unfortunately, I am relatively new to arduino programming.  As such, my modifications are not ideal, however with some tweaking and patience, they work.

I recommend taking a look at Karpowicz's Google Project entry before continuing.
There is also a blog explaining in more detail a similar program's method of interfacing with a wireless nunchuk located here.
If you have a wireless nunchuk other than Nyko Kama, Datel Wireless Duo-FX or Intec's wireless nunchuk and this program does not work for you, I suggest checking out the arduino forums.  You may also wish to read the instructable on the matter (single nunchuk).

Processing:
The processing code uses several different libraries, the first of which is the serial library, the purpose of which is self explanatory.  The second is PeasyCam.  This library allows the use of the mouse to control the view angle, zoom and pan.  The library can be downloaded here.  The third library is the Robot library.  This library allows for interfacing with the keyboard and mouse controls, allowing one to control the mouse and use the nunchuk buttons as keys.  These functions are disabled by default via //comment.  Ensure you save all open projects before compiling a program that uses Robot.

The processing program displays the serial data as two vectors and displays the previous 50 vectors connected via lines.  The program also allows for each vector's connected lines' colours to be changed via the thumbstick.  This is achieved via a set of Arraylists.



Wiring:
The physical setup for a dual wireless nunchuk interface was devised by integrating Karpowicz's design, shown on his Google Project entry with a switching concept observed here, created by Johnnyonthespot.
As mentioned by Karpowicz, the SDA and SDL pins must be connected to 1.8k pullup resistors.

The interface schematic between the arduino and the nunchuk's wireless module can be seen below:
Image created using Fritzing.

Note: this interface using the I2C pins, SDA (data) and SDL (clock), of the arduino, these allow for sending and receiving using set protocols via the "wire.h" library.  On my board SDA is located on pin 20, and SCL on 21, on other boards these pin locations may be changed or nonexistent.

In this example, I used pins 6 and 7 to control the switching between the two nunchuk wireless modules instead of 22 and 24, this can easily be changed in the arduino program by changing the values for nunchuk1 and nunchuk2 to the desired pin numbers.
The transistors I used were 2N2222 and the resistors connected to the Base of each transistor have a value of 680 ohms.

Connecting Nunchuks to Arduino:
To connect the nunchuks to the arduino a bit of patience is needed.  You must first power up both nunchuks, then press the sync button for one of them (for example; the one in your right hand).  Once the serial output displays "Waiting for Duo FX Controller Device" again then you must then sync the second nunchuk.  The program will then try to connect to the second nunchuk.  This may take some time, as the setups for each of the nunchuks tend to interfere with each other.  If, after a minute or two, the setup does not complete, try turning one of the nunchuks off, closing the display window, recompile ►, then turning the nunchuk on and resynchronizing it.  After the first successful setup, the setup often goes much quicker for the setups thereafter when recompiling.