from machine import Pin, I2C
import time
from pico_i2c_lcd import I2cLcd
# Pin configuration
button_dot = Pin(3, Pin.IN, Pin.PULL_UP) # Button 1 for dot (input pin)
button_dash = Pin(4, Pin.IN, Pin.PULL_UP) # Button 2 for dash (input pin)
led = Pin(14, Pin.OUT) # LED output pin
buzzer = Pin(15, Pin.OUT) # Buzzer output pin
# Morse Code Dictionary (Dot is "." and Dash is "-")
morse_dict = {
'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': '-----',
' ': '/'
}
# Morse to text mapping
def morse_to_text(morse_code):
morse_code = morse_code.split(' ')
text = ''
for code in morse_code:
for char, morse in morse_dict.items():
if code == morse:
text += char
break
return text
# Initialize I2C and LCD
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # SDA on GPIO 0, SCL on GPIO 1
I2C_ADDR = i2c.scan()[0] # Get the I2C address of the LCD
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) # 16x2 LCD at the found address
lcd.clear()
# Morse Code Timer Setup
dot_time = 0.3 # Time duration for a dot in seconds
dash_time = 3 * dot_time # Dash duration
pause_time = 7 * dot_time # Pause between words
max_wait_time = 15 # Maximum wait time for translation in seconds
# LED Visual Morse code
def led_on(type):
if type == 'dot':
led.on() # Turn on LED for dot
elif type == 'dash':
led.on() # LED on for dash
def led_off(type):
led.off() # Turn off LED
# Sound beep (Buzzer)
def beep_on(type):
buzzer.on() # Turn on buzzer for beep
def beep_off(type):
buzzer.off() # Turn off buzzer
# Main loop
def main():
morse_code_input = '' # Initialize Morse code input string
last_button_press_time = time.time() # Track the last button press time
button_pressed_time = 0 # Track the duration of the button press
while True:
if button_dot.value() == 0: # Button 1 (dot) pressed
button_pressed_time = time.time()
while button_dot.value() == 0: # Keep checking while the button is held
time.sleep(0.1) # Small delay to prevent excessive CPU usage
# Wait for button to be released, then register the dot
time.sleep(dot_time) # Ensure the dot duration is consistent
morse_code_input += '.' # Add dot to morse code input
led_on('dot') # Turn on LED for dot
beep_on('dot') # Beep for dot
time.sleep(dot_time) # Keep LED and beep on for dot duration
led_off('dot') # Turn off LED after dot duration
beep_off('dot') # Turn off buzzer after dot duration
last_button_press_time = time.time() # Reset the timer
elif button_dash.value() == 0: # Button 2 (dash) pressed
button_pressed_time = time.time()
while button_dash.value() == 0: # Keep checking while the button is held
time.sleep(0.1) # Small delay to prevent excessive CPU usage
# Wait for button to be released, then register the dash
time.sleep(dot_time) # Ensure a slight delay before dash
morse_code_input += '-' # Add dash to morse code input
led_on('dash') # Turn on LED for dash
beep_on('dash') # Beep for dash
time.sleep(dash_time) # Keep LED and beep on for dash duration
led_off('dash') # Turn off LED after dash duration
beep_off('dash') # Turn off buzzer after dash duration
last_button_press_time = time.time() # Reset the timer
# Check for timeout (15 seconds of inactivity)
if time.time() - last_button_press_time >= max_wait_time and morse_code_input:
lcd.clear()
lcd.putstr("Morse Code:")
lcd.putstr(morse_code_input)
translated_text = morse_to_text(morse_code_input)
lcd.putstr("\nText: " + translated_text)
morse_code_input = "" # Clear input after translation
# Wait for 5 seconds before translating if no buttons pressed
if time.time() - last_button_press_time >= 5 and morse_code_input:
lcd.clear()
lcd.putstr("Morse Code:")
lcd.putstr(morse_code_input)
translated_text = morse_to_text(morse_code_input)
lcd.putstr("\nText: " + translated_text)
morse_code_input = "" # Clear input after translation
# Start the program
if __name__ == "__main__":
main()