NXT Button Control by User Application Program

There are four buttons on the NXT front panel. The LEFT, ENTER and RIGHT buttons are on the top row and the EXIT button is on the second row.

Normally, when a program is running, the LEFT and RIGHT buttons are used to toggle between various standard displays. And the EXIT button is used to stop execution of the user program. These characteristics can be overwritten so that button actions can be controlled within a running application program

 

nNxtButtonTask

The normal value of this variable is -1’ to indicate that button handling should be processed by the NXT firmware and not within a user application program. Setting this variable to any other value gives application programs control of the NXT buttons.

 

When the value is set to a valid task, then that task will be started (or restarted) whenever a button is depressed. If it is an invalid task, then no action is taken other than allowing application control over all of the buttons.

 

nNxtButtonPressed

Firmware can only recognize one button push at a time. This variable contains the index (0..3) of the currently pressed button or -1 to indicate no buttons are pressed.

 

nNxtExitClicks

This variable contains the number of consecutive exit button ‘clicks’ that must be received before a user program is aborted. The default value is one click. If a user program wants to use the exit button, then it may set this field to a higher number. The firmware is always monitoring the exit button for program abort, even when application has grabbed control of the buttons; this will prevent a incorrect (or malicious) program from locking the NXT up.

 

There are several sample programs (e.g. Centipede, Tetris) included in the RobotC distribution that utilize application program control over buttons.