WiiLAB Example Code - getWiimoteAccel
Note: MATLAB R2007a or greater is required for this code to run correctly.
Annotations
The getWiimoteAccel function allows the user to obtain the current readings from each of the Wiimote's three accelerometers. The function returns an array of values, one for each axis, allowing the user to track the Wiimote's movement in 3-space.
Usage
accel = getWiimoteAccel();
Return
getWiimoteAccel will return an array of 3 elements. Each element corresponds to one of the Wiimote's accelerometers as follows: [X Y Z]. Each element will be a normalized value reflecting the actual acceleration of the Wiimote 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 Wiimote is being accelerated along its positive X and negative Y axes.
accel = getWiimoteAccel();
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] = getWiimoteAccel ()
% usage: getWiimoteAccel ()
% purpose: Gets the Acceleration values from the wiimote
% note: initializeWiimote() must be called before calling this function
global wiimote;
% update the wiimote state values
wiimote.GetAccelState();
% assign outputs
X = wiimote.Accel.X;
Y = wiimote.Accel.Y;
Z = wiimote.Accel.Z;
end