We are going to practice using functions by making our simple LED circuit blink in Morse code.
We are going to be using the same circuit as we did in the Blinking LED exercise. The diagrams are provided again here for reference.
Morse code is a system for sending messages as a sequence of ON and OFF signals with predefined spaces in between.
Attribution: Rhey T. Snodgrass & Victor F. Camp, 1922, Public domain, via Wikimedia Commons
Characters in Morse code are represented by a series of dots and dashes, which we will use to determine the length of time our LED turns on for.
1 dot = turning the LED on for 1 unit of time, then off again
1 dash = turning the LED on for 3 units of time, then off again
Between letters, we wait for 3 units.
Between words, we wait for 7 units.
It would be very confusing (and long!) to write out every single on and off. That's why we're going to use functions to represent dots and dashes. We've written the functions for you, so all you have to do is call them in the right order. We've also defined a variable called UNIT to help you with the timing.
Fill in the blanks in the code below. We want to blink the message SOS, so we need to make our LED:
blink 3 dots (S)
wait for 3 units
blink 3 dashes (O)
wait for 3 units
blink 3 dots (S)
wait for 7 units
import board
import digitalio
import time
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
UNIT = .2
def dot():
led.value = True
time.sleep(UNIT)
led.value = False
time.sleep(UNIT)
def dash():
led.value = True
time.sleep(3*UNIT)
led.value = False
time.sleep(UNIT)
while True:
__________
__________
__________
time.sleep(___*UNIT)
__________
__________
__________
time.sleep(___*UNIT)
__________
__________
__________
time.sleep(___*UNIT)
import board
import digitalio
import time
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
UNIT = .2
def dot():
led.value = True
time.sleep(UNIT)
led.value = False
time.sleep(UNIT)
def dash():
led.value = True
time.sleep(3*UNIT)
led.value = False
time.sleep(UNIT)
while True:
dot()
dot()
dot()
time.sleep(3*UNIT)
dash()
dash()
dash()
time.sleep(3*UNIT)
dot()
dot()
dot()
time.sleep(7*UNIT)
Now that you have functions for dots and dashes, you can blink any message you want! Have fun sending messages!