Search code examples
pythonledmorse-codeesp32

Let LED blink for dot or dash in morse


Using a ESP32 with python, this is my script. What it does is you can fill in some text and it will convert that to morse code and display it. But I also want the led to blink a second when it's a dash and 0,25 of a second when it's a dot.

With the script shown down below I get one blink. I think it's because of the pattern.match. I tried find() and findall but then I get these outputs

import morse
Enter sentence: hi

.... ..
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "morse.py", line 82, in <module>
AttributeError: 'ure' object has no attribute 'findall'

and

import morse
Enter sentence: hi
.... ..
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "morse.py", line 82, in <module>
AttributeError: 'ure' object has no attribute 'find'

the script itself

import time
from machine import Pin
led=Pin(5,Pin.OUT)
BRate=0.25

import re

def morse_dash():
    led.value(1)
    time.sleep(4*BRate)
    led.value(0)
    time.sleep(BRate)

def morse_dot():
    led.value(1)
    time.sleep(BRate)
    led.value(0)
    time.sleep(BRate)

CODE = {' ': '_', 
"'": '.----.', 
'(': '-.--.-', 
')': '-.--.-', 
',': '--..--', 
'-': '-....-', 
'.': '.-.-.-', 
'/': '-..-.', 
'0': '-----', 
'1': '.----', 
'2': '..---', 
'3': '...--', 
'4': '....-', 
'5': '.....', 
'6': '-....', 
'7': '--...', 
'8': '---..', 
'9': '----.', 
':': '---...', 
';': '-.-.-.', 
'?': '..--..', 
'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': '--..', 
'_': '..--.-'}

def convertToMorseCode(sentence):
    sentence = sentence.upper()
    encodedSentence = ""
    for character in sentence:
        encodedSentence += CODE[character] + " " 
    return encodedSentence

while True:

    sentence = input("Enter sentence: ")
    encodedSentence = convertToMorseCode(sentence)
    print(encodedSentence)
    pattern = re.compile('.')
    if pattern.match(encodedSentence):
        [morse_dot() for _ in range(1)]
    else:
        [morse_dash() for _ in range(1)]

Solution

  • You don't need regular expressions

    import time
    from machine import Pin
    led=Pin(5,Pin.OUT)
    BRate=0.25
    
    
    def morse_dash():
        led.value(1)
        time.sleep(4*BRate)
        led.value(0)
        time.sleep(BRate)
    
    def morse_pause():
        time.sleep(BRate)
    
    def morse_dot():
        led.value(1)
        time.sleep(BRate)
        led.value(0)
        time.sleep(BRate)
    
    CODE = {' ': '_', 
    "'": '.----.', 
    '(': '-.--.-', 
    ')': '-.--.-', 
    ',': '--..--', 
    '-': '-....-', 
    '.': '.-.-.-', 
    '/': '-..-.', 
    '0': '-----', 
    '1': '.----', 
    '2': '..---', 
    '3': '...--', 
    '4': '....-', 
    '5': '.....', 
    '6': '-....', 
    '7': '--...', 
    '8': '---..', 
    '9': '----.', 
    ':': '---...', 
    ';': '-.-.-.', 
    '?': '..--..', 
    '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': '--..', 
    '_': '..--.-'}
    
    def convertToMorseCode(sentence):
        sentence = sentence.upper()
        encodedSentence = ""
        for character in sentence:
            encodedSentence += CODE[character] + " " 
        return encodedSentence
    
    while True:
    
        sentence = input("Enter sentence: ")
        encodedSentence = convertToMorseCode(sentence)
        print(encodedSentence)
        for i in encodedSentence:
            if i == ".":
                morse_dot()
            elif i == "-":
                morse_dash()
            else:
                morse_pause()