Did you successfully finish the add-activity in the last chapter? If not, it doesn't matter. After completing this chapter, you will find it is just a piece of cake. The project here is to make a button-controlled traffic light. The Arduino will execute when the button is pressed, and by changing the state of light, we can make the cars stop and allow the pedestrian to cross safely.
We are going to learn how to interact with Arduino and define a block in Mind+ here, so the number of codes in this project is relatively large. But be patient, you will benefit a lot from this section.
Here list 6 resistors, 5 of them are for the 5 LEDs to reduce current, how about the last one? Actually, it is for the button, we call it pull-down resistor.
Connect all parts together as the figure 3-1 shows. It’s a little bit complicated, please check carefully before powering up the module. The light-green lines marked on the breadboard represent socket connection and are not real wires.
Note: always turn off the power before you connect.
Variable and Block will be used in this project. Let’s learn how to make a variable and define a block in Mind+.
Make a Variable
Step1: click “Make a Numeric Variable”
Step 2: input a name for your variable. It can begin with letter, underscore, digits, or Chinese characters(Chinese will be converted into Pinyin in the auto-generated codes).
Step 3: the new variable "test" has been created.
Define a block
Step1: click “Make a Block”
Step 2: input a name for the function.
Step 3: the block we created "define function" will appear in the command section, we can place other blocks under this function to define it:
Step 4: drag this block into program to call it.
Input Example Program
Connect the Arduino board and your computer, open Mind+ and load the Arduino library. Complete the program shown in picture 3-1:
Three variables are required here. You can jump to the command learning part if encountering problems in using new blocks.
Click “Upload” to download the program into Arduino. The left three LEDs are for cars and the rest two are for pedestrians. Firstly, the green traffic light and the red pedestrian light turn on to allow cars to pass. Once you pressed the button, the pedestrian light changes from red to green, and the traffic light changes from green to yellow and red, then pedestrians can go. There is a Cross Time for people to cross the road. When it comes to an end, the green pedestrians flash quickly to notify people. After that, all lights back to the initial state.
It seems to be very complex, but don’t worry, we will lead you step by step to learn all commands used in the codes. You will comprehend the entire program soon.
1.// Dynamic variable
2.volatile float mind_n_Cross_Time, mind_n_Change_Time, mind_n_Button_State;
3.// Function declaration
4.void DF_Change_Light();
5.
6.
7.// Main program starts
8.void setup() {
9. mind_n_Cross_Time = 5;
10. mind_n_Change_Time = 0;
11. digitalWrite(10, HIGH);
12. digitalWrite(8, HIGH);
13.}
14.void loop() {
15. mind_n_Button_State = digitalRead(9);
16. if (((mind_n_Button_State==1) && ((millis()-mind_n_Change_Time)>5000))) {
17. DF_Change_Light();
18. }
19.}
20.// Define function
21.void DF_Change_Light() {
22. digitalWrite(10, LOW);
23. digitalWrite(11, HIGH);
24. delay(2000);
25. digitalWrite(11, LOW);
26. digitalWrite(12, HIGH);
27. delay(1000);
28. digitalWrite(8, LOW);
29. digitalWrite(7, HIGH);
30. delay(mind_n_Cross_Time * 1000);
31. for (int index = 0; index < 10; index++) {
32. digitalWrite(7, HIGH);
33. delay(250);
34. digitalWrite(7, LOW);
35. delay(250);
36. }
37. digitalWrite(8, HIGH);
38. delay(500);
39. digitalWrite(12, LOW);
40. digitalWrite(11, HIGH);
41. delay(1000);
42. digitalWrite(11, LOW);
43. digitalWrite(10, HIGH);
44. mind_n_Change_Time = millis();
45.}
Let’s begin with the first line.
This is the text description of the code, we call it comment.
Comment
In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters. The syntax of comments varies from programming languages.
C++ has block comments delimited by /* and */ that can span multiple lines and line comments delimited by //.
For example:
Then let’s move to the third and fourth lines.
Function Declaration
In graphical programming, before calling a function, we have to create and name it first, similarly, in coding, a function must be declared first when defining or calling it. Most of the functions used in this tutorial do not come with input and output. The format is shown below:
void FunctionName ();
The format of function with input and output will be given later.
The second line of code is used to declare a variable.
Every time we use a new variable, we have to declare it first. Here are the blocks they correspond with:
What is a Variable?
We can compare a variable as a container or box where we can store things. The thing we put into the box must be smaller than the box itself, otherwise, it may overflow. Same as that, the data stored in variable should be within a certain range. Sometimes the value of variable could be changed in the process of programming running, and we will analyze it deeply later.
We have learned how to make and name a variable in Mind+’s graphical programming part, however, there are a lot of rules for naming a variable when coding manually. For instance, in C:
● Variable name must begin with letter or underscore.
● Variables are case sensitive.
● They can be constructed with digits, letters.
● No special symbols are allowed other than underscore.
● Some specified words like, main, if, or while are unacceptable.
Can the box capacity of a variable be infinitely big?
Do variables have different sizes? The answer is Yes. Seen as the chart 3-1.
There are various types of variables corresponding with different data types. Int and long are for integers, char for characters, float and double for variables with decimal point.
Theoretically, the size of the variable box can be all the same, however, here is the thing, the storage space for a micro-controller is limited. There are only 32K flash memory for Arduino Uno main chip (Atmega328) so if we could save some storage space, why not do it?
In Mind+’s graphical programming, the numerical variable is set to be the type of float with volatile specifier, which is compatible with decimal, integers or variables in interrupt. When coding manually, the type and specifier of variables can be selected freely.
Global variable and local variable
Here a new knowledge point needs to be introduced: global variable and local variable.
Global variables, the variable declared in the second line, are declared outside any function, and they can be accessed on any function in the program.
The index in the 32nd line is a local variable.
Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name with different functions. In project 2, every for loop has a variable index, but they do not conflict with each other since every index can be used only in its own loop.
We will find a new function in line 15: digitalRead()
■ digitalRead()
The format is shown below:
The related block in Mind+:
This function is used to read the state of the digital pin, High or Low(Hight is 1, low is 0). A parameter pin has to be transferred to the function. In the project, we have to read the button signal, and the button is connected pin 9.
The read button signal will be passed to the variable mind_n_Button_State. When the variable is equal to 1(High), the button is pressed, if it is 0(Low), button unpressed.
We can directly check the value of Button State to determine if the button is pressed.
Here comes a new statement---if.
■ if Statement
An if statement is a programming conditional statement that, if proved true, performs a function or displays information, if not, exit if statement.
The basic format of if statement is:
The expression refers to the condition, if it is evaluated to be true, the statement block will get executed, or it will get skipped.
In line 15, the first condition is Button State is HIGH. When the button is pressed, the Button State will be HIGH. The second condition is the value millis() minus Chang Time is over 5000. The two conditions are linked by “&&”, a logic operator that returns True if both operands are True and returns False otherwise.
There is a new function in the if statement above.
■ millis()
It returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow(go back to zero), after approximately 50 days.
Here we use it to calculate if there is a break more than 5s when the button is pressed once again. If there is not, the codes will be skipped to avoid errors that may be caused by pressing the button accidentally.
Logical Operators
The common operators:
■ &&-------AND (Returns true if both operands are true and false otherwise)
■ ||--------OR (Any of its arguments are true, it returns true, otherwise, false)
■ !--------NOT (Returns the inverse value)
The 1 and 0 in the truth table can represent High/Low, or True/False. For example, (10 > 8) && (98 is true so the return is 1. 9
Button Switch
The button we used here has 4 pins. Once it is pressed, the left and right sides will be connected while the top and bottom are always connected.
A push-button can be used to control the electricity flowing on the circuit. In the project, when the button is pressed, D Pin 9 will be connected to 5V, and it will be read as High. Otherwise, it is in Low state(GND).
What is a Pull-down Resistor?
Pull-down resistors are used in electronic logic circuits to ensure that inputs to the Arduino settle at expected logic levels if external devices are disconnected or are at high-impedance. As the name suggests, a pull-down resistor weakly “pull” the voltage of the wire it is connected to towards its voltage source level when the other components on the line are inactive.
When the switch on the line is open, it has a high impedance and acts like it is disconnected. Since the other components act as though they are disconnected, the circuit acts as though it is disconnected, and the pull-down resistor brings the wire up to the low logic level. When another component on the line goes active, it will override the low logic level set by the pull-down resistor. The pull-down resistor assures that the wire is at a defined low logic level even if no active devices are connected to it.
1. Input codes into Mind+, never miss any chance to practice! But be careful, don’t lose any necessary elements otherwise it will fail to compile, just like the picture shown below:
2. Use 6 LEDs to make a lighting effect of water flowing.
3. Light up the LEDs from the middle towards both sides.
4. Light up from left to right in 1, 2, 3....