You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32 Python摩尔斯码LED闪烁异常及正则方法报错排查

Fixing Your ESP32 Morse Code LED Script

Let's get your Morse code LED working properly—you've got two key issues here, and they're both straightforward to fix.

1. Ditch the Regex (It's Unnecessary & Causing Errors)

First off, the AttributeError you're seeing is because in MicroPython, the re module is actually a lightweight version called ure that doesn't support find() or findall() methods. But here's the good news: you don't need regex at all for this task. Your goal is to process each character in the encoded Morse string, which is way easier with a simple loop.

Your original code only checks the first character of the encoded Morse string and runs one blink action—no wonder it only flashes once! You need to iterate through every character in encodedSentence and trigger the corresponding dot/dash blink for each one.

Modified Working Code

Here's the updated version of your script with both issues fixed:

import time
from machine import Pin

led = Pin(5, Pin.OUT)
BRate = 0.25  # Base rate: dot = 0.25s, dash = 1s (4x base)

def morse_dash():
    led.value(1)
    time.sleep(4 * BRate)
    led.value(0)
    time.sleep(BRate)  # Gap after dash

def morse_dot():
    led.value(1)
    time.sleep(BRate)
    led.value(0)
    time.sleep(BRate)  # Gap after dot

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)
    
    # Iterate through each character in the Morse code string
    for char in encodedSentence:
        if char == '.':
            morse_dot()
        elif char == '-':
            morse_dash()
        elif char == '_':
            # Longer pause for word separation (matches Morse code standards)
            time.sleep(3 * BRate)
        elif char == ' ':
            # Short pause between characters (adjust if needed)
            time.sleep(BRate)

Key Changes Explained:

  • Removed all regex code: We now loop directly through each character in the encoded Morse string, which is simpler and avoids the ure limitations.
  • Full string processing: Every . and - now triggers its corresponding blink function, so your LED will flash the complete Morse pattern instead of just one blink.
  • Added proper pauses: We handle _ (which maps to your input spaces) with a longer pause, following standard Morse code word separation rules, and a short pause for character spacing.

内容的提问来源于stack exchange,提问作者Julian La Lau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:26:40