3. Chapter LED Bar Graph
We have learned previously how to control 1 or 2 LEDs through Sketch on the control board and learned some basic knowledge of programming. Now let us try to control 10 LEDs and learn how to simplify the code.
3.1. Project LED Bar Graph Display
Let us use control board to control a bar graph LED consisting of 10 LEDs.
3.1.1. Component List
Control board x1
|
||
Breadboard x1
|
GPIO Extension Board x1
|
|
USB cable x1
|
Jumper M/M x10
|
|
LED bar graph x1
|
Resistor 220Ω x10
|
|
3.1.2. Component Knowledge
Let us learn about the basic features of components to use them better.
3.1.2.1. LED bar graph
LED bar graph is a component integration consisting of 10 LEDs. At the bottom of the LED bar graph, there are two rows of pins, corresponding to positive and negative pole separately. If the LED bar graph cannot work in the circuit, it is probably because the connection between positive and negative pole is wrong. Please try to reverse the LED bar graph connection.
3.1.3. Code Knowledge
This section will use new code knowledge.
3.1.3.1. Array
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.
3.1.3.2. Loop
The loop statement is used to perform repetitive work such as the initialization to all the elements of an array.
1while(expression)
2functionX();
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)
2functionX();
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–” equivalent to “i=i-1”.
“i++” is equivalent to “i=i+1”. And “i–” equivalent to “i=i-1”.
3.1.4. Circuit
Let us use pin 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 of the control board to drive LED bar graph.
Schematic diagram |
|
Hardware connection If you need any support, please feel free to contact us via: support@freenove.com |
|
3.1.5. Sketch
Now let us complete the sketch to control LED bar graph.
3.1.5.1. Sketch LED_bar_graph_Display
First, write a sketch to achieve the LED light water.
1/**********************************************************************
2 Filename : Sketch_3.1.1_LED_bar_graph_Display
3 Description : LED bar graph Display
4 Auther : www.freenove.com
5 Modification: 2024/08/05
6**********************************************************************/
7
8const int ledCount = 10; // the number of LEDs in the bar graph
9
10// an array of pin numbers to which LEDs are attached
11int ledPins[] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
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}
26
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 = 10; // 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 used 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[] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
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}
Verify and upload the code, then you will see the LED bar graph flashing like flowing water.
3.1.5.2. Sketch LED_bar_graph_Display
Then modify the code to create a reciprocating LED light water.
1/**********************************************************************
2 Filename : Sketch_3.1.2_LED_bar_graph_Display
3 Description : LED bar graph Display
4 Auther : www.freenove.com
5 Modification: 2024/08/05
6**********************************************************************/
7
8const int ledCount = 10; // the number of LEDs in the bar graph
9
10// an array of pin numbers to which LEDs are attached
11int ledPins[] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
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 // makes the "i"th LED of LED bar graph bright in turn
22 for (int i = 0; i < ledCount; i++) {
23 barGraphDisplay(i);
24 }
25 // makes the "i"th LED of LED bar graph bright in reverse order
26 for (int i = ledCount; i > 0; i--) {
27 barGraphDisplay(i - 1);
28 }
29}
30
31void barGraphDisplay(int ledOn) {
32 // make the "ledOn"th LED of LED bar graph on and the others off
33 for (int i = 0; i < ledCount; i++) {
34 if (i == ledOn)
35 digitalWrite(ledPins[i], HIGH);
36 else
37 digitalWrite(ledPins[i], LOW);
38 }
39 delay(100);
40}
We have modified the code inside the function loop() to make the LED bar graph light up in one direction, and then in a reverse direction repeatedly.
Verify and upload the code, then you will see a reciprocating LED water light on LED bar graph.








