2. Chapter LED Bar Graph
We have learned how to control one LED to blink. Next, we will learn how to control a number of LEDs
2.1. Project FollowLight
In this project, we will use the mouse to control the LED Bar Graph
2.1.1. Component List
|
Jumper Wires x6 |
Bar Graph LED x1 |
Resistor 220Ω x10 |
2.1.2. Component knowledge
Let us learn about the basic features of these components to use and understand them better.
2.1.2.1. Bar Graph LED
A Bar Graph LED has 10 LEDs integrated into one compact component. The two rows of pins at its bottom are paired to identify each LED like the single LED used earlier.
2.1.3. Circuit
A reference system of labels is used in the circuit diagram below. Pins with the same network label are connected together.
Schematic diagram
Hardware connection
Note
If LEDbar doesn’t work, rotate LEDbar 180° to try. The label is random.
In this circuit, the cathodes of the LEDs are connected to the GPIO, which is different from the previous circuit. The LEDs turn ON when the GPIO output is low level in the program.
2.1.4. Sketch
2.1.4.1. Sketch FollowLight
First, observe the result after running the sketch, and then learn about the code in detail.
Use Processing to open the file Sketch_02_1_1_FollowLight.
$ processing ~/Freenove_Kit/Processing/Sketches/Sketch_02_1_1_FollowLight/Sketch_02_1_1_FollowLight.pde
Click on “RUN” to run the code.
After the program is executed, slide the mouse in the Display Window, then the state of LED Bar Graph will be changed, as shown below.
The following is program code:
1/*****************************************************
2 * Filename : Sketch_02_1_1_FollowLight
3 * Description : Use the mouse to control the LEDGraph Bar
4 * auther : www.freenove.com
5 * modification: 2024/09/04
6 *****************************************************/
7import freenove.processing.io.*;
8
9int leds[]={17, 18, 27, 22, 23, 24, 25, 2, 3, 8}; //define ledPins
10
11void setup() {
12 size(640, 360); //display window size
13 for (int i=0; i<10; i++) { //set led Pins to output mode
14 GPIO.pinMode(leds[i], GPIO.OUTPUT);
15 }
16 background(102);
17 textAlign(CENTER); //set the text centered
18 textSize(40); //set text size
19 text("Follow Light", width / 2, 40); //title
20 textSize(16);
21 text("www.freenove.com", width / 2, height - 20); //site
22}
23
24void draw() {
25 for (int i=0; i<10; i++) { //draw 10 rectanglar box
26 if (mouseX>(25+60*i)) { //if the mouse cursor on the right of rectanglar box
27 fill(255, 0, 0); //fill the rectanglar box in red color
28 GPIO.digitalWrite(leds[i], GPIO.LOW); //turn on the corresponding led
29 } else {
30 fill(255, 255, 255); //else fill the rectanglar box in white color and turn off the led
31 GPIO.digitalWrite(leds[i], GPIO.HIGH);
32 }
33 rect(25+60*i, 90, 50, 180); //draw a rectanglar box
34 }
35}
In the function draw(), we draw 10 rectangles to represent 10 LEDs of LED Bar Graph. We make rectangles on the left of mouse filled with red, corresponding LEDs turned ON. And make We make rectangles on the right of mouse filled with red, corresponding LEDs turned OFF. In this way, when slide the mouse to right, the more LEDs on the left of mouse will be turned ON. When to the left, the reverse is the case.
1void draw() {
2 for (int i=0; i<10; i++) { //draw 10 rectanglar box
3 if (mouseX>(25+60*i)) { //if the mouse cursor on the right of rectanglar box
4 fill(255, 0, 0); //fill the rectanglar box in red color
5 GPIO.digitalWrite(leds[i], GPIO.LOW); //turn on the corresponding led
6 } else {
7 fill(255, 255, 255); //else fill the rectanglar box in white color and turn off the led
8 GPIO.digitalWrite(leds[i], GPIO.HIGH);
9 }
10 rect(25+60*i, 90, 50, 180); //draw a rectanglar box
11 }
12}


