// Morse Code Translator with LED and Buzzer
// For Wokwi Arduino Uno Simulator
const int ledPin = 13; // LED connected to digital pin 13
const int buzzerPin = 12; // Buzzer connected to digital pin 12
// Morse code timing constants (in milliseconds)
const int dotDuration = 200; // Duration of a dot
const int dashDuration = dotDuration * 3; // Duration of a dash
const int elementGap = dotDuration; // Gap between dots/dashes
const int letterGap = dotDuration * 3; // Gap between letters
const int wordGap = dotDuration * 7; // Gap between words
// Morse code lookup table
const char* morseCodes[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", // S-Z
"-----", ".----", "..---", "...--", "....-", ".....", "-....", // 0-6
"--...", "---..", "----." // 7-9
};
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
Serial.println("Morse Code Translator");
Serial.println("Enter text to translate to Morse code:");
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readString();
input.toUpperCase();
Serial.print("Translating: ");
Serial.println(input);
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == ' ') {
// Word space
Serial.print(" / ");
delay(wordGap);
} else if (isAlphaNumeric(c)) {
// Convert character to Morse code
if (isAlpha(c)) {
// Letters A-Z (ASCII 65-90)
int index = c - 'A';
Serial.print(morseCodes[index]);
Serial.print(' ');
playMorseCode(morseCodes[index]);
} else if (isDigit(c)) {
// Numbers 0-9 (ASCII 48-57)
int index = c - '0' + 26; // Numbers start at index 26
Serial.print(morseCodes[index]);
Serial.print(' ');
playMorseCode(morseCodes[index]);
}
delay(letterGap);
}
}
Serial.println();
Serial.println("Translation complete. Enter new text:");
}
}
void playMorseCode(const char* morseCode) {
for (int i = 0; i < strlen(morseCode); i++) {
char element = morseCode[i];
// Turn on LED and buzzer
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000); // 1000Hz tone
if (element == '.') {
delay(dotDuration);
} else if (element == '-') {
delay(dashDuration);
}
// Turn off LED and buzzer
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
// Gap between elements (except after last element)
if (i < strlen(morseCode) - 1) {
delay(elementGap);
}
}
}