Logo

FNK0017

  • Welcome
    • How to Start
    • Unpack
    • Get Support
    • Attention
    • About
    • Copyright

Preface

  • Preface
    • Control Board
    • Programming Software

First Use

  • First Use

Projects

  • 1. Chapter LED Blink
    • 1.1. Project Control LED with Manual Button
      • 1.1.1. Component List
      • 1.1.2. Circuit Knowledge
        • 1.1.2.1. Power supply
        • 1.1.2.2. Voltage
        • 1.1.2.3. Current
        • 1.1.2.4. Resistor
      • 1.1.3. Component Knowledge
        • 1.1.3.1. Jumper
        • 1.1.3.2. Breadboard
        • 1.1.3.3. Push Button Switch
        • 1.1.3.4. LED
      • 1.1.4. Circuit
    • 1.2. Project Control LED with Control Board
      • 1.2.1. Component List
      • 1.2.2. Circuit Knowledge
        • 1.2.2.1. Analog signal and Digital signal
        • 1.2.2.2. Low level and high level
      • 1.2.3. Code Knowledge
        • 1.2.3.1. Comments
        • 1.2.3.2. Data type
        • 1.2.3.3. Constant
        • 1.2.3.4. Variable
        • 1.2.3.5. Function
      • 1.2.4. Circuit
      • 1.2.5. Sketch
        • 1.2.5.1. Sketch 1.2.1
          • Reset()
  • 2. Chapter Two LEDs Blink
    • 2.1. Project Two LEDs Blink
      • 2.1.1. Component List
      • 2.1.2. Code Knowledge
        • 2.1.2.1. Parameters of function
        • 2.1.2.2. Boolean data type
        • 2.1.2.3. Logical operator
        • 2.1.2.4. Relation operator
        • 2.1.2.5. Conditional statement
      • 2.1.3. Circuit
      • 2.1.4. Sketch
        • 2.1.4.1. Sketch 2.1.1
        • 2.1.4.2. Sketch 2.1.2
        • 2.1.4.3. Sketch 2.1.3
  • 3. Chapter LED Bar Graph
    • 3.1. Project LED Bar Graph Display
      • 3.1.1. Component List
      • 3.1.2. Component Knowledge
        • 3.1.2.1. LED bar graph
      • 3.1.3. Code Knowledge
        • 3.1.3.1. Array
      • 3.1.4. Circuit
      • 3.1.5. Sketch
        • 3.1.5.1. Sketch 3.1.1
        • 3.1.5.2. Sketch 3.1.2
  • 4. Chapter LED Blink Smoothly
    • 4.1. Project LEDs Emit Different Brightness
      • 4.1.1. Component List
      • 4.1.2. Circuit Knowledge
        • 4.1.2.1. PWM
      • 4.1.3. Code Knowledge
        • 4.1.3.1. Return value of function
        • 4.1.3.2. return
      • 4.1.4. Circuit
      • 4.1.5. Sketch
        • 4.1.5.1. Sketch 4.1.1
          • analogWrite()
          • map()
    • 4.2. Project LED Blinking Smoothly
      • 4.2.1. Component List
      • 4.2.2. Circuit
      • 4.2.3. Sketch
        • 4.2.3.1. Sketch 4.2.1
  • 5. Chapter Control LED with Push Button Switch
    • 5.1. Project Control LED with Push Button Switch
      • 5.1.1. Component List
      • 5.1.2. Circuit Knowledge
      • 5.1.3. Circuit
      • 5.1.4. Sketch
        • digitalRead()
    • 5.2. Project Change LED State with Push Button Switch
      • 5.2.1. Component List
      • 5.2.2. Circuit Knowledge
        • 5.2.2.1. Debounce a push button switch
      • 5.2.3. Circuit
      • 5.2.4. Sketch
        • 5.2.4.1. Sketch 5.2.1
  • 6. Chapter Serial
    • 6.1. Project Send Data through Serial
      • 6.1.1. Component List
      • 6.1.2. Code Knowledge
        • 6.1.2.1. Bit and Byte
      • 6.1.3. Circuit Knowledge
        • 6.1.3.1. Serial and parallel communication
        • 6.1.3.2. Serial communication
        • 6.1.3.3. Serial port on Control board
      • 6.1.4. Circuit
      • 6.1.5. Sketch
        • 6.1.5.1. Sketch 6.1.1
    • 6.2. Project Receive Data through Serial Port
      • 6.2.1. Component List
      • 6.2.2. Code Knowledge
        • 6.2.2.1. Interrupt
      • 6.2.3. Circuit
      • 6.2.4. Sketch
        • 6.2.4.1. Sketch 6.2.1
        • 6.2.4.2. Sketch 6.2.2
    • 6.3. Project Application of Serial
      • 6.3.1. Component List
      • 6.3.2. Circuit
      • 6.3.3. Sketch
        • 6.3.3.1. Sketch 6.3.1
          • constrain()
  • 7. Chapter ADC
    • 7.1. Project ADC
      • 7.1.1. Component List
      • 7.1.2. Circuit Knowledge
        • 7.1.2.1. ADC
      • 7.1.3. Component Knowledge
        • 7.1.3.1. Potentiometer
        • 7.1.3.2. Rotary potentiometer
      • 7.1.4. Circuit
      • 7.1.5. Sketch
        • 7.1.5.1. Sketch 7.1.1
    • 7.2. Project Control LED by Potentiometer
      • 7.2.1. Component List
      • 7.2.2. Circuit
      • 7.2.3. Sketch
        • 7.2.3.1. Sketch 7.2.1
    • 7.3. Project Control LED by Potentiometer
      • 7.3.1. Component List
      • 7.3.2. Component Knowledge
        • 7.3.2.1. Photoresistor
      • 7.3.3. Circuit
      • 7.3.4. Sketch
        • 7.3.4.1. Sketch 7.3.1
  • 8. Chapter RGB LED
    • 8.1. Project Control RGB LED through Potentiometer
      • 8.1.1. Component List
      • 8.1.2. Component Knowledge
        • 8.1.2.1. RGB LED
      • 8.1.3. Circuit
      • 8.1.4. Sketch
        • 8.1.4.1. Sketch 8.1.1
    • 8.2. Project Multicolored LED
      • 8.2.1. Component List
      • 8.2.2. Circuit
      • 8.2.3. Sketch
        • 8.2.3.1. Sketch 8.2.1
          • random()
  • 9. Chapter Buzzer
    • 9.1. Project Active Buzzer
      • 9.1.1. Component List
      • 9.1.2. Component knowledge
        • 9.1.2.1. Transistor
        • 9.1.2.2. Buzzer
      • 9.1.3. Circuit
      • 9.1.4. Sketch
        • 9.1.4.1. Sketch 9.1.1
    • 9.2. Project Passive Buzzer
      • 9.2.1. Component List
      • 9.2.2. Circuit
      • 9.2.3. Sketch
        • 9.2.3.1. Sketch 9.2.1
          • tone()
  • 10. Chapter Motor
    • 10.1. Project Control Motor by Relay
      • 10.1.1. Component List
      • 10.1.2. Component Knowledge
        • 10.1.2.1. DC Motor
        • 10.1.2.2. Capacitor
        • 10.1.2.3. Relay
        • 10.1.2.4. Inductor
        • 10.1.2.5. Circuit
      • 10.1.3. Sketch
        • 10.1.3.1. Sketch 10.1.1
          • millis()
    • 10.2. Project Control Motor with L293D
      • 10.2.1. Component List
      • 10.2.2. Component Knowledge
        • 10.2.2.1. L293D
      • 10.2.3. Circuit
      • 10.2.4. Sketch
        • 10.2.4.1. Sketch 10.2.1
          • abs()
  • 11. Chapter Servo
    • 11.1. Project Servo Sweep
      • 11.1.1. Component Knowledge
        • 11.1.1.1. Servo
      • 11.1.2. Circuit
      • 11.1.3. Sketch
        • 11.1.3.1. Sketch 11.1.1
    • 11.2. Project Control Servo with Potentiometer
      • 11.2.1. Component List
      • 11.2.2. Circuit
      • 11.2.3. Sketch
        • 11.2.3.1. Sketch 11.2.1
  • 12. Chapter Temperature Sensor
    • 12.1. Project Detect the Temperature
      • 12.1.1. Component List
      • 12.1.2. Component Knowledge
        • 12.1.2.1. Thermistor
      • 12.1.3. Circuit
      • 12.1.4. Sketch
        • 12.1.4.1. Sketch 12.1.1
  • 13. Chapter Joystick
    • 13.1. Project Joystick
      • 13.1.1. Component List
      • 13.1.2. Component Knowledge
        • 13.1.2.1. Joystick
      • 13.1.3. Circuit
      • 13.1.4. Sketch
        • 13.1.4.1. Sketch 13.1.1
          • INPUT_PULLUP()
  • 14. Chapter Acceleration sensor
    • 14.1. Project Acceleration Detection
      • 14.1.1. Component List
      • 14.1.2. Component Knowledge
        • 14.1.2.1. I2C communication
        • 14.1.2.2. MPU6050
      • 14.1.3. Circuit
      • 14.1.4. Sketch
        • 14.1.4.1. Sketch 14.1.1
  • 15. Chapter LED Matrix
    • 15.1. Project 74HC595
      • 15.1.1. Component List
      • 15.1.2. Code Knowledge
        • 15.1.2.1. Hexadecimal
      • 15.1.3. Component Knowledge
        • 15.1.3.1. 74HC595
      • 15.1.4. Circuit
      • 15.1.5. Sketch
        • 15.1.5.1. Sketch 15.1.1
    • 15.2. Project LED Matrix
      • 15.2.1. Component List
      • 15.2.2. Component Knowledge
        • 15.2.2.1. LED matrix
      • 15.2.3. Circuit
      • 15.2.4. Sketch
        • 15.2.4.1. Sketch 15.2.1
  • 16. Chapter LCD1602
    • 16.1. Project Display the String on LCD1602
      • 16.1.1. Component List
      • 16.1.2. Component Knowledge
        • 16.1.2.1. I2C LCD1602
      • 16.1.3. Circuit
      • 16.1.4. Sketch
        • 16.1.4.1. Sketch 16.1.1
    • 16.2. Project I2C LCD1602 Clock
      • 16.2.1. Code Knowledge
        • 16.2.1.1. Timer
      • 16.2.2. Circuit
      • 16.2.3. Sketch
        • 16.2.3.1. Sketch 16.2.1
  • 17. Chapter Digital Display
    • 17.1. Project 1-digit 7-segment Display
      • 17.1.1. Component List
      • 17.1.2. Component Knowledge
        • 17.1.2.1. 1-digit 7-segment display
      • 17.1.3. Sketch
        • 17.1.3.1. Sketch 17.1.1
    • 17.2. Project 4-digit 7-segment Display
      • 17.2.1. Component Knowledge
        • 17.2.1.1. 4-digit 7-segment display
      • 17.2.2. Circuit
      • 17.2.3. Sketch
        • 17.2.3.1. Sketch 17.2.1
        • 17.2.3.2. Sketch 17.2.2
  • 18. Chapter Stepper Motor
    • 18.1. Project Drive Stepper Motor
      • 18.1.1. Component List
      • 18.1.2. Component Knowledge
        • 18.1.2.1. Stepper Motor
        • 18.1.2.2. ULN2003 stepper motor driver
      • 18.1.3. Circuit
      • 18.1.4. Sketch
        • 18.1.4.1. Sketch 18.1.1
  • 19. Chapter Matrix Keypad
    • 19.1. Project Get Input Characters
      • 19.1.1. Component List
      • 19.1.2. Component Knowledge
        • 19.1.2.1. 4x4 keypad
      • 19.1.3. Circuit
      • 19.1.4. Sketch
        • 19.1.4.1. Sketch 19.1.1
    • 19.2. Project Combination Lock
      • 19.2.1. Component List
      • 19.2.2. Circuit
      • 19.2.3. Sketch
        • 19.2.3.1. Sketch 19.2.1
  • 20. Chapter Vibration Switch
    • 20.1. Project Detect Vibration
      • 20.1.1. Component List
      • 20.1.2. Component Knowledge
        • 20.1.2.1. Vibration Switch
      • 20.1.3. Circuit Knowledge
      • 20.1.4. Circuit
      • 20.1.5. Sketch
        • 20.1.5.1. Sketch 20.1.1
  • 21. Chapter Infrared Remote
    • 21.1. Project Infrared Remote Control
      • 21.1.1. Component List
      • 21.1.2. Component Knowledge
        • 21.1.2.1. Infrared remote
        • 21.1.2.2. Infrared receiver
      • 21.1.3. Circuit
      • 21.1.4. Sketch
        • 21.1.4.1. Sketch 21.1.1
    • 21.2. Project Control LED through Infrared Remote
      • 21.2.1. Component List
      • 21.2.2. Circuit
      • 21.2.3. Sketch
        • 21.2.3.1. Sketch 21.2.1
  • 22. Chapter Temperature & Humidity Sensor
    • 22.1. Project Temperature & Humidity Sensor
      • 22.1.1. Component List
      • 22.1.2. Component Knowledge
        • 22.1.2.1. DHT11
      • 22.1.3. Circuit
      • 22.1.4. Sketch
        • 22.1.4.1. Sketch 22.1.1
  • 23. Chapter Infrared Motion Sensor
    • 23.1. Project Infrared Motion Sensor
      • 23.1.1. Component List
      • 23.1.2. Component Knowledge
        • 23.1.2.1. Infrared Motion Sensor
      • 23.1.3. Circuit
      • 23.1.4. Sketch
        • 23.1.4.1. Sketch 23.1.1
  • 24. Chapter Ultrasonic Ranging
    • 24.1. Project Ultrasonic Ranging
      • 24.1.1. Component List
      • 24.1.2. Component Knowledge
        • 24.1.2.1. Ultrasonic ranging module
      • 24.1.3. Circuit
      • 24.1.4. Sketch
        • 24.1.4.1. Sketch 24.1.1
          • pulseIn()
        • 24.1.4.2. Sketch 24.1.2
  • 25. Chapter Soldering Circuit Board
    • 25.1. Project Solder a Buzzer
      • 25.1.1. Component List
      • 25.1.2. Circuit
      • 25.1.3. Solder the Circuit
    • 25.2. Project Solder a Flowing Water Light
      • 25.2.1. Component List
      • 25.2.2. Solder the Circuit
      • 25.2.3. Connect the Circuit
      • 25.2.4. Sketch
        • 25.2.4.1. Sketch 25.2.1

Other Components

  • Other Components
    • Component Knowledge
      • Toggle switch
      • Switch diode
      • 9V battery cable
      • Power supply module for breadboard

What's Next?

  • What’s Next?

Appendix

  • Appendix
    • ASCII Table
    • Resistor Color Code
fnk0017-docs
  • 2. Chapter Two LEDs Blink
Previous Next

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

Chapter01_00

Breadboard x1

Chapter01_01

USB cable x1

Chapter01_02

LED x2

Chapter01_03

Resistor 220Ω x1

Chapter01_04

Jumper M/M x3

Chapter01_06

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):

../../../_images/Chapter02_00.png

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.

../../../_images/Chapter02_01.png

When there are many statements needed to be executed, we can put them into “{}”:

../../../_images/Chapter02_02.png

Only the section of code in which conditions are tenable will be executed:

../../../_images/Chapter02_03.png

In addition, it can judge multiple conditions.

../../../_images/Chapter02_04.png

2.1.3. Circuit

Use pin 4 and pin 5 of the control board to drive these two LEDs respectively.

Schematic diagram

Hardware connection

Chapter02_05

Chapter02_06

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.

../../../_images/Chapter02_07.png

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.

Previous Next