#!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Fri Aug 23 20:20:01 2024@author: zephod beeblebrox"""# Morse Code DictionaryMORSE_CODE_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': '-----', ', ': '--..--', '.': '.-.-.-', '?': '..--..','/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-', ' ': '/'}def message_to_morse(message):# Convert message to uppercasemessage = message.upper()for char in message:if char == ' ':print(" delay(WORD_SPACE - LETTER_SPACE);")else:morse_char = MORSE_CODE_DICT.get(char, '')for i, symbol in enumerate(morse_char):if symbol == "-":print (" sendDash();")if symbol == ".":print(" sendDot();")# print(symbol)if i < len(morse_char) - 1:print(" delay(SYMBOL_SPACE);")print(" delay( LETTER_SPACE - SYMBOL_SPACE);")if __name__ == "__main__":# Input message from usermessage = input("//Enter your message: ")print("#define DOT_DURATION 50 // Duration of a dot in milliseconds")print ("#define DASH_DURATION (3 * DOT_DURATION) // Duration of a dash")print ("#define SYMBOL_SPACE DOT_DURATION // Space between symbols in the same letter")print("#define LETTER_SPACE (3 * DOT_DURATION) // Space between letters")print("#define WORD_SPACE (7 * DOT_DURATION) // Space between words")print("#define MESSAGE_DELAY 30000 // 30-second delay between messages")print("#define MORSE_PIN 4 // Define the pin used for Morse code output")print("void sendDot() {")print(" digitalWrite(MORSE_PIN, HIGH); // Set the pin high")print(" delay(DOT_DURATION);")print(" digitalWrite(MORSE_PIN, LOW); // Set the pin low")print(" }")print("void sendDash() {")print(" digitalWrite(MORSE_PIN, HIGH); // Set the pin high")print(" delay(DASH_DURATION);")print(" digitalWrite(MORSE_PIN, LOW); // Set the pin low")print(" }")print("void setup() {")print(" pinMode(MORSE_PIN, OUTPUT); // Set the pin as an output")print("}")print("void loop() {")# Convert and print in Morse code formatmessage_to_morse(message)print(" delay(MESSAGE_DELAY);")print("}")
Entering a simple message such as "a b c" produces this output:
//Enter your message: a b c
#define DOT_DURATION 50 // Duration of a dot in milliseconds
#define DASH_DURATION (3 * DOT_DURATION) // Duration of a dash
#define SYMBOL_SPACE DOT_DURATION // Space between symbols in the same letter
#define LETTER_SPACE (3 * DOT_DURATION) // Space between letters
#define WORD_SPACE (7 * DOT_DURATION) // Space between words
#define MESSAGE_DELAY 30000 // 30-second delay between messages
#define MORSE_PIN 4 // Define the pin used for Morse code output
void sendDot() {
digitalWrite(MORSE_PIN, HIGH); // Set the pin high
delay(DOT_DURATION);
digitalWrite(MORSE_PIN, LOW); // Set the pin low
}
void sendDash() {
digitalWrite(MORSE_PIN, HIGH); // Set the pin high
delay(DASH_DURATION);
digitalWrite(MORSE_PIN, LOW); // Set the pin low
}
void setup() {
pinMode(MORSE_PIN, OUTPUT); // Set the pin as an output
}
void loop() {
sendDot();
delay(SYMBOL_SPACE);
sendDash();
delay( LETTER_SPACE - SYMBOL_SPACE);
delay(WORD_SPACE - LETTER_SPACE);
sendDash();
delay(SYMBOL_SPACE);
sendDot();
delay(SYMBOL_SPACE);
sendDot();
delay(SYMBOL_SPACE);
sendDot();
delay( LETTER_SPACE - SYMBOL_SPACE);
delay(WORD_SPACE - LETTER_SPACE);
sendDash();
delay(SYMBOL_SPACE);
sendDot();
delay(SYMBOL_SPACE);
sendDash();
delay(SYMBOL_SPACE);
sendDot();
delay( LETTER_SPACE - SYMBOL_SPACE);
delay(MESSAGE_DELAY);
}
And the Arduino compiler gives this message:
Sketch uses 414 bytes (40%) of program storage space. Maximum is 1024 bytes.
Global variables use 4 bytes (6%) of dynamic memory, leaving 60 bytes for local variables. Maximum is 64 bytes.