WiiLAB Example Code - getNunchukAccel
Note: MATLAB R2007a or greater is required for this code to run correctly.
Annotations
The getNunchukAccel function allows the user to obtain the current readings from each of the three accelerometers of the Wiimote's Nunchuk extension. The function returns an array of values, one for each axis, allowing the user to track the Nunchuk's movement in 3-space.
Usage
accel = getNunchukAccel();
Return
getNunchukAccel will return an array of 3 elements. Each element corresponds to one of the Nunchuk's accelerometers as follows: [X Y Z]. Each element will be a normalized value reflecting the actual acceleration of the Nunchuk in the given direction.
Example
The following snippet of code will execute the block inside the if statement and 'Motion detected in the positive X and negative Y directions' will be displayed if the Nunchuk is being accelerated along its positive X and negative Y axes.
accel = getNunchukAccel();
if ( (accel(1) > 0) && (accel(2) < 0) )
disp('Motion detected in the positive X and negative Y directions');
end
Raw Source Code
The source code is also available as a .m file download at the bottom of the page.
function [X Y Z] = getNunchukAccel ()
% usage: getNunchukAccel ()
% purpose: Gets the Acceleration values from the wiimote nunchuk
% note: initializeWiimote() must be called before calling this function
global wiimote;
% update the wiimote state values
wiimote.GetNunchukAccelState();
% assign outputs
X = wiimote.NunchukAccel.X;
Y = wiimote.NunchukAccel.Y;
Z = wiimote.NunchukAccel.Z;
end