PlayingWithFusion.com Home

0 Items
View Cart

Document: 1208
Posted: November 21, 2022
Modified: November 28, 2022

Home > Tech Article Categories > R3actor and Arduino > Morse Code Example

Morse Code Example

Read text from the micro-SD card and display it on an LED in morse code

Parts List

for this project, you will need:

  • (x1) R3aktor M0 Logger Board
  • (x1) USB-C cable
  • (x1) Solderless breadboard
  • (x2) Jumper wires
  • (x1) LED
  • (x1) 220 to 4.7k Ohm Resistor

Hardware Setup

Place the LED in the breadboard. Connect the cathode in series with the resistor to pin D5 or the R3actor board. Connect the LED’s anode (the longer lead) to GND.

SD Card Setup

Connect the micro-SD card to a computer. Create an empty file in the root directory named morse.txt

If using Windows, open file explorer and navigate to the microSD card. In the top left, select New -> Text Document.

Open the file using your favorite text editor and insert the text you wish to translate into morse code.

If using Windows, open the text file by double clicking on it. Edit the text and then go to File -> Save.

Close the text file and safely eject the micro-SD card.

When this notification appears, it is safe to remove the micro-SD card from your computer.

Create Sketch (Software)

Connect the R3aktor board to your computer with the USB-C cable. The USB connection provides power to R3actor and provides a debug interface to allow programming.

Insert the micro-SD card with the morse.txt file into the R3aktor board.

