Introduction
What if our robot gets in trouble half way across the planet? Let's make a program so it can transmit S.O.S in Morse Code to let us know if its in trouble!
Tools
No tools specified.
-
-
Having red/green LEDs is a great start to communicating with our robot
-
What if we want to communicate more than just red or green?
-
Morse Code allows us to send any letter or number we like, just using a single light or buzzer!
-
Watch the video to find out more.
-
-
-
It's your turn to try and decode some morse code into words!
-
See if you can work out the sequence of letters in the video using the decoding card.
-
Make sure you get this challenge checked off when you know the answer!
-
-
-
Let's write a program that will send the letter S.
-
In the picture is a program to send 1 dot with the red LED. For your delay() lines, we suggest:
-
100 milliseconds for a dot
-
1000 milliseconds for a dash
-
Extend the program in the picture to send 3 dots, which is an S - your LED module should still be plugged in to a1 like the last lesson!
-
-
-
Good programs always try not to repeat code that does the same thing, so the program is as small and efficient as possible.
-
We could put the code for one dot in the loop, but this would just do dots forever - not what we want!
-
We are going to use a for loop to repeat something a set number of times.
-
-
-
A for loop takes 4 inputs:
-
Initialiser - this is used to count how many times the loop runs, and is usually called i
-
Condition - the loop runs until this is false. For example, this for loop would run so long as i is less than 3.
-
Incrementor - this is how much i is changed by, every time the loop runs. In this case, for every run of the loop, i is increased by 1 (++ just means add 1)
-
So this for loop starts with i=0. After the first loop run, i is increased to 1. After the second it is increased to 2, and after the third run it is increased to 3. When i=3, i is not <3, so the loop stops after 3 runs.
-
-
-
Put your code to send a dot with the red LED inside the for loop, so your robot sends an 's' with much less code.
-
Try changing the loop inputs, and see how it affects how many dots get sent!
-
-
-
Indentation is very important in programming - it makes it easy for others to read your code, and for you to read it later!
-
You can indent a line by pressing the TAB key.
-
Generally, if a line of code is inside something else, it should be indented more. For example, invent.begin(); is inside the setup, so it is indented 1 level
-
Our digitalWrite lines are inside both the setup, and the for loop, so they are indented 2 levels.
-
Try and make sure all your programs are indented properly! Ask your tutor if you are unsure.
-
-
-
You might have noticed when you were listening to the code, that between the letters we need longer gaps so you can tell when they start and finish.
-
A time of 2 seconds works well.
-
Put your for loop that flashes an S inside the main program loop, so it flashes S forever, and add a wait block so there is a gap of seconds between each S.
-
Make sure it is indented properly!
-
-
-
Let's make a program to get our robot to flash SOS using an LED, in case it has a problem.
-
Your program should flash the sequence of dots and dashes required for the letters SOS - for bonus points put it in the loop to make it flash SOS forever!
-
Try to shorten your program using for loops - you will need 3 separate loops, or 2 if you're really clever!
-
-
-
Using lights for Morse Code is great for long distances, such as between two ships, but what if you are looking the other way when your robot is in trouble?
-
Replace your LED module with the buzzer module like in the picture, so your robot buzzes SOS instead.
-
If you're feeling really clever, put the LED module back into another output and add some more code so it flashes and buzzes the sequence for S.O.S!
-