Send morse code on the built-in LED of an Arduino.
Follow the commits to see the progression of code from a single, massive, loop()
function into the modular code it is now.
MorseCode.cpp
encapsulates the entirety of sending Morse code for a given ASCII character.
My design philosophy is many small methods that do one thing well. (This improves code testability, although in this case,
I did not write specs to test the methods.) When a comment is needed to describe a line of code,
replace the line by a suitably named method that can eliminate the comment.
Signal.cpp
encapsulates the logic of sending the the Morse code symbol, decoupling that effort
from the class that wants to send a symbol. Signal.cpp
has two methods on
and off
that sends visually over an LED.
File morse-code.ino
determines the LED to use; currently choosing the onboard LED.
The idea behind Signal.cpp
is that if you want to send an audible code, you modify this class, and only this class.
No need to dig into other working code and possibly break it.
My only disappointment is that MorseCode
is tightly coupled to Signal
due to the way C++ behaves.
My original thought process is that it would be nice to have a VisualSignal
and AudibleSignal
class and MorseCode
could use either.
I come from a dynamic language background: Javascript, Ruby. In those languages if two classes have the same methods,
they share the same implicit interface. In C++, to declare an interface you need to declare an abstract base class.
Since I wasn't about to implement an audible signalling class, I decided against this extra complexity. If you need to, modify Signal
to be audible instead of visual.
From macOS Mojave to Catalina, Apple made the decision to go full 64-bit; 32-bit applications are no longer supported. Installing the most recent macOS Arduino (1.8.12 at the time of writing) environment gets over the 64-bit problem.
DigiSpark has its own board manager to be able to program its chips. To install
-
Choose Arduino > Preferences
-
In Additional Board Manager URLs add
https://raw.githubusercontent.com/digistump/arduino-boards-index/master/package_digistump_index.json
and clickOK
. -
In Tools > Board > Boards Manager... install Digistump AVR Boards. My version, at time of writing, is 1.6.7 and exhibited a 32-bit code problem. Perhaps higher versions are fixed.
-
In Tools > Board choose Digispark (Default - 16.5MHz). The value of Tools > Programmer does not matter.
-
Now comes the fun part: trying to Verify (compile) code on macOS Catalina will end up with an error message in the Arduino window:
avr-g++: bad CPU type in executable
Tracking down the problem on the Arduino forums, apparently Digistump is a special snowflake and decided to ship their own 32-bit tool chain rather than use what is provided by the Arduino application. We need to remove their tool chain and trick them into using the 64-bit Arduino tool change.
-
Quit the Arduino IDE: Arduino > Quit Arduino.
-
Start a
Terminal
application and run these commands$ cd ~/Library/Arduino15/packages/arduino/tools/avr-gcc # remove the Digistump 32-bit tool chain $ rm -rfd 4.8.1-arduino5 # symlink in the Arduino tool chain * $ ln -s /Applications/Arduino.app/Contents/Java/hardware/tools/avr 4.8.1-arduino5
See: reference
-
Start the Arduino IDE and Verify (compile) should now work.