Guest User

Transmitter

a guest
Feb 8th, 2018
197
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Servo:
  2.  
  3.     """
  4.     A simple class for controlling hobby servos.
  5.     Args:
  6.         pin (pin0 .. pin3): The pin where servo is connected.
  7.         freq (int): The frequency of the signal, in hertz.
  8.         min_us (int): The minimum signal length supported by the servo.
  9.         max_us (int): The maximum signal length supported by the servo.
  10.         angle (int): The angle between minimum and maximum positions.
  11.     Usage:
  12.         SG90 @ 3.3v servo connected to pin0
  13.         = Servo(pin0).write_angle(90)
  14.     """
  15.  
  16.     def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):
  17.         self.min_us = min_us
  18.         self.max_us = max_us
  19.         self.us = 0
  20.         self.freq = freq
  21.         self.angle = angle
  22.         self.analog_period = 0
  23.         self.pin = pin
  24.         analog_period = round((1/self.freq) * 1000)  # hertz to miliseconds
  25.         self.pin.set_analog_period(analog_period)
  26.  
  27.     def write_us(self, us):
  28.         us = min(self.max_us, max(self.min_us, us))
  29.         duty = round(us * 1024 * self.freq // 1000000)
  30.         self.pin.write_analog(duty)
  31.         self.pin.write_digital(0)  # turn the pin off
  32.  
  33.     def write_angle(self, degrees=None):
  34.         degrees = degrees % 360
  35.         total_range = self.max_us - self.min_us
  36.         us = self.min_us + total_range * degrees // self.angle
  37.         self.write_us(us)
  38.  
  39. from microbit import *
  40.  
  41. MORSE_CODE =  {'A': '.-',     'B': '-...',   'C': '-.-.',
  42.     'D': '-..',    'E': '.',      'F': '..-.',
  43.     'G': '--.',    'H': '....',   'I': '..',
  44.     'J': '.---',   'K': '-.-',    'L': '.-..',
  45.     'M': '--',     'N': '-.',     'O': '---',
  46.     'P': '.--.',   'Q': '--.-',   'R': '.-.',
  47.     'S': '...',    'T': '-',      'U': '..-',
  48.     'V': '...-',   'W': '.--',    'X': '-..-',
  49.     'Y': '-.--',   'Z': '--..',
  50.     '0': '-----',  '1': '.----',  '2': '..---',
  51.     '3': '...--',  '4': '....-',  '5': '.....',
  52.     '6': '-....',  '7': '--...',  '8': '---..',
  53.     '9': '----.' }
  54.  
  55. message_to_send = "HELLO WORLD" #insert your own message here
  56. encoded_message = "".join([MORSE_CODE[letter] + ' ' for letter in message_to_send])
  57.  
  58. press_angle = 150
  59. release_angle = 60
  60.  
  61. Servo(pin0).write_angle(release_angle)
  62.  
  63. for char in encoded_message: #repeat for each character in the encoded message
  64.     display.show(char, wait=False)
  65.     if char == '.':
  66.         Servo(pin0).write_angle(press_angle) #tug on the string for 0.6s
  67.         sleep(600)
  68.     elif char == '-':
  69.         Servo(pin0).write_angle(press_angle)
  70.         sleep(1200)
  71.     else:
  72.         Servo(pin0).write_angle(press_angle)
  73.         sleep(2000)
  74.     Servo(pin0).write_angle(release_angle) #release the string for 1s
  75.     sleep(1000)
RAW Paste Data