Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from microbit import *
- MORSE_DECODE = {'.-': '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'}
- cur_letter = "" #current string of dits and dahs
- cur_char = "" #current character to display
- press_length = 0
- while True:
- display.show(cur_char, wait=False)
- if pin0.read_analog() < 100: #string is being tugged
- press_length += 100
- elif press_length != 0: #string has been released
- if press_length < 600:
- cur_letter += "."
- elif press_length < 1200:
- cur_letter += "-"
- else: #letter complete
- if cur_letter in MORSE_DECODE.keys():
- cur_char = MORSE_DECODE[cur_letter]
- else:
- cur_char = "?"
- else:
- press_length = 0
- sleep(100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement