Morse Code - Lab 3

Morse Code Mbed System

This system uses a push button, a speaker and a LED screen to simulate a Morse Code machine. Using 1 second as my unit of measurement for a tick, the Morse Code machine works as follows:

  • The push button is used to signal a high or low tick
  • The Speaker will sound if the push button is pushed
  • The LED screen will output the characters one by one as they are entered in Morse Code

Push Button

The push button is connected by connecting ground to a DigitalIn pin (p8 in this code) and has an internal PullUp resistor.

Speaker

The speaker is connected with 5v as the power supply to the black wire and then using a 2N3904 General Purpose Amplifier to amplify the volume. It is connected as shown on This Page.

Through the code, a PwmOut signal is used to send an analog signal to the speaker.

LED Screen

The LED screen is connected as followed. /media/uploads/Jgreub/ledhookup.png

Here's the Sparkfun Datasheet.

How to Use

First, understand Morse Code by going to the Wiki Page. As said before, my unit of measure is a second and the LED1 flashing light is used to display this time. The LED will blink on for a second and then off for a second. This is there to help the user keep track of time while entering Morse code.

Simply push the button, and the speaker will output a feedback noise, and the code will sample the button in the middle of the second. If it is held down in the middle of the second, an 'Up' tick will be recorded. Continue entering ticks until your desired character is entered and then wait 3 'Down' ticks until the character is displayed on the LED screen.

This version only allows alphabetic characters as input.

After 10 seconds of 'Down' ticks in a row, the LED screen will be cleared.

Morse Code

/media/uploads/Jgreub/morsecodelist.png

Example Video

Main.cpp Code

Import program

00001 // Morse Code Machine      February 27, 2013
00002 
00003 #include "mbed.h"
00004 #include "MorseCode.h"
00005 
00006 //Need to Setup LCD Screen as TextLCD lcd(p15, p16, p17, p18, p19, p20) - Is included in MorseCode.cpp
00007 DigitalIn Button(p8); // Button for User to Input Morse Code
00008 DigitalOut LED(LED1); // LED to Indicate Time (every 1 seconds)
00009 PwmOut Speaker(p21); // Speaker to play noise when button pushed
00010 Timer Time; // Keeps track of time
00011 
00012 int main() {
00013     // Setup
00014     int up = 0;
00015     int read = 0;
00016     Button.mode(PullUp);
00017     Speaker.period(1.0/800.0);
00018     Time.start();
00019     
00020     //Start
00021     while(1) {
00022     
00023         //Timer For User Comfort
00024         if(Time.read_ms()%1000<15) { 
00025             up = !up;
00026             read = 0;       // Reset for Next Second
00027             if((Time.read_ms() - 1740000) > 0) {
00028                 Time.reset(); // Reset if more than 29 minutes.
00029             }
00030             wait_ms(15);
00031         }
00032         
00033         // LED Ticker
00034         LED = (up)? 1 : 0;
00035         
00036         // Audio
00037         if(!Button) {
00038             Speaker = 0.25; // Volume
00039             wait_ms(15);
00040         }
00041         else {
00042             Speaker = 0.0; //Mute
00043             wait_ms(15);
00044         }
00045         
00046         // Do Morse Code  
00047         if(Time.read_ms()%1000 > 500 && read == 0) { // Half a Second Has Passed
00048             read = 1;
00049             MorseCode(!Button); // Read Button Value
00050         }
00051         
00052     }// End While
00053 }

Morse Code Source And Header

Import program

00001 //Morse Code Source File
00002 
00003 #include "mbed.h"
00004 #include "TextLCD.h"
00005 
00006 TextLCD lcd(p15, p16, p17, p18, p19, p20); // LED screen to display text as it is typed
00007 
00008 int Value = 0;
00009 int First = 1;
00010 int ThreeZeros = 0;
00011 int SevenZeros = 0;
00012 int TenZeros = 0;
00013 
00014 void printChar(int val) {
00015     switch(val) {
00016     case 0: // Printing 0 does nothing
00017         break;
00018     case 184:
00019         lcd.putc('a');
00020         break;
00021     case 3752:
00022         lcd.putc('b');
00023         break;
00024     case 15080:
00025         lcd.putc('c');
00026         break;
00027     case 936:
00028         lcd.putc('d');
00029         break;
00030     case 8:
00031         lcd.putc('e');
00032         break;
00033     case 2792:
00034         lcd.putc('f');
00035         break;
00036     case 3816:
00037         lcd.putc('g');
00038         break;
00039     case 680:
00040         lcd.putc('h');
00041         break;
00042     case 40:
00043         lcd.putc('i');
00044         break;
00045     case 48056:
00046         lcd.putc('j');
00047         break;
00048     case 3768:
00049         lcd.putc('k');
00050         break;
00051     case 2984:
00052         lcd.putc('l');
00053         break;
00054     case 952:
00055         lcd.putc('m');
00056         break;
00057     case 232:
00058         lcd.putc('n');
00059         break;
00060     case 15288:
00061         lcd.putc('o');
00062         break;
00063     case 12008:
00064         lcd.putc('p');
00065         break;
00066     case 61112:
00067         lcd.putc('q');
00068         break;
00069     case 744:
00070         lcd.putc('r');
00071         break;
00072     case 168:
00073         lcd.putc('s');
00074         break;
00075     case 56:
00076         lcd.putc('t');
00077         break;
00078     case 696:
00079         lcd.putc('u');
00080         break;
00081     case 2744:
00082         lcd.putc('v');
00083         break;
00084     case 3000:
00085         lcd.putc('w');
00086         break;
00087     case 15032:
00088         lcd.putc('x');
00089         break;
00090     case 60344:
00091         lcd.putc('y');
00092         break;
00093     case 15272:
00094         lcd.putc('z');
00095         break;
00096     }
00097 }
00098 
00099 void MorseCode(int tick) {
00100     if(tick) {
00101         ThreeZeros = 0;
00102         SevenZeros = 0;
00103         TenZeros = 0;
00104         
00105         //Update Value with a 1
00106         Value = Value << 1;
00107         Value += 1;
00108     }
00109     else {
00110         ThreeZeros += 1;
00111         SevenZeros += 1;
00112         TenZeros += 1;
00113         
00114         //Update Value with a Zero
00115         Value = Value << 1;
00116     }
00117     
00118     if(First) {
00119         lcd.cls();
00120         First = 0;
00121     }
00122     
00123     // Print Char if three zeros in a row
00124     if(ThreeZeros == 3) {
00125         printChar(Value);
00126         ThreeZeros = 0;
00127         Value = 0;
00128     }
00129     if(SevenZeros == 7) { // Space
00130         lcd.putc(' ');
00131         SevenZeros = 0;
00132     }
00133     if(TenZeros == 10) { // Clear
00134         lcd.cls();
00135         SevenZeros = 0;
00136         TenZeros = 0;
00137     }
00138 }

Import program

00001 // Morse Code Header File     February 27, 2013
00002 void MorseCode(int tick);


Please log in to post comments.