Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Servo:
- """
- A simple class for controlling hobby servos.
- Args:
- pin (pin0 .. pin3): The pin where servo is connected.
- freq (int): The frequency of the signal, in hertz.
- min_us (int): The minimum signal length supported by the servo.
- max_us (int): The maximum signal length supported by the servo.
- angle (int): The angle between minimum and maximum positions.
- Usage:
- SG90 @ 3.3v servo connected to pin0
- = Servo(pin0).write_angle(90)
- """
- def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):
- self.min_us = min_us
- self.max_us = max_us
- self.us = 0
- self.freq = freq
- self.angle = angle
- self.analog_period = 0
- self.pin = pin
- analog_period = round((1/self.freq) * 1000) # hertz to miliseconds
- self.pin.set_analog_period(analog_period)
- def write_us(self, us):
- us = min(self.max_us, max(self.min_us, us))
- duty = round(us * 1024 * self.freq // 1000000)
- self.pin.write_analog(duty)
- self.pin.write_digital(0) # turn the pin off
- def write_angle(self, degrees=None):
- degrees = degrees % 360
- total_range = self.max_us - self.min_us
- us = self.min_us + total_range * degrees // self.angle
- self.write_us(us)
- from microbit import *
- MORSE_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': '--..',
- '0': '-----', '1': '.----', '2': '..---',
- '3': '...--', '4': '....-', '5': '.....',
- '6': '-....', '7': '--...', '8': '---..',
- '9': '----.' }
- message_to_send = "HELLO WORLD" #insert your own message here
- encoded_message = "".join([MORSE_CODE[letter] + ' ' for letter in message_to_send])
- press_angle = 150
- release_angle = 60
- Servo(pin0).write_angle(release_angle)
- for char in encoded_message: #repeat for each character in the encoded message
- display.show(char, wait=False)
- if char == '.':
- Servo(pin0).write_angle(press_angle) #tug on the string for 0.6s
- sleep(600)
- elif char == '-':
- Servo(pin0).write_angle(press_angle)
- sleep(1200)
- else:
- Servo(pin0).write_angle(press_angle)
- sleep(2000)
- Servo(pin0).write_angle(release_angle) #release the string for 1s
- sleep(1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement