Nibbles and Bits
The Care and Feeding of My Pet Arduino


by Budd Churchward - WB7FHC - NIBBLES AND BITS LIBRARY


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);
  pinMode(speaker,OUTPUT);
  // initialize the serial communication:
  Serial.begin(9600);
}



 void loop() {
   val=digitalRead(myKey);
   if (val) keyIsDown();
   if (!val) keyIsUp();
 }
 
 void keyIsDown() {
   tone(speaker,myTone);
   WaitWait=FullWait;
   downTime++;   //Count how long the key is down
   characterDone=false;
   ditOrDah=false;
   delay(myBounce);
 }

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

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