Introduction
Only one LED is controlled in the previous experiments, which is easy and may be less interesting. In this one, let’s try several LEDs at the same time – make them light up and go out alternately, just like flowing water.
Components
– 1 * SunFounder Mars/Mercury Board (Mars board as an example)
– 1 * Breadboard
– 8 * LED
– 8 * Resistor (220Ω)
– 1 * USB cable
– Jumper wires
Principle
This experiment is simply to turn on eight LEDs in turn. The eight LEDs are connected to pin 2-pin 9 respectively. Set them as High level and the corresponding LED at the pin will light up. Control the time of each LED brightening and you will see flowing LED lights.
The corresponding schematic diagram is as follows:
Experimental Procedures
Step 1: Build the circuit
Step 2: Write the code
1) Set the status of the 8 I/O pins as output. Since there are 8 pins involved, use the for() statement to set pin 2 to pin 9 as output respectively.
Write 2 – 9 to i (a variable that keeps changing) respectively, then use pinMode() to set i as output.
2) In the loop()function, light up the 8 LEDs, then extinguish them one by one.
Set pin 2 to pin 9 as High level respectively, and upload the code to the board. Then you will see the LEDs light up one by one.
Now, you can try to make the LEDs brighten and dim in different ways. E.g., extinguish the LEDs one by one. (You need to add the following code behind “}” in the last part. )
Or you can try lighting up the two LEDs in the middle first, then lighting up those next to them in two directions in turn, and extinguish them from the two ends to the middle one by one.
Check the following code to make this happen:
LEDs light up from middle to two sides
LEDs go out from two sides to the middle
For the complete code, refer to the package:
Ultimate Starter Kit for Arduino\Code\Flowing_LED_Lights
Step 3: After compiling the code successfully, select correct Board and Port, then upload the sketch to the board. Here you will see LEDs light up one by one from pin2 to pin9, and go out one by one from pin9 to pin2. Then the LEDs light up from middle to the two sides, and go out from two sides to middle.