String Sentence = ""; //
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(D6, OUTPUT);
digitalWrite(26, HIGH);
Serial.begin(9600);
Serial.println("Welcome to the Morse Translator");
}
//Functions
void dot(){ //Function that makes a dot with the led
digitalWrite(D6, HIGH); //Turn on the LED
delay(500); // Delay of 0.5 seconds
digitalWrite(D6, LOW); //Turn off the LED
delay(500); // Delay of 0.5 seconds
}
void line(){ //Function that makes a line with the led
digitalWrite(D6, HIGH); //Turn on the LED
delay(1000); //Delay of 1 second
digitalWrite(D6, LOW); //Turn off the LED
delay(500); //Delay of 0.5 seconds
}
// the loop function runs over and over again forever
void loop()
{
//Read sentence
delay(5000); //Delay of 5 seconds
Serial.println("Write the sentence you want to translate into Morse code on the Message section"); //Prints the action I want the user to make
delay(500); //Delay of 5 seconds
while (Serial.available() == 0){} //Wait for user input
Sentence = Serial.readString(); //Saves the user input on the String "Sentence"
Serial.println("-------------------------"); //Generates a separation
//Translating the sentence
for (int i = 0; i < Sentence.length(); i++){ //Action that will be repeated for each character in the sentence
if (Sentence[i] == 'A' || Sentence[i] == 'a') { //If the character is A o a, this action will be made
Serial.println("A = . _"); //Prints the letter and the morse code
dot(); //Calls the function "dot" to create a the "." with the led
line(); //Calls the function "line" to create a the "-" with the led
}
else if (Sentence[i] == 'B' || Sentence[i] == 'b') { //If the character is B or b, this action will be made
Serial.println("B = _ . . . "); //Prints the letter and the morse code
line(); //Calls the function "line" to create a the "-" with the led
dot(); //Calls the function "dot" to create a the "." with the led
dot(); //Calls the function "dot" to create a the "." with the led
}
else if (Sentence[i] == 'C' || Sentence[i] == 'c') {
Serial.println("C = _ . _ . ");
line();
dot();
line();
dot();
}
else if (Sentence[i] == 'D' || Sentence[i] == 'd') {
Serial.println("D = _ . . ");
line();
dot();
}
else if (Sentence[i] == 'E' || Sentence[i] == 'e') {
Serial.println("E = . ");
dot();
}
else if (Sentence[i] == 'F' || Sentence[i] == 'f') {
Serial.println("F = _ . . ");
dot();
dot();
line();
dot();
}
else if (Sentence[i] == 'G' || Sentence[i] == 'g') {
Serial.println("G = _ _ . ");
line();
line();
dot();
}
else if (Sentence[i] == 'H' || Sentence[i] == 'h') {
Serial.println("H = . . . . ");
dot();
dot();
dot();
dot();
}
else if (Sentence[i] == 'I' || Sentence[i] == 'i') {
Serial.println("I = . . ");
dot();
dot();
}
else if (Sentence[i] == 'J' || Sentence[i] == 'j') {
Serial.println("J = . - - - ");
dot();
line();
line();
line();
}
else if (Sentence[i] == 'K' || Sentence[i] == 'k') {
Serial.println("K = - . - ");
line();
dot();
line();
}
else if (Sentence[i] == 'L' || Sentence[i] == 'l') {
Serial.println("L = . - . . ");
dot();
line();
dot();
dot();
}
else if (Sentence[i] == 'M' || Sentence[i] == 'm') {
Serial.println("M = - - ");
line();
line();
}
else if (Sentence[i] == 'N' || Sentence[i] == 'n') {
Serial.println("N = - . ");
line();
dot();
}
else if (Sentence[i] == 'O' || Sentence[i] == 'o') {
Serial.println("O = - - - ");
line();
line();
line();
}
else if (Sentence[i] == 'P' || Sentence[i] == 'p') {
Serial.println("P = . - - . ");
dot();
line();
line();
dot();
}
else if (Sentence[i] == 'Q' || Sentence[i] == 'q') {
Serial.println("Q = - - . - ");
line();
line();
dot();
line();
}
else if (Sentence[i] == 'R' || Sentence[i] == 'r') {
Serial.println("R = . - . ");
line();
dot();
line();
}
else if (Sentence[i] == 'S' || Sentence[i] == 's') {
Serial.println("S = . . . ");
dot();
dot();
dot();
}
else if (Sentence[i] == 'T' || Sentence[i] == 't') {
Serial.println("T = - ");
line();
}
else if (Sentence[i] == 'U' || Sentence[i] == 'u') {
Serial.println("U = . . - ");
dot();
dot();
line();
}
else if (Sentence[i] == 'V' || Sentence[i] == 'v') {
Serial.println("V = . . . - ");
dot();
dot();
dot();
line();
}
else if (Sentence[i] == 'W' || Sentence[i] == 'w') {
Serial.println("W = . - - ");
dot();
line();
line();
}
else if (Sentence[i] == 'X' || Sentence[i] == 'x') {
Serial.println("X = - . . - ");
line();
dot();
dot();
line();
}
else if (Sentence[i] == 'Y' || Sentence[i] == 'y') {
Serial.println("Y = - . - - ");
line();
dot();
line();
line();
}
else if (Sentence[i] == 'Z' || Sentence[i] == 'z') {
Serial.println("Z = - - . . ");
line();
line();
dot();
dot();
}
else if (Sentence[i] == ' ') {
Serial.println("Space");
delay(2000);
}
}
}