The snap:bit is an electronic component for the Snap Circuits educational electronic kit. It features a socket for connecting the BBC micro:bit. This allows the Snap Circuits to be programmatically controlled by the micro:bit.
In previous projects, we learned how to light an LED up and even how to make it blink.
Now, with the help of some programming code on the micro:bit, we will make the LED a little bit more useful. We will turn it into a Morse code beacon that will send SOS - the international distress signal in Morse code.
What is Morse code?The Morse code is a communication scheme for encoding text as a sequence of dots and dashes.
The image below describes how letters and digits are encoded in Morse code.
If you wonder how they came with this combination of dots and dashes, this cheat sheet should make it clear.
Morse code can be transmitted in various ways, including radio waves, sound waves, visual light, etc.
We will use the Red LED (D1) from Snap Circuits to send Morse code as a light signal.
Snap Circuits diagramBuild the circuit shown in the diagram above.
CodeYou can build the code yourself in the MakeCode Editor. You will find the "digital write pin" block under the Advanced > Pins section.
Alternatively, open the ready project here: https://makecode.microbit.org/_0KJeC4XJY5RU
Once ready, download the code to your micro:bit. Then disconnect all cables from your micro:bit. Both the USB and the battery pack must be disconnected from the micro:bit.
How it works...The code programs the micro:bit to send the SOS signal using the connected LED. The LED has to emit 3 short light signals, then 3 long light signals, then 3 short light signals again.
We define two functions: “dot” and “dash”. The “dot” function writes a digital 1 signal to pin P1 to turn the LED on. Then the function pauses for “unit” milliseconds, where “unit” is a variable that defines the time unit for the Morse code. Then the “dot” function writes a digital 0 signal to pin P1 to turn the LED off. Then it pauses again for “unit” ms.
Executing the “dot” function results in a short light emitted from the LED and a short pause with the LED off. This is equivalent to emitting the dot of the Morse code.
The “dash” function is very similar to the “dot” function. The difference is that the LED is on for 3 times the “unit” duration. This is equivalent to emitting the dash of the Morse code.
When you close the slide switch (S1), the Battery Holder (B1) powers the snap:bit through the 3V snap and the micro:bit turns on. The “on start” event triggers and the micro:bit displays the skull image. It also sets the “unit” variable to 200 ms. This means that the LED will be on for 200 ms when emitting a dot, and for 600 ms when emitting a dash.
When the “on start” event finishes, the “forever” loop starts. It calls the “dot” function 3 times, then the “dash” function 3 times, then the “dot” function again 3 times. Last, the loop pauses for 7 times the time unit, which is equivalent to the space between words in the Morse code. Then the “forever” loop starts over and emits the SOS signal again and again, until the micro:bit is turned off.
Try experimenting with the program and emit other signals as a Morse code. Can you emit your name as Morse code?
Comments