const int buzzerPin = 13;
// Define Morse code timings
const int dotDuration = 600 / 25; // Duration of a dot in milliseconds
const int dashDuration = 3 * dotDuration; // Duration of a dash (3 dots)
const int letterSpaceDuration = dotDuration; // Space between letters
const int wordSpaceDuration = 7 * dotDuration; // Space between words
// Morse code lookup table
const char* morseCode[] = {
".-", // 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 playMorseCharacter(const char morseChar) {
int index = morseChar - 'A';
if (index >= 0 && index < 26) {
const char* morseSequence = morseCode[index];
for (int i = 0; morseSequence[i] != '\0'; i++) {
if (morseSequence[i] == '.') {
tone(buzzerPin, 1000, dotDuration);
} else if (morseSequence[i] == '-') {
tone(buzzerPin, 1000, dashDuration);
}
delay(dotDuration + 150); // Add a small gap between dots and dashes
noTone(buzzerPin); // Stop the tone
delay(dotDuration); // Pause between dots and dashes
}
delay(letterSpaceDuration);
} else if (morseChar == ' ') {
delay(wordSpaceDuration);
}
}
void playSOS() {
// "SOS" in Morse code: "... --- ..."
playMorseCharacter('S');
playMorseCharacter('O');
playMorseCharacter('S');
}
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
playSOS(); // Play "SOS" in Morse code
delay(3000); // 3-second interval
}