Thursday, 9 February 2023

Morse Code - SOS

from machine import Pin from time import sleep # the message to send in morse-code messages = ["sos sos sos sos sos sos"] #timing for encoding beatLen = 0.15 #length of dits and dahs ditLen = beatLen * 1 dahLen = beatLen * 3 #assign the signal lamp pin and amke sure its off signalLamp = Pin(15, Pin.OUT) signalLamp.off() #according to stories I've heard... hellNotFrozen = True #table to transform readable letters into morse code code = {'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':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'} while hellNotFrozen: #loop through the list of messages for message in messages: # more code only has uppercase message = message.upper().strip() #loop through each character in the message for xmitChr in message: print(xmitChr, end = '') # show off the character on if xmitChr == ' ': # space means end of word # end of word - wait 4 more beats (7 total) sleep(beatLen * 4) else:# encode a character #loop through the ditdah code for this character for ditDah in code[xmitChr]: #turn the signal lamp on and count beats signalLamp.on() if ditDah == ".": sleep(ditLen) else: sleep(dahLen) # turn off signal lamp and 1 beat pause between signalLamp.off() sleep(beatLen) # end of character - wait for 2 more beats (3 in total) sleep(beatLen * 2) #end of message - wait 4 more beats (7 in total) sleep(beatLen * 4) print() #https://www.youtube.com/watch?v=cCSYFUeO-CU Observe the video for 'short short short', 'long long long', 'short short short'

No comments: