Project - SOS Beacon
Let's build a Morse code generator with the circuit below. 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 a 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 three quick blinks, while "O" is represented with 3 dashes, which we can represent with three slow blinks.
HARDWARE
fig 1 SOS Beacon Circuit
CODE
Sample code 1:
int ledPin = 10;
void setup() {
pinMode(ledPin, OUTPUT); // Initialize the LED pin as an OUTPUT
}
void loop() {
// Three quick blinks to represent the letter "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(1000);
// 1000 milliseconds delay to create a gap between letters
// Three slow blinks to represent the letter "O"
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(1000);
// 1000 milliseconds delay to create a gap between letters
// Three quick blinks again to represent the letter "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(5000);
// 5000 milliseconds delay before repeating the SOS signal
}
It requires a lot of repetitive work to code like this. Is there a better way? Take a look at the following sample code 2.
Sample code 2:
// Project: SOS Beacon
int ledPin = 10;
void setup() {
pinMode(ledPin, OUTPUT); // Initialize the LED pin as an OUTPUT
}
void loop() {
// Three quick blinks to represent the letter "S"
for (int x = 0; x < 3; x++) {
digitalWrite(ledPin, HIGH); // Turn the LED ON
delay(150); // Delay for 150 milliseconds
digitalWrite(ledPin, LOW); // Turn the LED OFF
delay(100); // Delay for 100 milliseconds
}
// 1000 milliseconds delay to create a gap between letters
delay(1000);
// Three slow blinks to represent the letter "O"
for (int x = 0; x < 3; x++) {
digitalWrite(ledPin, HIGH); // Turn the LED ON
delay(400); // Delay for 400 milliseconds
digitalWrite(ledPin, LOW); // Turn the LED OFF
delay(100); // Delay for 100 milliseconds
}
// 1000 milliseconds delay to create a gap between letters
delay(1000);
// Three quick blinks again to represent the letter "S"
for (int x = 0; x < 3; x++) {
digitalWrite(ledPin, HIGH); // Turn the LED ON
delay(150); // Delay for 150 milliseconds
digitalWrite(ledPin, LOW); // Turn the LED OFF
delay(100); // Delay for 100 milliseconds
}
// Wait for 5 seconds before repeating the SOS signal
delay(5000);
}
After uploading the code, you will see the LED blinking the SOS signal and repeating it after 5 seconds.
Encase the circuit in a waterproof container, and you'll have a handy SOS beacon for sailing trips and hikes.
CODE REVIEW
The first part(before the main loop()) 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 project[Blinking A LED] to turn the LED on and off. The difference here is that the main code contains 3 independent blocks of “for” statements.
The first block is to output 3 dots.
for (int x = 0; x < 3; x++) {
digitalWrite(ledPin, HIGH); // Turn the LED ON
delay(150); // Delay for 150 milliseconds
digitalWrite(ledPin, LOW); // Turn the LED OFF
delay(100); // Delay for 100 milliseconds
}
The“for”statement :
The“for” statement is to repeat a block of statements enclosed in brackets. we call each repeat of the“for” loop an iteration. 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++) {
...
}
First Iteration:
· Declare and initialize the variable x = 0.
· Check if x < 3, which is true, so execute the program in the loop body.
· Increment x by 1, updating x to 1.
Second Iteration:
· At this point, x = 1.
· Check if x < 3, which is true, so execute the program in the loop body.
· Increment x by 1, updating x to 2.
...
Loop Termination:
When the loop's condition x < 3 is evaluated and found to be false (since x has now been incremented to 3), the loop ends and the code in the loop body stops executing. The program then continues with the rest of the code.
(x++ is a shorthand notation for x = x + 1, which increments the value of x by one each time it is encountered.)
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:
Beware of 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 a 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 loops in the code. The first for loop repeats 3 times and the quick 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 slow 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.
Local variable and global variable
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, global variables need to be declared at the top of the code out of setup() and loop() functions and you will be able to use it anywhere in the code.
EXERCISE
Let’s make a traffic lights by using 3 digital pins to control 3 LED lights.