$USD
  • EUR€
  • £GBP
  • $USD
PROJECTS Arduino

Arduino Project 2: S.O.S distress signal

DFRobot Mar 31 2017 3207
Related Product: Beginner Kit for Arduino

In the arduino Tutorial 2: S.O.S distress signal with the arduino starter kit, we will learn how to build a Morse code generator with the circuit.

Let’s build a Morse code generator with the circuit we built in lesson 1. Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks. We can use a slow blink and quick blink of an LED instead of dots and dashes to indicate letters of the alphabet. For example, SOS. According to Morse code, “S” is represented with 3 dots which we can represent with a slow blink, while “O” is represented with 3 dashes which we can represent with a quick blink.


Sample code 2-1:
int ledPin = 10;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {


// 3 quick blinks to represent “S”
digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin,LOW);
delay(100);


digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin,LOW);
delay(100);


digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin,LOW);
delay(100);


delay(100); //100 milliseconds as a break of each letter


//3 quick blinks to represent “0”
digitalWrite(ledPin,HIGH);
delay(400);
digitalWrite(ledPin,LOW);
delay(100);


digitalWrite(ledPin,HIGH);
delay(400);
digitalWrite(ledPin,LOW);
delay(100);


digitalWrite(ledPin,HIGH);
delay(400);
digitalWrite(ledPin,LOW);
delay(100);


delay(100);
// 100 milliseconds delay between each letter

//3 quick blinks to represent “S” again
digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin,LOW);
delay(100);


digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin,LOW);
delay(100);


digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin,LOW);
delay(100);


delay(5000); // wait 5 seconds to repeat the next S. O.S signal
}


CODE

It requires a lot of repetitive work to code like this. Is there a better way? Take a look at the following sample code.

Sample code 2-2:

//The second project -- S.O.S signal
int ledPin = 10;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
//3 quick blinks to represent “S” again
for(int x=0;x<3;x++){
digitalWrite(ledPin,HIGH);                                     //configure LED on
delay(150);                                                              //delay 150 milliseconds

digitalWrite(ledPin,LOW);                                    //configure LED off

delay(100);                                                      //delay 100 milliseconds

}


//100 milliseconds delay between of each letter
delay(100);


//3 quick blinks to represent “O”
for(int x=0;x<3;x++){
digitalWrite(ledPin,HIGH);                                    //configure LED on
delay(400);                                                        //delay 400 milliseconds    
digitalWrite(ledPin,LOW);                                    //configure LED off
delay(100);                                                          //delay 100 milliseconds  
}


//100 milliseconds delay between of each letter
delay(100);


// 3 quick blinks to represent “S” again
for(int x=0;x<3;x++){
digitalWrite(ledPin,HIGH);                                 //configure LED on 
delay(150);                                                          // delay 150 milliseconds
digitalWrite(ledPin,LOW);                                 //configure LED off
delay(100);                                                         //delay 100 milliseconds
}


// wait 5 seconds to repeat the next S.O.S signal
delay(5000);
}

After uploading the code, you will see the LED blinking the S.O.S signal and repeating it after 5 seconds. If you were to put the circuit in to a water-proof case, you could use it for sailing or hiking!



CODE

The first part of the two sketches are identical: we have initialized a variable and configured digital pin 10 to carry out the output signal. In the main code loop(), you can find lines similar to the last project to turn the LED on and off. The difference here is that the main code contains 3 independent blocks of statements.

The first block is to output 3 dots.


for(int x=0;x<3;x++){
digitalWrite(ledPin,HIGH);                //configure LED on
delay(150);                                         //delay 150 milliseconds
digitalWrite(ledPin,LOW);                //configure LED off
delay(100);                                        //delay 100 milliseconds

}


The “for” statement
The “for” statement is used to repeat a block of statements enclosed in brackets. An increment counter is usually used to increment and terminate the loop. The “for” statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/ pins.
There are three parts to the “for” loop header:

So for the “for“ statement in the
sketch
for(int x=0;x<3;x++){
}


Step 1: Interlize value of variable x as 0
Step 2: Evaluate if x is less than 3.
Step 3: If it is valid, execute the following statement
Step 4: x increases and becomes 2.
Step 5: Repeat Step 2 less than 3.
Step 6: Repeat step 3


Until x=3, the condition of x<3 is not valid then the program skips over the code.


We set x<3 to have it repeat 3 times. Calculating from 0 to 2, it repeats 3 times.

    
If we wanted it to repeat 100 times, we can use the following code: for(int x=0;x<100;x++){}


Some comparison operators like ">", "<" are frequently used in programming conditional statements. They will covered in more detail in the next section.


Here are some common operators that you might use:

"<" is an example of a comparison operator. Comparison operators allow the Arduino to compare two values. Below are some more examples of frequently used comparison operators:
==(equal)
!=(not equal)
<(less than)
>(greater than)
<=(less than or equal)
>=(greater than or equal)

Beware of accidentally using a single equals sign (e.g. if (x = 10) ).
The single equals sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). A double equals sign is comparison operator instead (e.g. if (x == 10) ),
Also, be careful that there is no space between <= or >=.


You can also use arithmetic operators such as + - * / .


Now you should know how the for loop operates. There are 3 for loops in the code. The first for loop repeats 3 times and the long-lasting flashing alternates 3 times, giving an output of 3 dots representing the letter S in Morse code. The second for loop also repeats 3 times and the temporary flashing alternates 3 times, giving an output of 3 dashes , representing the letter O in Morse code. The third for loop is identical to the first, and also outputs an S in Morse code.


You might notice there are various x variables in different blocks of code, but they don’t interfere with each other. Because it has a limited scope in a specific block of function. It is the counterpart of global variables that needs to declare at the top of the code out of setup() and loop()function and you will be able to use it anywhere in the code.


Exercise

Let’s make some traffic lights by using 3 digital pins to control 3 LED lights

.



Related category: arduino kits > education kits
Last Arduino Tutorial 1: LED Flashing

Next Arduino Tutorial 3: Interactive traffic lights


REVIEW