This project is based on the circuit we built in project 1, and we are gonna change the codes to turn the LED lighting into the S.O.S distress signal. S.O.S is an international call for help. In fact, the signal isn’t even really supposed to be three individual letters. It is just a continuous Morse code string of three dots, three dashes, and three dots all run together with no space(…---…).
In the Morse code alphabet, the letter “S” is represented by three dots, and “O” is three dashes. So here we can directly use the blink of an LED to imitate the dot and dash: slow blink for dot, quick blink for dash.
Let’s take a look at this example program 2-1 before starting the project.
Example Program 2-1:
Although there is nothing wrong with the program, don’t you think it is a little bit cumbersome. If there are 100 same actions, do we have to repeat 100 times? Of course not. Actually, the programming inventors have taken this into account and provided us with a solution.
Connect your Arduino board to a computer, open Mind+ and load the Arduino UNO blocks. Input the example code shown below:
Example Program 2-2:
Click “Upload” to download the codes into Arduino Board. If everything is going well, the LED will repeatedly blink according to the Morse code equivalent of the letters in SOS. Connect your board with external power supply, and place it in a waterproof case, then you can use it as a S.O.S signal generator.
Here are the C codes of the project.
1.// Main program start
2.void setup() {
3.
4.}
5.void loop() {
6. for (int index = 0; index < 3; index++) {
7. digitalWrite(13, HIGH);
8. delay(150);
9. digitalWrite(13, LOW);
10. delay(100);
11. }
12. delay(100);
13. for (int index = 0; index < 3; index++) {
14. digitalWrite(13, HIGH);
15. delay(450);
16. digitalWrite(13, LOW);
17. delay(100);
18. }
19. delay(100);
20. for (int index = 0; index < 3; index++) {
21. digitalWrite(13, HIGH);
22. delay(150);
23. digitalWrite(13, LOW);
24. delay(100);
25. }
26. delay(500);
27.}
There are three independent code segments that begin with “for” in the loop main function. That’s the key to solving the repetition problem we met before.
for loop
The format of for loop statement:
The sequence of “for loop” is as following.
Round 1: 1 → 2 → 3 → 4
Round 2: 2 → 3 → 4 ...
End when “2” is not true.
Now let’s analyze the for loop in the program:
1.for(int index=0;index<3;index++){
2. ……
3.}
Step1: initialize the variable index=0
Step 2: judge if the index is less than 3
Step 3: if the condition in step is true, execute the following statement
Step 4: change the index by 1 (index++ means to increase the index value by 1, that is to say, index=index+1. )
Step 5: go back to step 2, determine if the index is less than 3
Step 6: repeat step 3
...
Once index=3, the condition “index
Note: when coding manually, there must be two”=” to represent “equal to”.
Besides that, arithmetic operators are also used frequently such as, +, -, *, / .
Now we have learned the usage of “for loop”. Let’s analyze the “for” statement in this program. There are three for loops: the first one has 3 repetitions, which represent 3 quick blinks (the letter “S”); the 3 repetitions of the next for loop are for three slow blinks (letter “O”); the last one indicates the letter “S”.
There is a 0.1s pause between each “for loop” to distinguish the three letters, and we set a 0.5s delay when re-executing the main function “loop”.
OK, that’s all for the SOS distress signal project. What are the takeaway points in this part?It’s quite easy to summarize, have a try!
Click “Manual Editing”, and input the codes of this project into Mind+ manually.
Right-click the up-arrow next to “Upload”, select “Compile Only” to check the codes.
As with writing, formats are critical for C code. Beautiful and compact codes can improve our efficiency a lot. Practice makes perfect!
Look at the figure below and try figuring out what caused error.
How about using Mind+ to make a traffic light. Tips: use 3 digital pins to control 3 LEDs.
Click to download the Mind+.
For more information about Mind+, please go to: https://mindplus.dfrobot.com/