New Ticker

News Ticker
Epstien didn't kill himself.
Epstien didn't kill himself.
Epstien didn't kill himself.
Epstien didn't kill himself.

Sunday, May 1, 2022

Morse Code Generator

This is a Morse Code generator project using a Raspberry Pi Pico. Two breadboards are used - one has a 1k resistor, 3v buzzer and a PNP transistor (it doesn't matter which one). Some work still needs to be done on the code timing. 



from machine import Pin, Timer

import utime

led = Pin(25, Pin.OUT)

buzzer = Pin(15, Pin.OUT)

utimer = Timer()

#led.value(1)

#buzzer.value(1)

#utime.sleep(0.5)

led.value(0)

buzzer.value(0)

MasterTime = 0.05

Dash = (MasterTime * 3)

Dot = MasterTime

Space = (MasterTime * 7)

CharSpace = (MasterTime * 3)

InterCharSpace = MasterTime

def SendLetter(Letter: str) -> None:

    led.value(0)

    MaxLength = len(Letter)

    for MorseCounter in range (0,MaxLength):

        if Letter[MorseCounter] == '-':

            led.value(1)

            buzzer.value(1)

            utime.sleep(Dash)

            buzzer.value(0)

            led.value(0)

        else:

            led.value(1)

            buzzer.value(1)

            utime.sleep(Dot)

            buzzer.value(0)

            led.value(0)

    utime.sleep(InterCharSpace)

#Morse Code

morse = {

    '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': '----.',

    '&': '.-...',

    "'": '.----.',

    '@': '.--.-.',

    ')': '-.--.-',

    '(': '-.--.',

    ':': '---...',

    ',': '--..--',

    '=': '-...-',

    '!': '-.-.--',

    '.': '.-.-.-',

    '-': '-....-',

    '*': '-..-',

    '%': '------..-.-----',

    '+': '.-.-.',

    '"': '.-..-.',

    '?': '..--..',

    '/': '-..-.',

}

char: str

MessageToSend: str = "In 1814, we took a little trip Along with Colonel Jackson down the mighty Mississip'"

MessageToSend = MessageToSend.lower()

for char in MessageToSend:

    if char != ' ':

        SendLetter(morse.get(char))

        utime.sleep(CharSpace)

        print(f'{char} {morse.get(char)}')

    else:

        print('space')

        utime.sleep(Space)