WiiLAB Example Code - isNunchukButtonPressed
Note: MATLAB R2007a or greater is required for this code to run correctly.
Annotations
The isNunchukButtonPressed function allows the user to tell if a button on the Wiimote's nunchuk extension is currently being pressed.
Usage
isNunchukButtonPressed('[Button]');
* where [Button] is one of the following strings: C, Z
Return
isNunchukButtonPressed will return true (1) if the button is pressed and false (0) if it is not.
Example
The following snippet of code will execute the block inside the if statement, and 'C is pressed on the Nunchuk' will be displayed, if 'C' is pressed on the Nunchuk at the time of function call.
if ( isNunchukButtonPressed('C') )
disp('C is pressed on the Nunchuk');
end
Raw Source Code
The source code is also available as a .m file download at the bottom of the page.
function [buttonPressed] = isNunchukButtonPressed(ButtonToCheck)
% usage: isButtonPressedNunchuk(ButtonToCheck)
% Possible Buttons :: C Z
% purpose: Tests to see if a button is being pressed
global wiimote;
wiimote.GetNunchukButtonsState();
% check if the button is pressed based on the ButtonToCheck input
switch(upper(ButtonToCheck))
case 'C'
buttonPressed = wiimote.Buttons.C;
case 'Z'
buttonPressed = wiimote.ButtonsZ;
otherwise
buttonPressed = 0;
end