Guest User

Receiver

a guest
Feb 8th, 2018
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from microbit import *
  2.  
  3. MORSE_DECODE = {'.-': 'A', '-...': 'B', '-.-.': 'C',
  4.     '-..': 'D', '.': 'E', '..-.': 'F',
  5.     '--.': 'G', '....': 'H', '..': 'I',
  6.     '.---': 'J', '-.-': 'K', '.-..': 'L',
  7.     '--': 'M', '-.': 'N', '---': 'O',
  8.     '.--.': 'P', '--.-': 'Q', '.-.': 'R',
  9.     '...': 'S', '-': 'T', '..-': 'U',
  10.     '...-': 'V', '.--': 'W', '-..-': 'X',
  11.     '-.--': 'Y', '--..': 'Z',
  12.     '-----': '0', '.----': '1', '..---': '2',
  13.     '...--': '3', '....-': '4', '.....': '5',
  14.     '-....': '6', '--...': '7', '---..': '8',
  15.     '----.': '9'}
  16.  
  17. cur_letter = "" #current string of dits and dahs
  18. cur_char = "" #current character to display
  19. press_length = 0
  20.  
  21. while True:
  22.     display.show(cur_char, wait=False)
  23.     if pin0.read_analog() < 100: #string is being tugged
  24.         press_length += 100
  25.     elif press_length != 0: #string has been released
  26.         if press_length < 600:
  27.             cur_letter += "."
  28.         elif press_length < 1200:
  29.             cur_letter += "-"
  30.         else: #letter complete
  31.             if cur_letter in MORSE_DECODE.keys():
  32.                 cur_char = MORSE_DECODE[cur_letter]
  33.             else:
  34.                 cur_char = "?"
  35.     else:
  36.         press_length = 0
  37.  
  38.     sleep(100)
RAW Paste Data