2. Chapter Two LEDs Blink
In last chapter, we have already written code to make 1 LED blink on the control board. And now, we will try to make 2 LEDs blink for further programming study.
2.1. Project Two LEDs Blink
Now, try to make two LEDs blink on control board.
2.1.1. Component List
Control board x1 |
Breadboard x1 |
|
USB cable x1 |
LED x2 |
Resistor 220Ω x1 |
Jumper M/M x3 |
||
2.1.2. Code Knowledge
In the last chapter, we have taken a brief look at programming. Now let us learn more about the basic programming knowledge.
2.1.2.1. Parameters of function
In the last chapter, we have used a function with a parameter, such as:
1delay(1000); // wait for a second
Next, we will define a function with a parameter as below:
1void functionA(int i) {
2 i = i + 1;
3}
“i” is the parameter of this function. “int” is the type of i. When calling this function, it is necessary to enter the parameter of int type:
1functionA(1);
The input parameter will be assigned to “i” and involved in the calculation of the functionA(int i):
A function can define more than one parameter and the type of the parameters can be different:
1void functionB(int i, char j) {
2 char k = 'a';
3 i = i + 1;
4 k = j;
5}
2.1.2.2. Boolean data type
Data of Boolean type can only be assigned to “true” or “false”.
“true” generally represents a certain relationship which is tenable and correct, and “false” is the opposite.
1boolean isTrue;
2isTrue = true; // after the execution, "isTrue" is assigned to true.
3isTrue = false; // after the execution, "isTrue" is assigned to false.
In the code, the number 0 can be considered to be false, and nonzero numbers can be considered true.
2.1.2.3. Logical operator
The logic operators have “&&” (and), “||” (or), “!” (non), and the calculation object of them are boolean type. The result of logic operation is as follows:
&& |
true |
false |
true |
true |
false |
false |
false |
false |
|| |
true |
false |
true |
true |
true |
false |
true |
false |
! |
|
true |
false |
false |
true |
For example:
1boolean isTrue;
2isTrue = true && false; // after the execution, "isTrue"is assigned to false.
3isTrue = true || false; // after the execution, "isTrue"is assigned to true.
4isTrue = !true; // after the execution, "isTrue"is assigned to false.
2.1.2.4. Relation operator
Relational operator is used to judge whether the relationship of the two amount is tenable and correct. If the relationship is tenable, the result is true. Otherwise, the result is false.
For example, the results of “1<2” is true and the result of “1>2” is false:
1boolean isTrue;
2isTrue = 1 < 2; // after the execution, "isTrue"is true.
3isTrue = 1 > 2; // after the execution, "isTrue"is false.
There are other relational operators such as “==” (equal to), “>=” (greater than or equal to), “<=” (less than or equal to) and “!=” (not equal to).
2.1.2.5. Conditional statement
Conditional statements are used to decide whether or not to execute the program based on the result of judgment statement.
When there are many statements needed to be executed, we can put them into “{}”:
Only the section of code in which conditions are tenable will be executed:
In addition, it can judge multiple conditions.
2.1.3. Circuit
Use pin 4 and pin 5 of the control board to drive these two LEDs respectively.
Schematic diagram |
Hardware connection |
|---|---|
|
|
2.1.4. Sketch
In order to show the difference between using function and not using function, we will write two different sketches to make two LEDs blink.
2.1.4.1. Sketch 2.1.1
At first, use sketch without function to make two LEDs blink alternatively.
1/*
2 Sketch 2.1.1
3 Two LEDs Blink
4
5 modified 2016/5/13
6 by http://www.freenove.com
7*/
8
9// set pin numbers:
10int led1Pin = 4; // the number of the LED1 pin
11int led2Pin = 5; // the number of the LED2 pin
12
13void setup() {
14 // initialize the LED pin as an output:
15 pinMode(led1Pin, OUTPUT);
16 pinMode(led2Pin, OUTPUT);
17}
18
19void loop() {
20 digitalWrite(led1Pin, HIGH); // turn the LED1 on
21 digitalWrite(led2Pin, LOW); // turn the LED2 off
22 delay(1000); // wait for a second
23
24 digitalWrite(led1Pin, LOW); // turn the LED1 off
25 digitalWrite(led2Pin, HIGH); // turn the LED2 on
26 delay(1000); // wait for a second
27}
28
This section of code is similar to the previous section. The difference is that the amount of LEDs is two, and the two LEDs blink alternatively.
- Variable scope
In the 2nd and 3rd rows of the code above, we define two variables to store the pin number. These two variables defined outside the function are called “Global variable”, which can be called by all other functions. Variables defined inside a function is called “local variable”, which can be called only by the current function. When local variables and global variables have same names, the global variable is inaccessible within the function.
Verify and upload the code, then you will see the two LEDs blink alternatively.
2.1.4.2. Sketch 2.1.2
In the last sketch, we can see that the following two sections of the code are similar, so we will use one function to replace them to simplify the code.
1digitalWrite(led1Pin, HIGH); // turn the LED1 on
2digitalWrite(led2Pin, LOW); // turn the LED2 off
3delay(1000);
1digitalWrite(led1Pin, LOW); // turn the LED1 off
2digitalWrite(led2Pin, HIGH); // turn the LED2 on
3delay(1000); // wait for a second
Now, we will use a function to improve the above code.
1/*
2 Sketch 2.1.2
3 Two LEDs Blink
4
5 modified 2016/5/13
6 by http://www.freenove.com
7*/
8
9// set pin numbers:
10int led1Pin = 4; // the number of the LED1 pin
11int led2Pin = 5; // the number of the LED2 pin
12
13void setup() {
14 // initialize the LED pin as an output:
15 pinMode(led1Pin, OUTPUT);
16 pinMode(led2Pin, OUTPUT);
17}
18
19void loop() {
20 setLed(HIGH, LOW); // set LED1 on, and LED2 off.
21 setLed(LOW, HIGH); // set LED1 off, and LED2 on.
22}
23
24void setLed(int led1, int led2) {
25 digitalWrite(led1Pin, led1); // the state of LED1 is determined by variable led1.
26 digitalWrite(led2Pin, led2); // the state of LED2 is determined by variable led2.
27 delay(1000); // wait for a second
28}
29
In the sketch above, we integrate the 2 LED statements into one function, void setLed(int led1, int led2), and control two LEDs through the parameters led1 and led2.
1void setLed(int led1, int led2) {
2 digitalWrite(led1Pin, led1); // the state of LED1 is determined by variable led1.
3 digitalWrite(led2Pin, led2); // the state of LED2 is determined by variable led2.
4 delay(1000); // wait for a second
5}
When the function above is called, we will control the two LEDs by using different parameters as below.
1setLed(HIGH, LOW); // set LED1 on, and LED2 off.
2setLed(LOW, HIGH); // set LED1 off, and LED2 on.
Verify and upload the code, then you will see the two LEDs blink alternatively.
- HIGH and LOW
The macro is an identifier that represents a number, a statement, or a piece of code. HIGH and LOW are**
two macros. HIGH and LOW are defined in Arduino IDE as below:
#define HIGH 1
#define LOW 0
In the code, a macro is used as the content defined by itself.
For example, setLed (HIGH, LOW) is equivalent to setLed (1, 0).
Using macros can simplify the code and enhance its readability, such as INPUT, OUTPUT.
2.1.4.3. Sketch 2.1.3
In the previous section of code, we used a function that integrates two similar paragraphs of code. And we control two LEDs to blink alternatively by using two parameters. Now, let us try to use one parameter to control these two LEDs, which is achieved by conditional statements.
Now, we’ll use conditional statement to improve the code above.
1/*
2 Sketch 2.1.3
3 Two LEDs Blink
4
5 modified 2016/5/13
6 by http://www.freenove.com
7*/
8
9// set pin numbers:
10int led1Pin = 4; // the number of the LED1 pin
11int led2Pin = 5; // the number of the LED2 pin
12
13void setup() {
14 // initialize the LED pin as an output:
15 pinMode(led1Pin, OUTPUT);
16 pinMode(led2Pin, OUTPUT);
17}
18
19void loop() {
20 setLed1(HIGH); // set LED1 on, and LED2 off.
21 setLed1(LOW); // set LED1 off, and LED2 on.
22}
23
24void setLed1(int led1) {
25 digitalWrite(led1Pin, led1); // the state of LED1 is determined by variable led1.
26
27 if (led1 == HIGH) // the state of LED2 is determined by variable led1.
28 digitalWrite(led2Pin, LOW); // if LED1 is turned on, LED2 will be turned off.
29 else
30 digitalWrite(led2Pin, HIGH); // if LED1 is turned off, LED2 will be turned on.
31
32 delay(1000); // wait for a second
33}
34
Here, we rewrite the function so that we only need to set the state of LED1, and the state of LED2 can be set automatically.
Verify and upload the code, and then two LEDs blink alternatively.







