The City's Voice Returns
The thick silence that has blanketed Cogsworth City for months is about to be shattered. In the depths of the old communications tower, dust motes dance through shafts of amber light streaming through cracked windows. Your fingers trace the outline of an ancient telegraph key, its brass surface tarnished but still solid. This device once carried messages across continents, connecting isolated outposts with the rhythm of dots and dashes.
The emergency broadcast system went dark during the great shutdown, leaving the city's districts unable to coordinate their restoration efforts. Emergency crews work in isolation, unaware that help might be just a few blocks away. The old-timers speak in hushed tones about the days when trained operators could send complex messages faster than most people could type, their fingers dancing across metal keys with practiced precision.
Your HERO Board hums quietly on the workbench, ready to breathe life into this vintage communication method. Two LEDs wait in the parts bin, one for the sharp, quick pulses of dots, another for the longer, deliberate flashes of dashes. A simple button will become your telegraph key, transforming finger taps into the universal language that once connected the world. The weight of responsibility settles on your shoulders as you realize you're about to restore the city's first communication link since the blackout began.
What You'll Learn
When you finish building this Morse code device, you'll be able to:
- Measure how long someone holds down a button and use that timing to make decisions
- Control two different LEDs based on the length of a button press
- Use the millis() function to track time with precision
- Create functions that organize your code into reusable chunks
- Send debugging information to your computer through the Serial Monitor
- Understand the basic principles behind Morse code communication
Understanding Time-Based Input
Most buttons work like light switches: they're either on or off. But what if the length of time you hold the switch matters? Think about knocking on a door. A quick tap-tap-tap says "friendly visitor," while a long, heavy knock suggests urgency. Your microcontroller can detect these timing differences just like your brain does.
Morse code operates on this same principle. Samuel Morse designed it in the 1830s as a way to send messages over long distances using simple electrical pulses. A short pulse represents a dot, a long pulse represents a dash. String these together in specific patterns, and you can spell out any word or number. The letter "A" is dot-dash. The letter "S" is dot-dot-dot. The famous distress signal "SOS" translates to dot-dot-dot, dash-dash-dash, dot-dot-dot.
The brilliant part is that timing makes all the difference. Press and release quickly, and you've made a dot. Hold down longer before releasing, and you've created a dash. Your microcontroller needs to measure these time intervals and decide what kind of signal you intended. This requires precise timing measurement, which is where the millis() function becomes essential. Unlike delay(), which pauses your entire program, millis() tells you how many milliseconds have passed since your board started running, letting you track events without stopping everything else.
Wiring Your Morse Code Device

This circuit uses two separate LEDs to create distinct visual signals. Here's why each connection matters:
- Button to pin 2: Digital pin 2 can detect rapid changes in button state. Unlike analog pins, it gives clear HIGH or LOW readings.
- Dot LED to pin 8: This LED will flash quickly for short button presses. Pin 8 provides enough current to drive the LED brightly.
- Dash LED to pin 9: A separate LED makes it visually obvious when you're sending dashes versus dots. Different pins let us control them independently.
- Resistors for both LEDs: Without current-limiting resistors, LEDs would draw too much current and burn out instantly. The resistors act like water flow restrictors in a pipe.
- Ground connections: Both the button and LED circuits need to complete their electrical loops through ground. Think of ground as the return path for electricity.
The Complete Code
Here's the full program that transforms button timing into Morse code signals. Copy this into your controller, then we'll walk through how each section works: