Nibbles and Bits
The Care and Feeding of My Pet Arduino


Teaching Arduino to Copy Morse Code

« 1 2 3 4 5 6 7  8  9 10 11 12 13 15 16 17 18 19 »

Section 8
Now it is time to explore our strategy for decyphering Morse Code.

We are going to turn the incoming dits and dahs into a stream of bits, binary 1's and 0's.

The word bit is short for binary digit. Arbitrarily, I have choosen to designate the 1 bit as a dit and the 0 bit as a dah.

There is no good reason why it couldn't be the other way around. That's just what I chose to do.

The changes aren't very big this time. in printResutls() simply change our . and - to 1 and 0.

Compile and run the program. As you tap out Morse Code, neat little binary numbers show up on the monitor. Soon we will be changing those numbers into the actual letters being sent with your key.

Next Section »

int myKey=14;    // We are borrowing Analog Pin 0 and using it as digital
int speaker=11;  // Speaker will be hooked between pin 11 and ground

int val=0;       // A value for key up and down
int myTone=440;  // Freq. of our tone

boolean ditOrDah=true;
int dit=100;

boolean characterDone=true;
int myBounce=2;
int downTime=0;

long FullWait=10000;
long WaitWait=FullWait;

void setup() {
  pinMode(myKey, INPUT);
  // initialize the serial communication:

 void loop() {
   if (val) keyIsDown();
   if (!val) keyIsUp();
 void keyIsDown() {
   downTime++;   //Count how long the key is down

 void keyIsUp() {
   if (!ditOrDah) {
  if (!characterDone) {
      WaitWait--;  //We are counting down
      if (WaitWait==0) {
        Serial.print(' ');

void printResults() {
  if (downTime<dit) {
    // We got a dit
    Serial.print ('1');
  else {
    // We got a dah
    Serial.print ('0');