Hardware Reference
In-Depth Information
Recalling what you learned earlier in the chapter, convert the binary values
for each light step to decimal values that can easily be cycled through. Using a
for loop, you can cycle through an array of each of these values and shift them
out to the shift register one at the time. The code in Listing 7-2 does just that.
Listing 7-2: Light Rider Sequence Code—lightrider.ino
//Make a light rider animation
const int SER =8; //Serial output to shift register
const int LATCH =9; //Shift register latch pin
const int CLK =10; //Shift register clock pin
//Sequence of LEDs
int seq[14] = {1,2,4,8,16,32,64,128,64,32,16,8,4,2};
void setup()
{
//Set pins as outputs
pinMode(SER, OUTPUT);
pinMode(LATCH, OUTPUT);
pinMode(CLK, OUTPUT);
}
void loop()
{
for (int i = 0; i < 14; i++)
{
digitalWrite(LATCH, LOW); //Latch low - start sending
shiftOut(SER, CLK, MSBFIRST, seq[i]); //Shift most sig. bit first
digitalWrite(LATCH, HIGH); //Latch high - stop sending
delay(100); //Animation speed
}
}
By adjusting the value within the delay function, you can change the speed
of the animation. Try changing the values of the seq array to make different
pattern sequences.
NOTE Towatchademovideoofthelightrider,checkout www.exploringarduino
.com/content/ch7 .YoucanalsofindthisvideoontheWileywebsiteshownatthe
beginningofthischapter.
Search WWH ::




Custom Search