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 11

OK, things are really coming together, now. Let's clean up this sketch. We have put in many lines just to help you see how this things work. I recommend that you copy the items marked in yellow and paste them in the correct places, first. Notice that one of them just changes the format that our number is printed from BIN (binary) to DEC (decimal).

We are also renaming our old printResults() proceedure. It is now called: shiftBits(). Make that change in two places that are highlighted in yellow.

We don't need to print out the leading zeros, so all of that goes.

Once you have made the three changes marked in yellow. Carefully delete all the code highlighted in orange.

Finally compile the sketch to check for errors. Then run it as you have before. This time when you send characters in Morse Code, you will see a unique decimal number for each letter you send.

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;

int myNum=0;

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
 
  if (myNum==0) {
      myNum=1;  // This is our start bit
    }
   characterDone=false;
   ditOrDah=false;
   delay(myBounce);
 }

 void keyIsUp() {
   noTone(speaker);
   if (!ditOrDah) {
       shiftBits();
       printResults();
   }
  if (!characterDone) {
      WaitWait--;  //We are counting down
      if (WaitWait==0) {

        // The serial print library does
        // not support printing leading
        // zeros when printing binary numbers.
        // What a shame! --- These next lines
        // will pad out the zeros that the library
        // didn't include.
        
        if (myNum<128) Serial.print('0');
        if (myNum<64) Serial.print('0');
        if (myNum<32) Serial.print('0');
        if (myNum<16) Serial.print('0');
        if (myNum<8) Serial.print('0');
        if (myNum<4) Serial.print('0');
        if (myNum<2) Serial.print('0');
        
 
        Serial.print(myNum, DEC);
        Serial.print(' ');
        characterDone=true;
        myNum=0;
      }

      downTime=0;
    }
}
void shiftBits() {
void printResults() {
  WaitWait=FullWait;
  ditOrDah=true;
  if (downTime<dit) {
    // We got a dit
    myNum = myNum << 1;  //shift bits left
    myNum++;
  }
  else {
    // We got a dah
    myNum = myNum << 1; // shift bits left
  }
}