2. Chapter Flowing LED
We have learned previously how to control 1 LED through Sketch on the control board and learned some basic knowledge of programming. Now let us try to control 14 LEDs and learn how to simplify the code.
2.1. Project 2.1 Flowing LED Display
Let us use control board to control 14 LEDs.
2.1.1. Component List
Control board x1
|
USB cable x1
|
Freenove Projects Board
|
|
2.1.2. Code Knowledge
This section will introduce new code knowledge.
2.1.2.1. Array
An array is used to record a set of variables. An array is defined as below:
1int a[10];
“int” is the type of the array and “10” represents the amount of elements of the array. This array can store 10 int types of elements as below.
1int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Or there is another form that the number of elements is the size of the array:
1int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
We can reference elements of an array as below:
1int i, j;
2i = a[0];
3j = a[1];
4a[0] = 0;
Among them, “[]” is the array index, with a[0] as the first elements in the array.
For example, now we define an array b[] below:
1int b[] = {5, 6, 7, 8};
The value of each element in array b[] is as follows:
b[0] |
b[1] |
b[2] |
b[3] |
|---|---|---|---|
5 |
6 |
7 |
8 |
This is just the use of one-dimensional array. And there are two-dimensional arrays, three-dimensional arrays, and multi-dimensional arrays. Readers interested of this part can develop your own learning.
2.1.2.2. Loop
The loop statement is used to perform repetitive work such as the initialization to all the elements of an array.
1while(expression)
2 functionX();
When there is more than one statement to be executed, the form is as follows:
1while(expression){
2 functionX();
3 functionY();
4}
The first step of the execution is judging the expression inside “()”. If the result is false, the statements inside “{}” will not be executed; if result is true, the statements will be executed.
1int i = 0;
2while (i < 2)
3 i = i + 1;
4i = 5;
First time: i<2, i=0 is tenable, execute i=i+1, then i=1;
Second time: i<2, i=1 is tenable, execute i=i+1, then i=2;
Third time: i<2, i=2 is not tenable, execution of loop statements is completed. Statement i=5 will be executed next.
“do while” and “while” is similar. The difference is that the loop statements of “do while” is executed before judging expression. The result of the judgment will decide whether or not to go on the next execution:
1do {
2 functionX();
3} while (expression);
“for” is another loop statement, and its form is as follows:
1for (expression1; expression2; expression 3)
2 functionX();
When there is more than one statement to be executed, the form is as follows:
1for (expression 1; expression 2; expression 3) {
2 functionX();
3 functionY();
4}
Expression 1 is generally used to initialize variables; expression 2 is a judgement which is used to decide whether or not to execute loop statements; the expression 3 is generally used to change the value of variables.
For example:
1int i = 0, j = 0;
2for (i = 0; i < 2; i++)
3 j++;
4i = 5;
First time: i=0, i<2 is tenable, execute j++, and execute i++, then i=1, j=1;
Second time: i=1, i<2 is tenable, execute j++, and execute i++, then i=2, j=2;
Third time: i<2 is tenable, i=2 is not tenable. The execution of loop statements is completed. Statement i=5 will be executed next.
- Operator ++, --
“i++” is equivalent to “i=i+1”. And “i–” is equivalent to “i=i-1”.
2.1.3. Circuit
Let us use pins 0-13 of the control board to drive LEDs.
Schematic diagram
|
Hardware connection
|
Hardware connection
|
|
2.1.4. Sketch
Now let us complete the sketch to control LEDs.
2.1.4.1. Flowing_LED_Display
First, write a sketch to achieve the “movements” of flowing water.
1/*
2 Flowing_LED_Display
3
4 modified 2021/7/2
5 by http://www.freenove.com
6*/
7
8const int ledCount = 14; // the number of LEDs in the bar graph
9
10// an array of pin numbers to which LEDs are attached
11int ledPins[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
12
13void setup() {
14 // loop over the pin array and set them all to output:
15 for (int i = 0; i < ledCount; i++) {
16 pinMode(ledPins[i], OUTPUT);
17 }
18}
19
20void loop() {
21 // the ith LED of LED bar graph will light up in turn
22 for (int i = 0; i < ledCount; i++) {
23 barGraphDisplay(i);
24 }
25 for (int i = ledCount-1; i >=0; i--) {
26 barGraphDisplay(i);
27 }
28}
29
30void barGraphDisplay(int ledOn) {
31 // make the "ledOn"th LED of LED bar graph on and the others off
32 for (int i = 0; i < ledCount; i++) {
33 if (i == ledOn)
34 digitalWrite(ledPins[i], HIGH);
35 else
36 digitalWrite(ledPins[i], LOW);
37 }
38 delay(100);
39}
Firstly, let us define a read-only variable to record the number of LEDs as the number of times in the loop.
1const int ledCount = 14; // the number of LEDs in the bar graph
- Read-only variable
“const” keyword is used to define read-only variables, which is called in the same way as other variables. But read-only variables can only be assigned once.
Then we define an array to store the number of pins connected to LED bar graph. So it is convenient to manipulate arrays to modify the pin number.
1// an array of pin numbers to which LEDs are attached
2int ledPins[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
Use loop statement to set the pins to output mode in function setup().
1void setup() {
2 // loop over the pin array and set them all to output:
3 for (int i = 0; i < ledCount; i++) {
4 pinMode(ledPins[i], OUTPUT);
5 }
6}
Define a function to turn ON a certain LED on the LED bar graph and turn OFF the other LEDs.
1void barGraphDisplay(int ledOn) {
2 // make the "ledOn"th LED of LED bar graph on and the others off
3 for (int i = 0; i < ledCount; i++) {
4 if (i == ledOn)
5 digitalWrite(ledPins[i], HIGH);
6 else
7 digitalWrite(ledPins[i], LOW);
8 }
9 delay(100);
10}
Finally, when the above function is called cyclically, there will be a formation of flowing water lamp effect in LED bar graph.
1void loop() {
2 // the ith LED of LED bar graph will light up in turn
3 for (int i = 0; i < ledCount; i++) {
4 barGraphDisplay(i);
5 }
6 for (int i = ledCount-1; i >=0; i--) {
7 barGraphDisplay(i);
8 }
9}
Verify and upload the code, then you will see the LED bar graph flashing like flowing water.





