WiiLAB Example Code - getNunchukButtons
Note: MATLAB R2007a or greater is required for this code to run correctly.
Annotations
The getNunchukButtons function allows the user to tell which buttons are currently being pressed on the Wiimote's Nunchuk extension. The function returns an array of values, one for each button, allowing the user to determine if multiple buttons are being pressed simultaneously.
Usage
buttons = getNunchukButtons();
Return
getNunchukButtons will return an array of 2 elements. Each element corresponds to a button on the Nunchuk as follows: [C Z]. Each element will be a 1 (if pressed) and a 0 (if not pressed).
Example
The following snippet of code will execute the block inside the if statement, and 'C and Z are being pressed on the Nunchuk' will display, if 'C' and 'Z' are being pressed on the Wiimote at the time of function call.
buttons = getNunchukButtons();
if ( buttons(1) && buttons(2) )
disp('C and Z are being 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 [C Z] = getNunchukButtons ()
% usage: getNunchukButtons ()
% purpose: Gets the Button values from the wiimote nunchuk (true if they are
% pressed - false if they are not)
% note: initializeWiimote() must be called before calling this function
global wiimote;
% update the wiimote state values
wiimote.GetNunchukButtonsState();
% assign outputs
C = wiimote.NunchukButtons.C;
Z = wiimote.NunchukButtons.Z;
end