-
Notifications
You must be signed in to change notification settings - Fork 71
Project 1: Morse Code
The circuit used in the morse code example is about as simple as it gets.
Wire from GPIO pin #17 to a 1K ohm resistor, and then into an LED. From there connect the LED to ground.
The idea is pretty simple; with only a single LED what can you do? For starters you could send Morse Code.
The app lets you type in a phrase, and it will blink the LED appropriately.
Morse Code isn't binary, the timing of the light being on and off also carriers meaning. Additionally each letter of the alphabet has it's own unique sequences of dots and dashes, varying from a single dot or dash up to four. To keep track of all of these we use a hash table to easily allow us to look up the appropriate code for each letter:
character_timing = { "a" => [dot, dash], "b" => [dash, dot, dot, dot], "c" => [dash, dot, dash, dot],
"d" => [dash, dot, dot], "e" => [dot], "f" => [dot, dot, dash, dot],
"g" => [dash, dash, dot], "h" => [dot, dot, dot, dot], "i" => [dot, dot],
"j" => [dot, dash, dash, dash], "k" => [dash, dot, dash], "l" => [dot, dash, dot, dot],
"m" => [dash, dash], "n" => [dash, dot], "o" => [dash, dash, dash],
"p" => [dot, dash, dash, dot], "q" => [dash, dash, dot, dash], "r" => [dot, dash, dot],
"s" => [dot, dot, dot], "t" => [dash], "u" => [dot, dot, dash],
"v" => [dot, dot, dot, dash], "w" => [dot, dash, dash], "x" => [dash, dot, dot, dash],
"y" => [dash, dot, dash, dash], "z" => [dash, dash, dot, dot]}
The core of the application will then loop over each character typed, and turn the LED on/off and then sleep for a varying amount of time.
something.each_char do |letter|
if letter == " "
pin.off
sleep medium_gap
else
character_timing[letter].each do |timing|
pin.on
sleep timing
pin.off
sleep inter_element_gap
end
sleep short_gap - inter_element_gap
end
The complete sample code can be found in the examples/morse_code directory of the Pi Piper project.