const int BUZZER_PIN = 4; // Connect buzzer to GPIO 4
const int DOT_DURATION = 300; // Duration of a dot in milliseconds
const int DASH_DURATION = 900; // Dash is 3 times the dot duration
const int INTER_ELEMENT_GAP = 300; // Gap between dots and dashes
const int INTER_LETTER_GAP = 900; // Gap between letters
const int INTER_WORD_GAP = 2100; // Gap between words
// Morse code patterns for A-Z
const char* MORSE_CODE[] = {
".-", // 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
};
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Text to Morse Code Converter");
Serial.println("Enter text to convert to Morse Code (e.g., 'ABC'):");
// Example text to convert (replace this with any string you'd like)
String inputText = "ABC"; // Change this to any text you want to convert
convertTextToMorse(inputText); // Convert and play the Morse code
}
void playDot() {
tone(BUZZER_PIN, 1000); // Play tone at 1000 Hz
delay(DOT_DURATION); // Wait for dot duration
noTone(BUZZER_PIN); // Stop the tone
delay(INTER_ELEMENT_GAP); // Wait for the gap between elements
}
void playDash() {
tone(BUZZER_PIN, 1000); // Play tone at 1000 Hz
delay(DASH_DURATION); // Wait for dash duration
noTone(BUZZER_PIN); // Stop the tone
delay(INTER_ELEMENT_GAP); // Wait for the gap between elements
}
void playMorseChar(char c) {
if (c >= 'A' && c <= 'Z') {
const char* morse = MORSE_CODE[c - 'A']; // Get the corresponding Morse code
Serial.print("Morse for ");
Serial.print(c);
Serial.print(": ");
Serial.println(morse);
// Play the Morse code for the character
for (int i = 0; morse[i] != '\0'; i++) {
if (morse[i] == '.') {
playDot(); // Play dot
} else if (morse[i] == '-') {
playDash(); // Play dash
}
}
delay(INTER_LETTER_GAP); // Gap between letters
} else if (c == ' ') {
delay(INTER_WORD_GAP); // Gap between words
}
}
void convertTextToMorse(String text) {
text.toUpperCase(); // Convert text to uppercase
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
playMorseChar(c); // Convert and play Morse code for each character
}
}
void loop() {
// No need to do anything in the loop
}