const int ledPin = 13;
const int unitTime = 200;
unsigned long previousMillis = 0;
int dotDuration = unitTime; // Duration of a dot
void setup() {
pinMode(ledPin, OUTPUT); // Initialize the LED pin as an output
digitalWrite(ledPin, HIGH); // Ensure LED is initially off
Serial.begin(9600);
Serial.println("ENTER HERE:");
}
// Morse code representation for A-Z and 0-9
const char* morseCode[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."
};
// Function to blink a dot
void dot() {
digitalWrite(ledPin, LOW); // Turn the LED on
delay(dotDuration); // Wait for dot duration
digitalWrite(ledPin, HIGH); // Turn the LED off
}
// Function to blink a dash
void dash() {
digitalWrite(ledPin, LOW); // Turn the LED on
delay(dotDuration * 3); // Wait for dash duration
digitalWrite(ledPin, HIGH); // Turn the LED off
}
// Function to pause between Morse code characters
void morsePause() {
digitalWrite(ledPin, HIGH); // Ensure LED is off during the pause
delay(1000); // Pause for 1000 milliseconds
}
// Function to convert a character to Morse code and blink it
void morseCharacter(char ch) {
if (ch == ' ') {
delay(unitTime * 3); // Wait for 3 unit times for space between words
return;
}
if (ch >= 'A' && ch <= 'Z') {
const char* code = morseCode[ch - 'A'];
while (*code) {
if (*code == '.') {
dot();
delay(unitTime); // Wait for space between dot and dash
}
else if (*code == '-') {
dash();
delay(unitTime); // Wait for space between dot and dash
}
code++;
}
}
else if (ch >= '0' && ch <= '9') {
const char* code = morseCode[ch - '0' + 26];
while (*code) {
if (*code == '.') {
dot();
delay(unitTime); // Wait for space between dot and dash
}
else if (*code == '-') {
dash();
delay(unitTime); // Wait for space between dot and dash
}
code++;
}
}
morsePause(); // Pause after each Morse code character
}
void loop() {
if (Serial.available() > 0) {
String inputString = Serial.readStringUntil('\n'); // Read the input string until newline character
inputString.toUpperCase(); // Convert the input string to uppercase
// Convert and blink each character of the input string in Morse code
for (int i = 0; i < inputString.length(); i++) {
char ch = inputString.charAt(i);
morseCharacter(ch);
}
}
}