// Define the Baudot codes and their corresponding frequencies
const int buzzerPin = 9;

// Baudot frequencies for letters A-Z (in Hz)
const int baudotFrequencies[26] = {
    440, // A
    466, // B
    494, // C
    523, // D
    554, // E
    587, // F
    622, // G
    659, // H
    698, // I
    740, // J
    784, // K
    830, // L
    880, // M
    932, // N
    988, // O
    1046, // P
    1109, // Q
    1175, // R
    1245, // S
    1319, // T
    1397, // U
    1480, // V
    1568, // W
    1651, // X
    1746, // Y
    1845  // Z
};

void setup() {
    Serial.begin(9600);
    pinMode(buzzerPin, OUTPUT);
    Serial.println("Enter a message: ");
}

void loop() {
    // Check if data is available
    if (Serial.available() > 0) {
        String message = Serial.readStringUntil('\n'); // Read message until newline
        message.toUpperCase(); // Convert message to uppercase

        // Loop through each character in the message
        for (char letter : message) {
            if (letter >= 'A' && letter <= 'Z') {
                int index = letter - 'A'; // Get the index (0-25) for the letter
                playFrequency(baudotFrequencies[index], 200); // Play frequency for 200ms
            } else {
                // If the character is not A-Z, we can either skip or beep a different frequency
                // Uncomment the line below to beep a frequency for unsupported characters
                // playFrequency(500, 200); // Beep for unsupported characters
            }
        }
    }
}

// Function to play a frequency on the buzzer for a specified duration
void playFrequency(int frequency, int duration) {
    tone(buzzerPin, frequency); // Start playing the frequency
    delay(duration); // Wait for the duration
    noTone(buzzerPin); // Stop playing
}