Open the Arduino IDE. Copy and paste this code:

  1.  
  2. #include <SD.h>
  3.  
  4. // Digital pin led is connected to
  5. // The constant LED_BUILTIN may be used instead of 5 to use the built-in LED on R3actor
  6. int morseLED = 5;
  7.  
  8. // Base delay value in milliseconds
  9. int d = 100;
  10.  
  11. char character;
  12. String letter;
  13.  
  14. File sdFile;
  15.  
  16. void setup() {
  17.  
  18.    pinMode(morseLED, OUTPUT);
  19.  
  20.    Serial.begin(9600);
  21.    while (!Serial) {;}
  22.    Serial.print("Initializing SD card...");
  23.    if (!SD.begin(SDCARD_SS_PIN)) { // SDCARD_SS_PIN is the defined pin for the SD Card reader
  24.       Serial.println("Initialization failed.");
  25.       while (1);
  26.    }
  27.  
  28.    Serial.println("Initialization done.");
  29.    sdFile = SD.open("morse.txt", FILE_READ);
  30.  
  31.    if (sdFile) {
  32.  
  33.       Serial.println("File Opened");
  34.       while (sdFile.available()) {
  35.  
  36.       character = sdFile.read();  // Read the next character from the text file
  37.       letter = String(character); // convert that character into a string
  38.       letter.toLowerCase();       // convert the string to lower case
  39.       Serial.print(letter);       // print the letter to the monitor
  40.       translateCode(letter);      // pass the letter into the translation function
  41.  
  42.       }
  43.  
  44.       sdFile.close();
  45.       Serial.println("File Closed");
  46.  
  47.    } else {
  48.       Serial.println("error opeining TextToTranslate.txt");
  49.    }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. // Morse Code Translation Functions
  56. void translateCode(String input) {
  57.    if (input == "a") A();
  58.    if (input == "b") B();
  59.    if (input == "c") C();
  60.    if (input == "d") D();
  61.    if (input == "e") E();
  62.    if (input == "f") f();
  63.    if (input == "g") G();
  64.    if (input == "h") H();
  65.    if (input == "i") I();
  66.    if (input == "j") J();
  67.    if (input == "k") K();
  68.    if (input == "l") L();
  69.    if (input == "m") M();
  70.    if (input == "n") N();
  71.    if (input == "o") O();
  72.    if (input == "p") P();
  73.    if (input == "q") Q();
  74.    if (input == "r") R();
  75.    if (input == "s") S();
  76.    if (input == "t") T();
  77.    if (input == "u") U();
  78.    if (input == "v") V();
  79.    if (input == "w") W();
  80.    if (input == "x") X();
  81.    if (input == "y") Y();
  82.    if (input == "z") Z();
  83.    if (input == " ") wordDelay();
  84.  
  85.    if (input == "0") zero();
  86.    if (input == "1") one();
  87.    if (input == "2") two();
  88.    if (input == "3") three();
  89.    if (input == "4") four();
  90.    if (input == "5") five();
  91.    if (input == "6") six();
  92.    if (input == "7") seven();
  93.    if (input == "8") eight();
  94.    if (input == "9") nine();
  95.  
  96. }
  97.  
  98. // Morse Code Key: Letters
  99. void A(){ dot();  dash();                 letterDelay(); }
  100. void B(){ dash(); dot();  dot();  dot();  letterDelay(); }
  101. void C(){ dash(); dot();  dash(); dot();  letterDelay(); }
  102. void D(){ dash(); dot();  dot();          letterDelay(); }
  103. void E(){ dot();                          letterDelay(); }
  104. void f(){ dot();  dot();  dash(); dot();  letterDelay(); }
  105. void G(){ dash(); dash(); dot();          letterDelay(); }
  106. void H(){ dot();  dot();  dot();  dot();  letterDelay(); }
  107. void I(){ dot();  dot();                  letterDelay(); }
  108. void J(){ dot();  dash(); dash(); dash(); letterDelay(); }
  109. void K(){ dash(); dot();  dash();         letterDelay(); }
  110. void L(){ dot();  dash(); dot();  dot();  letterDelay(); }
  111. void M(){ dash(); dash();                 letterDelay(); }
  112. void N(){ dash(); dot();                  letterDelay(); }
  113. void O(){ dash(); dash(); dash();         letterDelay(); }
  114. void P(){ dot();  dash(); dash(); dot();  letterDelay(); }
  115. void Q(){ dash(); dash(); dot();  dash(); letterDelay(); }
  116. void R(){ dot();  dash(); dot();          letterDelay(); }
  117. void S(){ dot();  dot();  dot();          letterDelay(); }
  118. void T(){ dash();                         letterDelay(); }
  119. void U(){ dot();  dot();  dash();         letterDelay(); }
  120. void V(){ dot();  dot();  dot();  dash(); letterDelay(); }
  121. void W(){ dot();  dash(); dash();         letterDelay(); }
  122. void X(){ dash(); dot();  dot();  dash(); letterDelay(); }
  123. void Y(){ dash(); dot();  dash(); dash(); letterDelay(); }
  124. void Z(){ dash(); dash(); dot();  dot();  letterDelay(); }
  125.  
  126. // Morse Code Key: Numbers
  127. void zero()   { dash(); dash(); dash(); dash(); dash(); letterDelay(); }
  128. void one()    { dot();  dash(); dash(); dash(); dash(); letterDelay(); }
  129. void two()    { dot();  dot();  dash(); dash(); dash(); letterDelay(); }
  130. void three()  { dot();  dot();  dot();  dash(); dash(); letterDelay(); }
  131. void four()   { dot();  dot();  dot();  dot();  dash(); letterDelay(); }
  132. void five()   { dot();  dot();  dot();  dot();  dot();  letterDelay(); }
  133. void six()    { dash(); dot();  dot();  dot();  dot();  letterDelay(); }
  134. void seven()  { dash(); dash(); dot();  dot();  dot();  letterDelay(); }
  135. void eight()  { dash(); dash(); dash(); dot();  dot();  letterDelay(); }
  136. void nine()   { dash(); dash(); dash(); dash(); dot();  letterDelay(); }
  137.  
  138. // Morse Code Functions
  139. void dot() {
  140.    Serial.print("*");
  141.    digitalWrite(morseLED, HIGH);
  142.    delay(d*2);
  143.    digitalWrite(morseLED, LOW);
  144.    delay(d*4);
  145. }
  146.  
  147. void dash() {
  148.    Serial.print("-");
  149.    digitalWrite(morseLED, HIGH);
  150.    delay(d*6);
  151.    digitalWrite(morseLED, LOW);
  152.    delay(d*4);
  153. }
  154.  
  155. // Delay in between letters
  156. void letterDelay() {
  157.    Serial.print("  ");
  158.    delay(d*10);
  159. }
  160.  
  161. // Delay in between words
  162. void wordDelay() {
  163.    Serial.println("");
  164.    delay(d*20);
  165. }
  166.  
  167. // The loop is not needed for this
  168. void loop() {}
  169.  

Program R3actor

Select the R3aktor board in the Arduino IDE and then select the upload button (the right arrow).

Open the serial monitor by going to Tools -> Serial Monitor to verify everything is working correctly.

You will see the output in the Serial Monitor matching the output on the LED. A * represents a dot and a represents a dash. The appearance of each character will correspond with the length of each flash of the LED.

Related Files

Related Products