Hardware Reference
In-Depth Information
Part 9 is the DspLevel() function, which informs the player what level they are now on. This function is called
before the game starts the next level. This function works in the opposite way to the notWin() function, by shifting
from left to right. In the loop, 1 is added to the high bit of the LEDshift variable by an OR of 0x1000 , then bit shifting the
variable LEDshift to the right by 1. The loop will run as many times as there are levels.
Listing 11-1. Stop It's Code, Part 9 of 11
void DspLevel() {
LEDshift = 0x0000;
for (int i = 0 ; i <= level ; i++) {
LEDshift = LEDshift | 0x1000; // add 1 to the high bits of LEDshift
LEDshift = LEDshift >> 1 ;
displayLED();
delay (50);
}
delay (500);
} // end DspLevel
In part 10 of Listing 11-1 are the two functions to handle the resetting of game play for each level after the reward
and loss patterns are displayed. Listing 11-1 also includes a function to increment the level after a win condition. The
resetPlay() function first calls the DspLevel() function, and then resets all of the game condition variables to their
initial state. The level variable is not reset in this function, but is a condition of a loss.
When the IncreaseLevel() function is called, the level variable is incremented by 1. This function also handles
the reset to level 0 if the player can make it past level 15; the reset is done by an AND mask. The level variable helps set
the speed of the LED sweep and needs to be kept below a certain number; otherwise, the time the LED stays on goes
negative and can halt the Arduino. The level reset in this function is also independent of the loss condition reset.
Listing 11-1. Stop It's Code, Part 10 of 11
void resetPlay () {
DspLevel();
Win = false;
button = false;
LEDshift = 0x0001;
RightLeft = false;
}
void IncreaseLevel() {
level++;
level = level & 0x0F;// reset level when greater than 15
}
The last function (shown in part 11 of Listing 11-1) is the main loop that sets the game into motion and ties
together all the functions of Listing 11-1. The first thing the loop() function does is to detach the interrupts so that the
player cannot cause false wins or losses. The interrupts are not turned off by the noInterrupts() function, because
that would stop the delay() function from working. Once the interrupts have been turned off, the button press and
win state flags are checked for handling. After the check for a win or loss, the loop() function moves the ON LED to
the next LED in the current direction it is traveling. The moveLED() function handles the movement and direction
changes of all the LEDs. After the new LED is on, the interrupt for the button() function is turned back on, followed
by a call to a delay.
 
Search WWH ::




Custom Search