MorseBorgFinal - Turn your Raspberry Pi into a Morse coding machine
So you have a PicoBorg and want to do something that is not motor based?
Maybe you have seen MorseBorg and want something other than LEDs talking morse?
Well here we have a PicoBorg morse solution, intended for driving a buzzer, solenoid, or any other signalling device (maybe some kind of Aldis lamp)?
Same principle as the previous versions, but now you run the script and then keep giving it input until you have finished the conversation, why not try talking with your mate across the road using bulbs!
Here's the code, you can download the MorseBorgFinal script file as text here
Save the text file on your pi as MorseBorgFinal.py
Make the script executable usingchmod +x MorseBorgFinal.py
and run usingsudo ./MorseBorgFinal.py
#!/usr/bin/env python # coding: Latin-1 # Import libary functions we need import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) import time # Set which GPIO pins the drive outputs are connected to DRIVE_1 = 4 DRIVE_2 = 18 DRIVE_3 = 8 DRIVE_4 = 7 # Set all of the drive pins as output pins GPIO.setup(DRIVE_1, GPIO.OUT) GPIO.setup(DRIVE_2, GPIO.OUT) GPIO.setup(DRIVE_3, GPIO.OUT) GPIO.setup(DRIVE_4, GPIO.OUT) # Function to set all drives off def MotorOff(): GPIO.output(DRIVE_1, GPIO.LOW) GPIO.output(DRIVE_2, GPIO.LOW) GPIO.output(DRIVE_3, GPIO.LOW) GPIO.output(DRIVE_4, GPIO.LOW) # Configuration dotPeriod = 0.2 # Length of time used by a dot morseDrive = DRIVE_1 # Which output to use on the PicoBorg # Morse key dMorse = {' ':' ', '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':'-----', '.':'.-.-.-', ',':'--..--', '?':'..--..', "'":'.----.', '!':'-.-.--', '/':'-..-.', '(':'-.--.', ')':'-.--.-', '&':'.-...', ':':'---...', ';':'-.-.-.', '=':'-...-', '+':'.-.-.', '-':'-....-', '_':'..--.-', '"':'.-..-.', '$':'...-..-', '@':'.--.-.'} try: # Start by turning all drives off MotorOff() raw_input('You can now turn on the power, press ENTER to continue') while True: # Read in the next message plain = raw_input("Next message (blank to quit)> ") if len(plain) == 0: break # Cycle through each letter in the message for letter in plain: # Find the coded morse to use key = letter.upper() if dMorse.has_key(key): coded = dMorse[key] print '%s %s' % (letter, coded) else: coded = ' ' print '%s ???' % (letter) # Run the morse sequence for symbol in coded: if symbol == '.': GPIO.output(morseDrive, GPIO.HIGH) time.sleep(dotPeriod) GPIO.output(morseDrive, GPIO.LOW) time.sleep(dotPeriod) elif symbol == '-': GPIO.output(morseDrive, GPIO.HIGH) time.sleep(dotPeriod * 3) GPIO.output(morseDrive, GPIO.LOW) time.sleep(dotPeriod) elif symbol == ' ': time.sleep(dotPeriod * 7) time.sleep(dotPeriod * 3) MotorOff() raw_input('Turn the power off now, press ENTER to continue') GPIO.cleanup() except KeyboardInterrupt: # CTRL+C exit, turn off the drives and release the GPIO pins print 'Terminated' MotorOff() raw_input('Turn the power off now, press ENTER to continue') GPIO.cleanup()