Home > Software engineering >  Trying to make a Raspberry Pi Pico project that blinks a light in morse code. How would I get the se
Trying to make a Raspberry Pi Pico project that blinks a light in morse code. How would I get the se

Time:05-18

from machine import Pin
from time import sleep
led = Pin(0, Pin.OUT)


def dot():
    led.value(1)
    sleep(0.5)
    led.value(0)
    sleep(2)


def dash():
    led.value(1)
    sleep(1)
    led.value(0)
    sleep(2)


# 0.5 Seconds = Dot
# 1 Second = Dash

# word to be translated
word = "Hi"

CodePudding user response:

You can treat a string in Python as a list, for example:

word = "Hi"
print(word[0])
print(word[1])
#H
#i

Knowing this, you can iterate over a string:

word = "Hi"
for letter in word:
   print(letter)

#H
#i

For your problem, you can also map all the letters with their respective codes:


# 0 -> dot
# 1 -> dash
codes = {
   "A": (0, 1),
   "B": (1, 0, 0, 0),
   "C": (1, 0, 1, 0)
}

word = "AC"

for letter in word:
    morse_codes = codes[letter]
    print(morse_codes)
}
#(0, 1)
#(1, 0, 1, 0)
  • Related