Turn on the external LED light module | MindPlus Coding Kit for Arduino Started Tutorial E02
userhead
AshG 2021-06-09 18:47:31
projectImage

In the last episode projects 1, we learned how to light up the UNO onboard LED light, and then we will learn how to control an external LED light.

projectImage

Task Navigation 

1. Learn variables and constants

2. Make an LED light that flickers at an increasing rate.

Key Points Analysis 

What are variables and constants?

A variable holds a value that can change or vary in a changeable process.

A constant holds value that stays the same during a process of change.

Here, we can think the variable as a box for storing data, and it can store a variety of data temporarily. For example, we use it to store integers. After 1 is stored, 2 is stored; 1 will be replaced by 2 and we can only get 2 from the box. In the same way, if you store 3,4,5 ... the value will be replaced by latest data in order.

Constants can be understood as a certain data in the box. For example, the number in the lottery box. When you draw the number 10, this number is a constant.

projectImage

Command List 

projectImage

Hands-On 

1. Hardware connection

LED lights are solid-state semiconductors that convert electrical energy into visible light. When connecting ordinary LED lights and Arduino, a protection resistor in series connection is required. Since the LED light emitting module in the figure below integrates LED lights and protection resistors, and leads to a 3-pin interface, we can directly connect the 3-pin interface from the LED light module to the Digital Pin 10 of the Arduino mainboard.

projectImage

Hardware connection (LED -10)

 

Please match the colors when plugging

2. Programming

Modify the pins according to the reference program of the previous task.

projectImage

Upload the modified program to the Arduino main control board, and you can see the flickering LED lights. The current project now makes the LED light flicker every second. What if I want to make the LED light flicker faster and faster? How should it be achieved?

With two previous projects, we get that flickering effects are achieved by delay. To make the LED light flicker faster and faster, we need to gradually shorten the delay time for the small bulb to be on, thereby causing the flicker effect to go faster. Let’s have a look at this program.

projectImage

It’s easy to see that this program consists of a repeated small piece of program. According to the principle of sequential execution described earlier, a set of modules will be executed from top to bottom. If we want to execute a lot of times, such as 100, are we really going to using 100 such blocks?

projectImage

We know the program will be extremely lengthy just by thinking of it. Maybe you don’t even remember how many times you’ve dragged at the end. So, is there a function that can repeat the above process automatically? Yes, we are going to talk about it next.

1)  Create variables

Step 1. Find the variable module

Click “Make a Numeric Variable” to create the variable "i"; the steps are as shown below.

projectImage
projectImage

Step 3. After creating the variable, we can set up the number of the loops. This process is determined by “until loop” (conditional loop).

projectImage

3. Program’s effect

The LED light will flicker faster and faster.

4. Program analysis

The above program implements the idea of "repetition". It is the commonly used “until loop” structure in programming.

projectImage

Until loop structure in Mind+

In the first section, we introduced that until loop is a conditional loop. It means that after executing the loop body once, the condition is determined (the value of i is determine here), the execution is continued if the condition is not met, and the loop only ends when the condition is met.

The flowchart is as follows:

projectImage

Until loop flowchart

So from the program we can see that the number of loops is controlled by the value of the variable "i". During the execution, the variable "i" starts from 1 and is decremented by 0.1 step size each time. (See further reading: What is the step size? )

So at the second execution, i=1-0.1=0.9; the third i=0.9-0.1=0.8, the fourth, i=0.8-0.1=0.7…

In this way, it is decremented until "i"

projectImage

Note: each cycle will execute the program once, which controls the LED to turn on and off.

projectImage

Further reading 

What is the step size?

The step size is the value of increment or decrement each time. If the step size is 1, the value is increased by 1 for each execution; if the step size is -1, the value is decreased by 1 for each execution. In the previous program, we set the initial value to 1. In order to make the LED light flicker faster and faster, we decreased it by 0.1.

Example: an operation is to be performed on the parameter N. Assuming that the step size is M, an operation is performed on N, then assign N + M to N. And then use the new N (N + M) value to perform the operation, and so on. As explained in the program above.

As we learn Arduino, we will be exposed to a variety of input devices. Among them, the button switch is the simplest and most widely used one. Here we will use Arduino to control the LED lights to make button pressing switch the light on and off every other time.

FILE
Program 1-2.rar 165KB Download 0
License
1
All Rights
Reserved
[[i.text]]
licensBg
0
0
COMMENTS ([[commentTotla]])
[[c.user_name]] [[c.create_time]]
[[c.parent_comment.count]]