Two LEDs Taking Turns
Part of the First Circuits section of Coddy's Arduino journey. Lesson 5 of 22.
Each LED needs its own pin, its own pinMode() and its own resistor. Here the red LED is on pin 8 and the green one on pin 9.
To make them take turns, turn one on and the other off, wait, then swap:
digitalWrite(8, HIGH); // red on
digitalWrite(9, LOW); // green off
delay(1000);The board runs one line at a time, but very fast: two digitalWrite() lines in a row happen a few millionths of a second apart, so to your eye they change together.
The order of the lines is the order things happen. Read your loop() from top to bottom and say what each LED is doing at every delay().
Challenge
EasyMake the two LEDs take turns, 500 ms each:
- make pins 8 and 9 outputs
- red on and green off, wait 500 ms
- red off and green on, wait 500 ms
One LED should always be on, never both. Press Run, then Check.
Try it yourself
void setup() {
// make pins 8 and 9 outputs
}
void loop() {
// red on, green off, wait
// red off, green on, wait
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in First Circuits
1Hello, Board
Blink an LEDSetup and LoopChange the Blink SpeedAn LED of Your OwnTwo LEDs Taking TurnsComments in Your SketchPractice on your own: Arduino playground