I am working on this tutorial,


and the author created a "Send" and "Receive" code for encoding and decoding Morse code. He created the "Send" code with the entire alphabet A-Z, but his receive code only has 9 letters.

I thought I could easily add in the missing characters, and even add in numbers, but I am stuck.

His code has 2 values to decode the morse - "a" and "f"

In his receive code he has a value "f" that is supposed to be calculated by the number of spaces, but I cannot see how he comes up with his result.

For example, this is his sending code for "a"

    case 'a': 

Then this is the receive code;

if(a==4 && f==10) {   Serial.print("a");

I understand the value of 4 for "a", but I cannot understand the value of 10 for "f"

"a" is calculated by assigning a value of 1 for a dot and 3 for a dash and adding the values (in this case one dot and one dash, 1+3 = 4)

He explains how to calculate "f" here in his tutorial, but I still don't understand.

  1. When the LASER falls on the LDR, the value goes beyond 1000, and then it will enter into a loop.

  2. Now quantize the values i.e. if the value goes beyond 1000, then assign it as '1', otherwise assign it as '0'.

  3. Inside a loop, calculate the number of '1's and '0's. Set the counters 'a' and 'f'. Count the number of '1's and store it in 'a' and similarly count the number of '0's and store it in 'f' respectively.

  4. Now check the specific values of 'a' and 'f' respectively. Each character will have different set of 'a' and 'f'. The matching value will be displayed.

That seems to indicate; Laser on = Over 1000 = 1 Laser off = Under 1000 = 0

So for the letter a, I only see 2 times the laser goes off, not 10.Minof formatting changes

  • the person that wrote the code did not bother to make the code easily readable by indenting code blocks correctly .... for instance the case 'a': block that you included in your question .... the case 'a': should not be indented and the next four lines should be indented ..... all the other case statement blocks should be formatted the same way ...................... the LightsOff(elemPause); should not be called inside the case blocks .... it should be called at the end of MorseDot() and MorseDash() functions ..... that would reduce the length of the program significantly
    – jsotola
    Jan 9, 2019 at 18:19

1 Answer 1


This is not a good tutorial. The receiver works by assuming 14 dotLen time periods for each character. There is a loop that runs 14 times, with a 200ms delay between each iteration. If the laser is detected, then a is incremented, otherwise f is incremented. This means that the sum of a and f is always 14, and the case where a==0 means nothing was transmitted. The incomplete receiver code shows only 10 options for received characters and leaves out a==5 (which would be R or U), but does define 1 through 10 otherwise.

The receiver code does not differentiate between dots and dashes, it only counts the number of dotLen transitions where light is detected. This is not Morse Code.

The receiver code cannot distinguish between S (dot-dot-dot) and T (dash) because they both have 3 dotLen value. Same for A (dot-dash) vs. N (dash-dot).

If you want to understand decoding morse code, this is not a good tutorial to follow. It does not decode morse code, and I cannot see how it would work even with it's companion transmitter program.

  • Thanks very much! So assuming I understand it is fake Morse code, and I'm more interested in sending and receiving text data via the laser, would I be able to create a full alpha-numeric set of characters by some method (maybe increasing the amount of dotLen time periods)? Jan 9, 2019 at 22:47
  • No. I would suggest to abandon this base code for any project. Find something else that already works and learn from that. Do not try to learn from a broken project. It won’t even work on its own. Jan 9, 2019 at 22:49
  • I agree, Morse code has variable length characters and no carrier detect which makes it challenging to decode. (Is it "on" or was that noise? Is it "off" or did I just lose signal?) I agree with jose, go with a machine-oriented code (such as Baudot or ASCII) and decode that instead.
    – Duston
    Jan 11, 2019 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.