const int RED_LED_ONE = 10;
const int RED_LED_TWO = 11;
const int RED_LED_THREE = 12;
const int DOT_SPEED = 500;
const int DASH_SPEED = DOT_SPEED * 2;
const int LETTER_DELAY = DOT_SPEED;
const int WORD_DELAY = DOT_SPEED * 7;
void setup() {
Serial.begin(9600);
// setup LEDs
pinMode(RED_LED_ONE, OUTPUT);
pinMode(RED_LED_TWO, OUTPUT);
pinMode(RED_LED_THREE, OUTPUT);
// get user input
while(true) {
String userInput = getUserInput("Please enter your message: ");
userInput.toLowerCase();
int inputLength = userInput.length();
// tranlate input into morse code
Serial.println("Begin!!!");
for (int i = 0; i < inputLength - 1; i++) {
char currentChar = userInput[i];
if (currentChar == ' ') {
wordDelay();
} else {
Serial.println("Current char is: " + String(currentChar));
morsifyLetter(currentChar);
}
}
Serial.print("And done!");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void morsifyLetter(char currentChar) {
// translate the current character into morse code, loop through the morse string
// and display the correct symbol
Serial.println("Received letter: " + String(currentChar));
String morseString = returnMorseString(currentChar);
Serial.println("Morse String: " + morseString);
int morseStringLength = morseString.length();
// iterate through the morse code letter
for (int i = 0; i < morseStringLength; i++) {
char currentChar = morseString[i];
if (currentChar == '.') {
displayDot();
} else {
displayDash();
}
}
Serial.println("Done with letter.");
letterDelay();
}
String getUserInput(String message) {
Serial.println(message);
String userInput;
while (!Serial.available()) {}
userInput = Serial.readString();
Serial.println("You entered: " + userInput);
return userInput;
}
void displayDash() {
Serial.println("Displaying dash.");
digitalWrite(RED_LED_ONE, HIGH);
digitalWrite(RED_LED_TWO, HIGH);
digitalWrite(RED_LED_THREE, HIGH);
delay(DASH_SPEED);
digitalWrite(RED_LED_ONE, LOW);
digitalWrite(RED_LED_TWO, LOW);
digitalWrite(RED_LED_THREE, LOW);
delay(DASH_SPEED);
}
void displayDot() {
Serial.println("Displaying dot.");
digitalWrite(RED_LED_ONE, HIGH);
delay(DOT_SPEED);
digitalWrite(RED_LED_ONE, LOW);
delay((DOT_SPEED));
}
void wordDelay() {
Serial.println("Word delay.\n");
delay(WORD_DELAY);
}
void letterDelay() {
Serial.println("Letter delay.\n");
delay(LETTER_DELAY);
}
String returnMorseString(char currentChar) {
switch (currentChar) {
// letters a to z
case 'a':
return ".-";
case 'b':
return "-...";
case 'c':
return "-.-.";
case 'd':
return "-..";
case 'e':
return ".";
case 'f':
return "..-.";
case 'g':
return "--.";
case 'h':
return "....";
case 'i':
return "..";
case 'j':
return ".---";
case 'k':
return "-.-";
case 'l':
return ".-..";
case 'm':
return "--";
case 'n':
return "-.";
case 'o':
return "---";
case 'p':
return ".--.";
case 'q':
return "--.-";
case 'r':
return ".-.";
case 's':
return "...";
case 't':
return "-";
case 'u':
return "..-";
case 'v':
return "...-";
case 'w':
return ".--";
case 'x':
return "-..-";
case 'y':
return "-.--";
case 'z':
return "--..";
// numbers
case '0':
return "-----";
case '1':
return ".----";
case '2':
return "..---";
case '3':
return "...--";
case '4':
return "....-";
case '5':
return ".....";
case '6':
return "-....";
case '7':
return "--...";
case '8':
return "---..";
case '9':
return "----.";
default:
return "";
}
}
// for (int i = 0; i < alphabetSize; i++) {
// if (myAlphabet[i][0] == currentChar) {
// return myAlphabet[i][1];
// }
// }