Hardware Reference
In-Depth Information
Listing 11-1. Stop It's Code, Part 1 of 11
int LEDshift = 0x0001; // holds the LED pattern
boolean RightLeft = false; // true for right
boolean Win = false; // when true, a win state has be achived
boolean button = false; // flag if the button has been pressed
byte level = 0; // curent level holder
void setup() {
DDRD = DDRD | B11111000; // pins 3 - 7 set data direction
DDRB = DDRB | B00111111; // pins 8 - 13
digitalWrite(2,HIGH); // pull up so the input can be signaled on a low transition
}
The code for part 2 of Stop it is the function to perform the LED sweep. The moveLED() function is called from
the main loop. The function first checks if the ON LED is at the first or last LED of the display. The check is performed
by AND masking the LEDshift variable. If the mask equals anything other than zero, then the check is true , and
depending on which mask is true , you set the flag RightLeft to the proper direction. The function then checks the
RightLeft direction variable to bit shift the LEDshift over one every time the moveLED() function is called. The
function then calls the displayLED() function.
Listing 11-1. Stop It's Code, Part 2 of 11
void moveLED() {
if (LEDshift & 0x0002) {
RightLeft = false;
}
if (LEDshift & 0x0800) {
RightLeft = true;
}
if (!RightLeft ) {
LEDshift = LEDshift << 1;
}
if (RightLeft) {
LEDshift = LEDshift >> 1;
}
displayLED();
} // end moveLED
The displayLED() function is part 3 for Listing 11-1. This function is responsible for changing the actual pin
states to control the LEDs. When the displayLED() function is called, the LEDshift variable is parsed and split to
match to the pins that are connected to the LED array. To get the LEDs that are connected to pins 3 through 7, the
LEDshift variable is masked against a number that correlates to the position of the bits needed, and the result is then
shifted to the left by two positions so that the final result is in the proper position for the pins. Before the total result is
written to the register, a NOT operation is performed so that the pins will be in the proper state for the LED.
Listing 11-1. Stop It's Code, Part 3 of 11
void displayLED() {
PORTD = ~((LEDshift & 0x003E) << 2); // format and place the proper bits into the registers
PORTB = ~((LEDshift & 0x0FC0) >> 6); // portd = low portb = hi
}
 
Search WWH ::




Custom Search