Re: Mimicking a Wii-mote using Flowcode and ECIO

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
0
No votes
5
4
100%
 
Total votes: 4

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Mimicking a Wii-mote using Flowcode and ECIO

Post by Benj »

Image

One of the most innovative features of the Nintendo Wii system is the ability to detect the tilt or acceleration of the controller. This allows for games such as tennis or racing to be controlled without having to press any buttons or wear out any mechanical parts. Instead you simply move or tilt the controller. This method of control is all done through an accelerometer module that can detect positive and negative acceleration force on any of the 3-axis that make up our 3-dimensional environment. When at rest the accelerometer will tell you which way the force of gravity is pulling. This would normally be in the negative Z direction if the module is the correct way up and parallel with the floor.

Using one of the Flowcode USB components it is possible to combine an ECIO with an accelerometer to create a fully working tilt controlled joystick for your PC. This is a follow on from the previous USB joystick article demonstrating how to create your own custom controller devices.

The accelerometer I will be using for this are from ST-Micro and can be purchased from Farnell or Future Electronics. The accelerometer devices are surface mount so if you're not too keen with dealing with surface mount components then you can use one of the pre-soldered modules available from either SparkFun Electronics or Ebay.

Image

We will be using an I2C bus and a single input pin to talk to the accelerometer module so as long as you have 3 spare I/O pins on your microcontroller then you can connect the accelerometer module. As we are using the I2C bus the accelerometer module can be ran at 2.5V even with the target microcontroller running at 3.3 or 5V as shown below.

Image

The I/O pins used to communicate to the accelerometer module are specified near the top of the custom_code.c file.

Code: Select all

//ACCELEROMETER COMPONENT DEFINES
#define MX_ACCEL_BUS	0//%a			//0=SPI, 1=I2C
#define MX_DATA_RATE	3//%b			//0=40hz, 1=160hz, 2=640hz, 3=2560hz
#define MX_SCALE		1//%c			//0=+/-2G, 1=+/-6G
#define MX_ADDRESS		0x3A//%d		//Default address 0x3A

#define MX_INT_PIN		0//%e
#define MX_INT_TRIS		trisc//
#define MX_INT_PORT		portc//%f

#ifdef AMX_ACCEL_BUS == 0				//SPI COMPONENT DEFINES

	#define MX_EN_PIN 		1//%g
	#define MX_EN_TRIS		trisc//
	#define MX_EN_PORT		portc//%h

#else							//I2C COMPONENT DEFINES

	#define MX_HARDWARE  	1//%i		//0=Sw, 1=Hw
	#define MX_MI2C_BAUD 	60//%j		//400khz

	#ifdef MX_HARDWARE == 0

		#define MX_PORT		portd//%k
		#define MX_TRIS 	trisd//%l
		#define MX_SCL  	3//%m
		#define MX_SDA  	4//%n

	#endif

#endif
To install the custom code file simply copy and paste it into your "Flowcode Vx/Components" directory. You can make a backup of the original custom code file if you wish to use this at a later date.

Program 1 demonstrates how to use the Flowcode USBserial component to collect readings from your accelerometer and display in a terminal emulator. This is useful for when you are designing your software as it helps to understand the sensor and what it is capable of. Also its nice to know the thing is definitely working before you start trying to take the functionality further.

Program 2 demonstrates how to use the Flowcode USB HID component to create a controller device that takes its input directly from the accelerometer module. This program could be added to by defining thresholds on the accelerometer readings to act as digital switch inputs. I have demonstrated this by allowing button 1 to be set if the accelerometer is rapidly moved in an upwards direction.
Accel.zip
(15.21 KiB) Downloaded 624 times
If you wanted to take this even further you could start looking into either the Bluetooth connection that the Wii-mote's use to connect to the Wii console or the IR camera and sensor bar combination that is used for aiming at the screen. This has already been put to some great use with a concept called "Head Tracking" as seen on YouTube.

Post Reply