Datasheet
Chapter 2: EEPROM Tricks and Program Tips ยท Page 65
ACTIVITY #5: PUSHBUTTON PROGRAM MODE SELECTION
In some competitions, changing strategy between sumo rounds could make a big
difference in your SumoBot's standing. This activity introduces a simple technique you
can use to select the SumoBot's mode of operation based on cues from the speaker.
Selecting the Mode
In the next example program, you can enter a mode selection by pressing and holding the
pushbutton while you press and release the Reset button. After releasing the Reset
button, the program will beep once, then twice, then three times, and so on up to five
times. To select mode one, let go of the pushbutton after the speaker has beeped once.
To select mode two, keep holding the pushbutton, and instead let go after the speaker has
beeped twice. For mode three, wait for three beeps before letting go, and so on.
This program uses an EEPROM byte with a different
Symbol name - ModeSelect. This
is another value that is initialized to zero when the program is downloaded, but
WRITE
commands will change this value during run-time.
' -----[ EEPROM Data ]-------------------------------------------------
ModeSelect DATA 0 ' Program select byte
This IF...THEN statement in the Initialization routine checks to see if the pushbutton is
pressed. If it is, the
Mode_Select subroutine gets called. Otherwise, the program just
moves on to the next command.
' -----[ Initialization ]----------------------------------------------
IF pbSense = 1 THEN GOSUB Mode_Select ' Call Mode_Select subroutine
The Mode_Select subroutine uses a pair of nested FOR...NEXT loops. The outer loop
uses the
temp variable to count from 1 to 5. The inner loop counts from 1 to temp. The
first time through the outer loop,
temp is 1, so the inner loop beeps the speaker once. The
inner loop is followed by a 1-second
PAUSE, after which, an IF...THEN statement checks
the pushbutton. If the pushbutton has been released, the
IF...THEN statement writes the
value 1 stored by
temp to the ModeSelect EEPROM byte and exits the outer loop.
If the pushbutton has not yet been released, the outer loop repeats. The second time
through the
FOR...NEXT loop, temp will be 2. So, the inner loop beeps twice. After
another 1 second
PAUSE, the outer loop again checks the pushbutton. If the pushbutton