#include <SD.h>
// Digital pin led is connected to
// The constant LED_BUILTIN may be used instead of 5 to use the built-in LED on R3actor
int morseLED = 5;
// Base delay value in milliseconds
int d = 100;
char character;
String letter;
File sdFile;
void setup() {
pinMode(morseLED, OUTPUT);
Serial.begin(9600);
while (!Serial) {;}
Serial.print("Initializing SD card...");
if (!SD.begin(SDCARD_SS_PIN)) { // SDCARD_SS_PIN is the defined pin for the SD Card reader
Serial.println("Initialization failed.");
while (1);
}
Serial.println("Initialization done.");
sdFile = SD.open("morse.txt", FILE_READ);
if (sdFile) {
Serial.println("File Opened");
while (sdFile.available()) {
character = sdFile.read(); // Read the next character from the text file
letter = String(character); // convert that character into a string
letter.toLowerCase(); // convert the string to lower case
Serial.print(letter); // print the letter to the monitor
translateCode(letter); // pass the letter into the translation function
}
sdFile.close();
Serial.println("File Closed");
} else {
Serial.println("error opeining TextToTranslate.txt");
}
}
// Morse Code Translation Functions
void translateCode(String input) {
if (input == "a") A();
if (input == "b") B();
if (input == "c") C();
if (input == "d") D();
if (input == "e") E();
if (input == "f") f();
if (input == "g") G();
if (input == "h") H();
if (input == "i") I();
if (input == "j") J();
if (input == "k") K();
if (input == "l") L();
if (input == "m") M();
if (input == "n") N();
if (input == "o") O();
if (input == "p") P();
if (input == "q") Q();
if (input == "r") R();
if (input == "s") S();
if (input == "t") T();
if (input == "u") U();
if (input == "v") V();
if (input == "w") W();
if (input == "x") X();
if (input == "y") Y();
if (input == "z") Z();
if (input == " ") wordDelay();
if (input == "0") zero();
if (input == "1") one();
if (input == "2") two();
if (input == "3") three();
if (input == "4") four();
if (input == "5") five();
if (input == "6") six();
if (input == "7") seven();
if (input == "8") eight();
if (input == "9") nine();
}
// Morse Code Key: Letters
void A(){ dot(); dash(); letterDelay(); }
void B(){ dash(); dot(); dot(); dot(); letterDelay(); }
void C(){ dash(); dot(); dash(); dot(); letterDelay(); }
void D(){ dash(); dot(); dot(); letterDelay(); }
void E(){ dot(); letterDelay(); }
void f(){ dot(); dot(); dash(); dot(); letterDelay(); }
void G(){ dash(); dash(); dot(); letterDelay(); }
void H(){ dot(); dot(); dot(); dot(); letterDelay(); }
void I(){ dot(); dot(); letterDelay(); }
void J(){ dot(); dash(); dash(); dash(); letterDelay(); }
void K(){ dash(); dot(); dash(); letterDelay(); }
void L(){ dot(); dash(); dot(); dot(); letterDelay(); }
void M(){ dash(); dash(); letterDelay(); }
void N(){ dash(); dot(); letterDelay(); }
void O(){ dash(); dash(); dash(); letterDelay(); }
void P(){ dot(); dash(); dash(); dot(); letterDelay(); }
void Q(){ dash(); dash(); dot(); dash(); letterDelay(); }
void R(){ dot(); dash(); dot(); letterDelay(); }
void S(){ dot(); dot(); dot(); letterDelay(); }
void T(){ dash(); letterDelay(); }
void U(){ dot(); dot(); dash(); letterDelay(); }
void V(){ dot(); dot(); dot(); dash(); letterDelay(); }
void W(){ dot(); dash(); dash(); letterDelay(); }
void X(){ dash(); dot(); dot(); dash(); letterDelay(); }
void Y(){ dash(); dot(); dash(); dash(); letterDelay(); }
void Z(){ dash(); dash(); dot(); dot(); letterDelay(); }
// Morse Code Key: Numbers
void zero() { dash(); dash(); dash(); dash(); dash(); letterDelay(); }
void one() { dot(); dash(); dash(); dash(); dash(); letterDelay(); }
void two() { dot(); dot(); dash(); dash(); dash(); letterDelay(); }
void three() { dot(); dot(); dot(); dash(); dash(); letterDelay(); }
void four() { dot(); dot(); dot(); dot(); dash(); letterDelay(); }
void five() { dot(); dot(); dot(); dot(); dot(); letterDelay(); }
void six() { dash(); dot(); dot(); dot(); dot(); letterDelay(); }
void seven() { dash(); dash(); dot(); dot(); dot(); letterDelay(); }
void eight() { dash(); dash(); dash(); dot(); dot(); letterDelay(); }
void nine() { dash(); dash(); dash(); dash(); dot(); letterDelay(); }
// Morse Code Functions
void dot() {
Serial.print("*");
digitalWrite(morseLED, HIGH);
delay(d*2);
digitalWrite(morseLED, LOW);
delay(d*4);
}
void dash() {
Serial.print("-");
digitalWrite(morseLED, HIGH);
delay(d*6);
digitalWrite(morseLED, LOW);
delay(d*4);
}
// Delay in between letters
void letterDelay() {
Serial.print(" ");
delay(d*10);
}
// Delay in between words
void wordDelay() {
Serial.println("");
delay(d*20);
}
// The loop is not needed for this
void loop() {}