Search code examples
arduinomorse-code

Morse Code Arduino by toggling the same LED light


Suppose I want to create the app which allow user enter a message on Android and display it in Morse code on the Arduino by toggling the LED. A message in Morse code consists of series of dashes (LONGS) and dots (shorts). These dashes and dots can then be displaying by turning the LED on for the correct number of time units.

#include <Usb.h>
#include <AndroidAccessory.h>
#define  LED_PIN  13

#define SHORT 0
#define LONG 1
#define LETTER 2
#define WORD 3
#define STOP 4

#define UNIT 250

AndroidAccessory acc("testing",
        "morse_code",
        "CoolAccessory",
        "1.0",
        "http://www.example.com/CoolAccessory",
                "0000000012345678");
void setup()
{
  // set communiation speed
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  acc.powerOn();
}

void loop()
{
  byte msg[125];
  if (acc.isConnected()) {
    int len = acc.read(msg, sizeof(msg), 1); // read data into msg variable
    if (len > 0) { // Only do something if a message has been received.
        displayMorseCode(msg, len);

    }
  } 
  else
    digitalWrite(LED_PIN , LOW); // turn off light
}

//For toggle the LED for certain length of time use the delay() 
//call delay(UNIT) to pause execution of UNIT milliseconds
//long unit *3 , short = unit
void displayMorseCode(byte* msg, int len) {

  // TODO :Interpret the message toggle LED on and off to display the 
           morse code
 if (msg[0] == 1) 
    digitalWrite(LED_PIN,HIGH); 
  else
    digitalWrite(LED_PIN,LOW); 

}  

The message consists of the following values, which have been defined as constants:

SHORT:a dot in morse
LONG:a dash morse
LETTER: the end of a letter in morse WORD: the end of a word in morse STOP: the end of the morse ex: message"SOS" encoded as (SHORT,SHORT,SHORT,LETTER,LONG,LONG,LONG,LETTER,SHORT,SHORT,SHORT,LETTER,WORD,STOP)

How to implementing displayMorseCode this function?


Solution

  • //For toggle the LED for certain length of time use the delay() 
    //call delay(UNIT) to pause execution of UNIT milliseconds
    //long unit *3 , short = unit
    void displayMorseCode(byte* msg, int len) {
    int delayT = 0;
    int unit = 300;
      // TODO :Interpret the message toggle LED on and off to display the 
               morse code
    for (int i = 0 ; i< len; i++)
    {
    if (msg[i] == 1) {
        digitalWrite(LED_PIN,HIGH); 
        delayT = 3*unit;
    }
    else {
        digitalWrite(LED_PIN,LOW); 
        delayT = unit;
    }
    
    delay(delayT);
    
    } 
    

    This is a very simple answer, that will change the duration depending on the byte received. Now you must create a dictionary for each byte, so that depending on the byte (i.e. S = short,short,short) you create the write output, in the same way I showed you, but you should change digitalWrite() with the new function, that will create each letter's morse code. So the if condition would be for each letter (i.e. if (msg[i] == 83) - the S in decimal ASCII-)