const int RELAY_PINS[] = {2, 3, 4, 5}; // Relay pins
const int dotDuration = 100; // Duration of a dot in milliseconds
const int dashDuration = 300; // Duration of a dash in milliseconds
const int interCharGap = 100; // Gap between characters in milliseconds
const int interWordGap = 300; // Gap between words in milliseconds
const int sentenceBreak = 60000; // One-minute break between sentences
const char* MORSE_CODE_DICT[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...",
"---..", "----.", "/"}; // Morse Codes
/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// Function to translate text to Morse code
String textToMorse(String text)
{
String morseCode = "";
for (int i = 0; i < text.length(); i++)
{
char c = toupper(text.charAt(i));
if (c >= 'A' && c <= 'Z')
{
morseCode += MORSE_CODE_DICT[c - 'A'];
}
else if (c >= '0' && c <= '9')
{
morseCode += MORSE_CODE_DICT[c - '0' + 26];
}
else if (c == ' ')
{
morseCode += MORSE_CODE_DICT[36];
}
morseCode += " "; // Character gap
}
return morseCode;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// Function to trigger all relays
void triggerRelays(int duration)
{
for (int i = 0; i < sizeof(RELAY_PINS) / sizeof(RELAY_PINS[0]); i++)
{
digitalWrite(RELAY_PINS[i], HIGH);
}
delay(duration);
for (int i = 0; i < sizeof(RELAY_PINS) / sizeof(RELAY_PINS[0]); i++)
{
digitalWrite(RELAY_PINS[i], LOW);
}
}
void setup()
{
Serial.begin(9600);
for (int i = 0; i < sizeof(RELAY_PINS) / sizeof(RELAY_PINS[0]); i++)
{
pinMode(RELAY_PINS[i], OUTPUT);
}
}
void loop()
{
String texts[] = {"HELLO", "Matias", "ARDUINO", "MORSE"};
for (int i = 0; i < sizeof(texts) / sizeof(texts[0]); i++)
{
String morseCode = textToMorse(texts[i]);
Serial.print("Text: ");
Serial.print(texts[i]);
//Serial.print(", Morse Code: ");
Serial.println(morseCode);
for (int j = 0; j < morseCode.length(); j++)
{
if (morseCode[j] == '.')
{
triggerRelays(dotDuration);
}
else if (morseCode[j] == '-')
{
triggerRelays(dashDuration);
}
delay(interCharGap);
}
delay(interWordGap);
}
delay(sentenceBreak); // One-minute break
}