Generating random and pre-recorded Morse code

main()

At the top of the main() function (lines 33-63), I initialize two variables (lines 35 and 36): iSymbol becomes a random number that generates a dot, dash, or space, and iCount is the number of dots, dashes, or spaces to create.

The DDR variables control the direction of the port in question. Each bit in the register controls a pin on the chip. In binary representation, then, a 0 (00) means the pin is an input, a 1 (01) means the pin is an output, and a 2 (10) makes the second pin an output while the first remains an input. A value of 3 (11) makes pins one and two outputs, and so on. With DDRB = 1; (line 38), the port is an output.

The PORT variable controls whether outputs are high or low, again with each bit representing a pin. If pins are configured as inputs, then PORT enables or disables the internal pull-up resistor. That's its function here in line 39, PORTB |= ( 1 << PB2). A pull-up resistor in the electronics world is the equivalent of a default value for a software function argument. It is literally overpowered when the input is driven externally to the opposite value. The 1 << PB2 left-shifts a 1 into the "slot" that corresponds to PORTB, pin 2. The |= (or equals) sets the value within the variable, without disturbing the other bits.

The for() loop (lines 41-55) has no arguments, which means it loops continuously. In line 43, rand() % 3 generates a random number between 0 and 2, which specifies the type of Morse element to generate – dot, dash, or space.

In line 45, I check whether iCount is greater than 0. If so, I use a switch statement (lines 47-52) to call the appropriate function. After any Morse element is generated, I pause for the length of one dot (line 53), then decrement iCount (line 54).

In the conditional statement in lines 57 to 60, I check the external input (the go button) for its current state (line 57). The PIN variable mirrors the state of the external pins. Starting at the innermost set of parentheses, PINB >> PB2 shifts the value of PB2 to the 1 position. Stepping out one set of parentheses, & 1 strips out all of the other values in the variable (the other bits/pins). Now the value will be either 1 or 0. The pull-up resistor discussed above makes the default value 1.

If the button is pressed, then the value is 0 and line 59 resets iCount to 50. In this application, as long as the button is held down, Morse code will continue to be generated (albeit as gibberish). When the button is released, the code will continue to be sent, but the counter will decrement. Once it reaches 0, no Morse elements will be generated until the button is pressed again.

Conclusion

Morse code still holds a fascination today, even if it has moved out of the realm of official communication (see the box "The Telegraph and the Railroad"). The combined fascination of hearing the clicks, watching the sounder move up and down, and seeing a message appear via the radio operator can be captivating. Even though the systems discussed in this article are designed specifically to produce gibberish, it conveys the idea of what is happening.

The Telegraph and the Railroad

When the steam engine first came into the picture, it quickly became the fastest machine on Earth. With no onboard communication, the telegraph provided the critical link to getting messages to trains. The key was to get the message to the next station ahead of the train.

The station agent would send the train orders up to the passing train via a train order hoop. Think of a hula hoop with a long handle. The engineer stuck his hand through, pulled the message off, and threw the hoop down on the ground. If the aim was off then the engineer would have to stop the train and come back to retrieve the orders.

This process led to engineers deliberately holding onto the hoop longer out of spite to make station agents walk farther to retrieve it. There is however at least one account of a station agent that countered by training his dog to retrieve the hoop for him.

Telegraph Wiring

Each set of stations along a railroad line was on a single telegraph circuit. The stations were a "party line." A shorting bar (Figure 5) kept the circuit closed unless the operator was actively sending. If no station was active, then all of the sounders were held in the down position. Sounders popping up alerted the operator that a message was imminent.

Figure 5: A telegraph key. The shorting bar is the tall black knob on the right.

For longer telegraph lines, relays were used. The primary telegraph line was responsible only for switching the relay. A local circuit and battery maintained by the station operated the sounder.

Stations that had more than one telegraph line would be equipped with multiple sounders and a jack box. When messages arrived, one of the smaller sounders would start clicking them out. Placing the plug in the jackbox would transfer the selected line to the main sounder, which could be considerably louder. Figure 6 shows examples of additional telegraph equipment.

Figure 6: Additional telegraph equipment. The black boxes on the lower right are telegraph relays, and the jack box is immediately to their left. Small sounders are located on the white shelf in the back.

I've looked at two different approaches to the same desired result, and each can be easily expanded to send real messages. Both approaches originated by using what was available at the time, but it demonstrates that, in the electronics world, you can often find multiple valid approaches to a problem. I hope you've enjoyed this brief look at each approach and will have some fun trying and comparing the two methods.

Acknowledgments

Scott would like to thank Stephen Greif and the Temple Railroad and Heritage Museum for the telegraph equipment used in these projects. www.rrhm.org

The Author

Scott Sumner is Producer/Educator for the Mayborn Planetarium and Space Theatre on the campus of Central Texas College in Killeen, Texas. He enjoys coding and playing with trains when he's not flying through the galaxy or creating the next great tale of the universe.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content