Suppose there are 6 LEDs hooked up to Arduino digital pins 3-8. Using for() loop
ID: 3675688 • Letter: S
Question
Suppose there are 6 LEDs hooked up to Arduino digital pins 3-8. Using for() loops, write a sketch that would turn the LEDs on for 0.5 seconds at a time in order, 3-8, and reverse order, 8-3, indefinitely. Refer to the pseudocode below to understand how the LEDs should behave:
Setup Declare digital pins 3-8.
Loop
Turn on first LED for 0.5 seconds. Turn off first LED.
Turn on second LED for 0.5 seconds. Turn off second LED.
Turn on third LED for 0.5 seconds. Turn off third LED.
Turn on fourth LED for 0.5 seconds. Turn off fourth LED.
Turn on fifth LED for 0.5 seconds. Turn off fifth LED.
Turn on sixth LED for 0.5 seconds. Turn off sixth LED.
Turn on fifth LED for 0.5 seconds. Turn off fifth LED.
Turn on fourth LED for 0.5 seconds. Turn off fourth LED.
Turn on third LED for 0.5 seconds. Turn off third LED.
Turn on second LED for 0.5 seconds. Turn off second LED.
Turn on first LED for 0.5 seconds. Turn off first LED.
Explanation / Answer
int count = 0;
int values[8][3] = {
{ LOW, LOW, LOW },
{ LOW, LOW, HIGH },
{ LOW, HIGH, LOW },
{ LOW, HIGH, HIGH },
{ HIGH, LOW, LOW },
{ HIGH, LOW, HIGH },
{ HIGH, HIGH, LOW },
{ HIGH, HIGH, HIGH },
};
void setup() {
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
count = (count + 1) % 8;
digitalWrite(1, values[count][0]);
digitalWrite(2, values[count][1]);
digitalWrite(3, values[count][2]);
digitalWrite(4, values[count][3]);
digitalWrite(5, values[count][4]);
digitalWrite(6, values[count][5]);
digitalWrite(7, values[count][6]);
digitalWrite(8, values[count][7]);
delay(1000);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.