Solution for the Morse Code Task

Committer:
noutram
Date:
Thu Sep 14 13:09:45 2017 +0000
Revision:
0:40f06a589018
Initial version by Chris Tipney

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:40f06a589018 1 // Elec143 Morsecode labwork
noutram 0:40f06a589018 2 //
noutram 0:40f06a589018 3 // Suggested solution using functions
noutram 0:40f06a589018 4 // Chris Tipney September 2017
noutram 0:40f06a589018 5 //
noutram 0:40f06a589018 6 // Morse code
noutram 0:40f06a589018 7 // dot = 150 ms
noutram 0:40f06a589018 8 // dash = 450 ms
noutram 0:40f06a589018 9 // symbol space = 150 ms
noutram 0:40f06a589018 10 // letter space = 450 ms
noutram 0:40f06a589018 11 // word space = 900 ms
noutram 0:40f06a589018 12 //
noutram 0:40f06a589018 13 // Note
noutram 0:40f06a589018 14 // Every symbol is followed by at least 150 ms
noutram 0:40f06a589018 15 // only the space beyond that varies according
noutram 0:40f06a589018 16 // to context
noutram 0:40f06a589018 17
noutram 0:40f06a589018 18 #include "mbed.h"
noutram 0:40f06a589018 19
noutram 0:40f06a589018 20 // function prototypes
noutram 0:40f06a589018 21 void sierra(int r); // output the pattern for the letter 'S' repeat according to value of r
noutram 0:40f06a589018 22 void oscar(int r); // output the pattern for the letter 'O' repeat according to value of r
noutram 0:40f06a589018 23 void dot(int r); // output a short flash followed by 150 ms space repeat according to value of r
noutram 0:40f06a589018 24 void dash(int r); // output a long flash followed by 150 ms space repeat according to value of r
noutram 0:40f06a589018 25 void letterspace(); // output 300 ms space to make up the rest of the letter space 450 ms
noutram 0:40f06a589018 26 void wordspace(); // output 750 ms space to make up the rest of the word space 900 ms
noutram 0:40f06a589018 27
noutram 0:40f06a589018 28 // led attached to port D7
noutram 0:40f06a589018 29 DigitalOut myled(D7);
noutram 0:40f06a589018 30
noutram 0:40f06a589018 31 int main()
noutram 0:40f06a589018 32 {
noutram 0:40f06a589018 33 while(1) // loop forever
noutram 0:40f06a589018 34 {
noutram 0:40f06a589018 35 sierra(1); // S
noutram 0:40f06a589018 36 oscar(1); // O
noutram 0:40f06a589018 37 sierra(1); // S
noutram 0:40f06a589018 38 wordspace();
noutram 0:40f06a589018 39 }
noutram 0:40f06a589018 40 }
noutram 0:40f06a589018 41
noutram 0:40f06a589018 42 // function implementations
noutram 0:40f06a589018 43
noutram 0:40f06a589018 44 void sierra(int rep)
noutram 0:40f06a589018 45 {
noutram 0:40f06a589018 46 for (int i = 0; i < rep; i++)
noutram 0:40f06a589018 47 {
noutram 0:40f06a589018 48 dot(3);
noutram 0:40f06a589018 49 letterspace();
noutram 0:40f06a589018 50 }
noutram 0:40f06a589018 51 }
noutram 0:40f06a589018 52
noutram 0:40f06a589018 53 void oscar(int rep)
noutram 0:40f06a589018 54 {
noutram 0:40f06a589018 55 for (int i = 0; i < rep; i++)
noutram 0:40f06a589018 56 {
noutram 0:40f06a589018 57 dash(3);
noutram 0:40f06a589018 58 letterspace();
noutram 0:40f06a589018 59 }
noutram 0:40f06a589018 60 }
noutram 0:40f06a589018 61
noutram 0:40f06a589018 62
noutram 0:40f06a589018 63 void dot(int rep)
noutram 0:40f06a589018 64 {
noutram 0:40f06a589018 65 for (int i = 0; i < rep; i++)
noutram 0:40f06a589018 66 {
noutram 0:40f06a589018 67 myled = 1;
noutram 0:40f06a589018 68 wait(0.150);
noutram 0:40f06a589018 69 myled = 0;
noutram 0:40f06a589018 70 wait(0.150);
noutram 0:40f06a589018 71 }
noutram 0:40f06a589018 72 }
noutram 0:40f06a589018 73
noutram 0:40f06a589018 74 void dash(int rep)
noutram 0:40f06a589018 75 {
noutram 0:40f06a589018 76 for (int i = 0; i < rep; i++)
noutram 0:40f06a589018 77 {
noutram 0:40f06a589018 78 myled = 1;
noutram 0:40f06a589018 79 wait(0.450);
noutram 0:40f06a589018 80 myled = 0;
noutram 0:40f06a589018 81 wait(0.150);
noutram 0:40f06a589018 82 }
noutram 0:40f06a589018 83 }
noutram 0:40f06a589018 84
noutram 0:40f06a589018 85 void wordspace()
noutram 0:40f06a589018 86 {
noutram 0:40f06a589018 87 wait(.750);
noutram 0:40f06a589018 88 }
noutram 0:40f06a589018 89
noutram 0:40f06a589018 90 void letterspace()
noutram 0:40f06a589018 91 {
noutram 0:40f06a589018 92 wait(.300);
noutram 0:40f06a589018 93 }