// Define Morse code patterns for each letter const int dotDuration = 250; // Duration of a dot in milliseconds const int dashDuration = 3 * dotDuration; // Duration of a dash (3 times the duration of a dot) const int interSymbolGap = dotDuration; // Gap between symbols (same duration as a dot) const int interLetterGap = 3 * dotDuration; // Gap between letters (3 times the duration of a dot) const int interWordGap = 7 * dotDuration; // Gap between words (7 times the duration of a dot) const char* morseTable[] = { ".-", // 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 }; int ledPin = 15; // LED pin number char inputChar; // Character received from the computer const char* morsePattern; // Morse code pattern for the input character void setup() { Serial.begin(9600); // Initialize serial communication at 9600 bps pinMode(ledPin, OUTPUT); // Set LED pin as output } void loop() { if (Serial.available() > 0) { // Check if data is available on serial port inputChar = Serial.read(); // Read the input character inputChar = toupper(inputChar); // Convert input character to uppercase if (isalpha(inputChar)) { // Check if input is a letter morsePattern = morseTable[inputChar - 'A']; // Get the Morse code pattern for the input letter for (int i = 0; i < strlen(morsePattern); i++) { // Loop through the Morse code pattern if (morsePattern[i] == '.') { // If current symbol is a dot, turn on the LED for the duration of a dot digitalWrite(ledPin, HIGH); delay(dotDuration); digitalWrite(ledPin, LOW); delay(interSymbolGap); } else if (morsePattern[i] == '-') { // If current symbol is a dash, turn on the LED for the duration of a dash digitalWrite(ledPin, HIGH); delay(dashDuration); digitalWrite(ledPin, LOW); delay(interSymbolGap); } } delay(interLetterGap); // Add gap between letters } else if (inputChar == ' ') { // If input is a space, add gap between words delay(interWordGap); } } }