WiiLab Example Code - BouncingBall: getXVel
Note: MATLAB R2007a or greater is required for this code to run correctly.
Annotations
Called by the resetWindow function to obtain a value for the constant horizontal velocity of the ball. The user keeps the Wiimote's 'B' button pressed and swings the Wiimote to the right, releasing the button mid-swing to record and use the x-acceleration as the ball's velocity along the x-axis.
Raw Source Code
The source code is also available as a .m file download at the bottom of the page.
function getXVel()
% usage: getXVel()
% purpose: obtains the velocity with which the user throws the ball by
% swinging the Wiimote to the right
global xVel;
% while the Wiimote's B trigger is held down
while (isButtonPressed('B'))
% update the velocity in response to the Wiimote's acceleration
xVel = getWiimoteAccel();
end %while
% if user swung the Wiimote the wrong direction or let go of B at the
% wrong time, producing a nonpositive velocity
if (xVel <= 0)
t4 = text(250, 275, 'Please try again.', 'HorizontalAlignment', 'center', 'FontSize', 12);
%xVel = 1;
waitForButtonPress('B');
getXVel();
hide(t4);
end %if
end