[Python] How do I use a button to input Morse code?

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    So I’m working on a project for a class where I need to take button presses, as Morse code and then translate them to English. I am using a dictionary for this. However I am a little stuck on where to go after the dictionary and variables.

    My variables are:

    • Dot time = .15
    • Dash time = .45
    • Character space = .15
    • Letter space = .45
    • Space = 1.05

    I have already imported gpiozero and time. And I know that I need to make some sort of timer to differentiate between dots and dashes, and a timer to differentiate between character space, letter space and a regular space. But I’m not quite sure how to set all of it up and then translate it to English.

    Edit:

    def user_message():
    message = ""
    current_word = ""
    start_time = 0
    while True:
    if button.is_pressed = False:
    start_time = time.time()
    else:
    if start_time != 0:
    end_time = time.time()
    pressed_time = end_time - start_time
    start_time = 0
    if pressed_time > dot_time:
    current_word += "-"
    else:
    current_word += "."
    time.sleep(character_space)


    This is the current code I have right now, sorry for my not being clear. The button is on a breadboard connected to a raspberry pi. And what I am trying to do is take inputs from the button in the form of Morse code and then have the Morse code translated to English. Thank you for being patient with me since I’m new to the platform.

    I have tried to do functions and referenced multiple GitHub sources that do similar things but they haven’t worked.

    Continue reading...

Compartilhe esta Página