Earworm
New 10th anniversary edition of my acclaimed novel about what happens when AI and the music business collide.
For issue 144 of The MagPi magazine, I created a program that flashes Morse code messages. In this version, you can type in a message to have it converted to dots, dashes and flashes.
Morse code is a way of sending messages using patterns of lights or sounds. Each letter and number is encoded using dots and dashes. A dot represents a short flash or sound, and a dash represents a long dash or sound (three times the length of a dot).
For issue 144 of The MagPi I wrote an introductory electronics tutorial. It shows you how to connect your first LED to a Raspberry Pi, and gives you the code to make it flash. To make the program a bit more useful, I turned it into a Morse code transmitter.
The version in the mag flashed a sequence of dots and dashes that was written in the program.
For the online version here, I've expanded it so that you can type in messages and they will be converted to Morse code. There wasn't room in the magazine for this longer listing.
On this webpage, you can download the code for the project.
For more information on how the code works, get issue 144 of The MagPi.
The photo shows strips of paper with Morse code messages stamped into them. I photographed it at the Porthcurno Museum of Global Communications.
This picture shows you how to connect your LED to Raspberry Pi using a breadboard. Use a 330 ohm resistor to stop the LED drawing too much current.
Connect your LED to your Raspberry Pi like this. You can conncet to other GPIO pins, but you'll need to modify the code if you do.
# Morse code transmitter by Sean McManus - www.sean.co.uk
from gpiozero import LED
from time import sleep
led1 = LED(14)
def blink(length):
led1.on()
sleep(length)
led1.off()
sleep(0.5)
def flash_message(message):
for symbol in message:
if symbol == ".":
blink(0.5)
if symbol == "-":
blink(1.5)
if symbol == " ":
sleep(1.5)
morse_code_dict = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
'Z': '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
' ': ' ' # adds an extra space where there's a space in the message
}
while True:
message = input("Enter your message: ")
morse = ""
for letter in message.upper():
if letter in morse_code_dict:
morse += morse_code_dict[letter] + " "
flash_message(morse)
© Sean McManus. All rights reserved.
Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!
New 10th anniversary edition of my acclaimed novel about what happens when AI and the music business collide.
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python.
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.
This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.
Code a space adventure game in this Python programming book published by No Starch Press.