## Introduction: Programming With Arduino & MindPlus-S.O.S Distress Signal

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.

## Step 1: Graphical Programming

Let’s take a look at this example program 2-1 (shown in the image on the left) before starting the project.

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 in the image on the right)

## Step 2: Command & Code

```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.}```

## Step 3: Code Analysis

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 is shown above.

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<3” is false, the program goes out of the “for loop” and executes the next statement.

Here we need it to repeat 3 times, so we set index<3. If we need 100 times repetition, then it should be: for(int index=0;index<100;index++){}

Please note the curly braces must be in matching pairs, otherwise, there will be errors appearing when compiling code. Pay attention to the details when coding!

Here are some commonly-used operators we may use in programming:

Comparison Operators are often used to compare two values, shown in the picture above.

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.

Note: when we change the program by dragging blocks, the codes in the "Auto Generate" interface will be altered accordingly. But if you are in the "Manual Editing" interface, currently and you click "Upload", the revised codes cannot be downloaded to your board. You have to switch to "Auto Generate" to update codes to the board.

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 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.