Connect a buzzer using D7 as vcc and D4 as gnd. Enter what you want to convert to morse using serial

Dependencies:   mbed

Committer:
sinx
Date:
Thu Oct 22 09:31:36 2015 +0000
Revision:
0:3669c33270c9
Child:
1:f5b33cb527ce
working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sinx 0:3669c33270c9 1 #include "mbed.h"
sinx 0:3669c33270c9 2
sinx 0:3669c33270c9 3 Serial pc(USBTX, USBRX);
sinx 0:3669c33270c9 4 DigitalOut led(LED1);
sinx 0:3669c33270c9 5
sinx 0:3669c33270c9 6 //0=dot,1=dash, 2=space
sinx 0:3669c33270c9 7 //1. The length of a dot is one unit.2. A dash is three units.3. The space between parts of the same letter is one unit.4. The space between letters is three units.5. The space between words is seven units.
sinx 0:3669c33270c9 8 int morseArray[37][5]={
sinx 0:3669c33270c9 9 {0,1,3,3,3},/*A*/ {1,0,0,0,3},/*B*/ {1,0,1,0,3},/*C*/ {0,3,3,3,3},/*D*/
sinx 0:3669c33270c9 10 {0,0,0,0,3},/*E*/ {1,1,0,3,3},/*F*/ {1,1,0,3,3},/*G*/ {0,0,0,0,3},/*H*/
sinx 0:3669c33270c9 11 {0,0,3,3,3},/*I*/ {0,1,1,1,3},/*J*/ {1,0,1,3,3},/*K*/ {0,1,0,0,3},/*L*/
sinx 0:3669c33270c9 12 {1,1,3,3,3},/*M*/ {1,0,3,3,3},/*N*/ {1,1,1,3,3},/*O*/ {0,1,1,0,3},/*P*/
sinx 0:3669c33270c9 13 {1,1,0,1,3},/*Q*/ {0,1,0,3,3},/*R*/ {0,0,0,3,3},/*S*/ {1,3,3,3,3},/*T*/
sinx 0:3669c33270c9 14 {0,0,1,3,3},/*U*/ {0,0,0,1,3},/*V*/ {0,1,1,3,3},/*W*/ {1,0,0,1,3},/*X*/
sinx 0:3669c33270c9 15 {1,0,1,1,3},/*Y*/ {1,1,0,0,3},/*Z*/ {1,1,1,1,1},/*0*/ {0,1,1,1,1},/*1*/
sinx 0:3669c33270c9 16 {0,0,1,1,1},/*2*/ {0,0,0,1,1},/*3*/ {0,0,0,0,1},/*4*/ {0,0,0,0,0},/*5*/
sinx 0:3669c33270c9 17 {1,0,0,0,0},/*6*/ {1,1,0,0,0},/*7*/ {1,1,1,0,0},/*8*/ {1,1,1,1,0},/*9*/
sinx 0:3669c33270c9 18 {2,3,3,3,3}/*space */
sinx 0:3669c33270c9 19 };
sinx 0:3669c33270c9 20 float unitInSeconds=(float)0.1;
sinx 0:3669c33270c9 21 float dot=unitInSeconds;
sinx 0:3669c33270c9 22 float dash=unitInSeconds*(float)3;
sinx 0:3669c33270c9 23 float internalLetterSepeator=dot;
sinx 0:3669c33270c9 24 float letterSeperator=dash;
sinx 0:3669c33270c9 25 float space= unitInSeconds*(float)7;
sinx 0:3669c33270c9 26 char sentence[100];
sinx 0:3669c33270c9 27
sinx 0:3669c33270c9 28 void morse(char letter);
sinx 0:3669c33270c9 29
sinx 0:3669c33270c9 30 void beep(int type);
sinx 0:3669c33270c9 31
sinx 0:3669c33270c9 32
sinx 0:3669c33270c9 33 int main()
sinx 0:3669c33270c9 34 {
sinx 0:3669c33270c9 35 led=1;
sinx 0:3669c33270c9 36 printf("Enter the sentence you want to morse\r\n");
sinx 0:3669c33270c9 37 int count = 0;
sinx 0:3669c33270c9 38 while (count < (99)) {
sinx 0:3669c33270c9 39 sentence[count] = pc.getc();
sinx 0:3669c33270c9 40 if ((sentence[count] == 0x0a) || (sentence[count] == 0x0d)) // end on a carriage return or a line feed.
sinx 0:3669c33270c9 41 break;
sinx 0:3669c33270c9 42 count ++;
sinx 0:3669c33270c9 43 }
sinx 0:3669c33270c9 44
sinx 0:3669c33270c9 45 sentence[count] = 0;
sinx 0:3669c33270c9 46 printf("you entered: %s\r\n",sentence);
sinx 0:3669c33270c9 47
sinx 0:3669c33270c9 48 while (true) {
sinx 0:3669c33270c9 49
sinx 0:3669c33270c9 50 for (int i =0; i < sizeof(sentence); i++) {
sinx 0:3669c33270c9 51 morse(sentence[i]);
sinx 0:3669c33270c9 52 }
sinx 0:3669c33270c9 53 wait(2*space);
sinx 0:3669c33270c9 54
sinx 0:3669c33270c9 55 }
sinx 0:3669c33270c9 56 }
sinx 0:3669c33270c9 57
sinx 0:3669c33270c9 58 void morse(char letter)
sinx 0:3669c33270c9 59 {
sinx 0:3669c33270c9 60 switch (letter)
sinx 0:3669c33270c9 61 {
sinx 0:3669c33270c9 62 case 'A':case 'a':{for (int i=0;i<5;i++)beep(morseArray[0][i]);wait(dash); break;}
sinx 0:3669c33270c9 63 case 'B':case 'b':{for (int i=0;i<5;i++)beep(morseArray[1][i]);wait(dash); break;}
sinx 0:3669c33270c9 64 case 'C':case 'c':{for (int i=0;i<5;i++)beep(morseArray[2][i]);wait(dash); break;}
sinx 0:3669c33270c9 65 case 'D':case 'd':{for (int i=0;i<5;i++)beep(morseArray[3][i]);wait(dash); break;}
sinx 0:3669c33270c9 66 case 'E':case 'e':{for (int i=0;i<5;i++)beep(morseArray[4][i]);wait(dash); break;}
sinx 0:3669c33270c9 67 case 'F':case 'f':{for (int i=0;i<5;i++)beep(morseArray[5][i]);wait(dash); break;}
sinx 0:3669c33270c9 68 case 'G':case 'g':{for (int i=0;i<5;i++)beep(morseArray[6][i]);wait(dash); break;}
sinx 0:3669c33270c9 69 case 'H':case 'h':{for (int i=0;i<5;i++)beep(morseArray[7][i]);wait(dash); break;}
sinx 0:3669c33270c9 70 case 'I':case 'i':{for (int i=0;i<5;i++)beep(morseArray[8][i]);wait(dash); break;}
sinx 0:3669c33270c9 71 case 'J':case 'j':{for (int i=0;i<5;i++)beep(morseArray[9][i]);wait(dash); break;}
sinx 0:3669c33270c9 72 case 'K':case 'k':{for (int i=0;i<5;i++)beep(morseArray[10][i]);wait(dash); break;}
sinx 0:3669c33270c9 73 case 'L':case 'l':{for (int i=0;i<5;i++)beep(morseArray[11][i]);wait(dash); break;}
sinx 0:3669c33270c9 74 case 'M':case 'm':{for (int i=0;i<5;i++)beep(morseArray[12][i]);wait(dash); break;}
sinx 0:3669c33270c9 75 case 'N':case 'n':{for (int i=0;i<5;i++)beep(morseArray[13][i]);wait(dash); break;}
sinx 0:3669c33270c9 76 case 'O':case 'o':{for (int i=0;i<5;i++)beep(morseArray[14][i]);wait(dash); break;}
sinx 0:3669c33270c9 77 case 'P':case 'p':{for (int i=0;i<5;i++)beep(morseArray[15][i]);wait(dash); break;}
sinx 0:3669c33270c9 78 case 'Q':case 'q':{for (int i=0;i<5;i++)beep(morseArray[16][i]);wait(dash); break;}
sinx 0:3669c33270c9 79 case 'R':case 'r':{for (int i=0;i<5;i++)beep(morseArray[17][i]);wait(dash); break;}
sinx 0:3669c33270c9 80 case 'S':case 's':{for (int i=0;i<5;i++)beep(morseArray[18][i]);wait(dash); break;}
sinx 0:3669c33270c9 81 case 'T':case 't':{for (int i=0;i<5;i++)beep(morseArray[19][i]);wait(dash); break;}
sinx 0:3669c33270c9 82 case 'U':case 'u':{for (int i=0;i<5;i++)beep(morseArray[20][i]);wait(dash); break;}
sinx 0:3669c33270c9 83 case 'V':case 'v':{for (int i=0;i<5;i++)beep(morseArray[21][i]);wait(dash); break;}
sinx 0:3669c33270c9 84 case 'W':case 'w':{for (int i=0;i<5;i++)beep(morseArray[22][i]);wait(dash); break;}
sinx 0:3669c33270c9 85 case 'X':case 'x':{for (int i=0;i<5;i++)beep(morseArray[23][i]);wait(dash); break;}
sinx 0:3669c33270c9 86 case 'Y':case 'y':{for (int i=0;i<5;i++)beep(morseArray[24][i]);wait(dash); break;}
sinx 0:3669c33270c9 87 case 'Z':case 'z':{for (int i=0;i<5;i++)beep(morseArray[25][i]);wait(dash); break;}
sinx 0:3669c33270c9 88 case '0':{for (int i=0;i<5;i++)beep(morseArray[26][i]);wait(dash); break;} case '1':{for (int i=0;i<5;i++)beep(morseArray[27][i]);wait(dash); break;}
sinx 0:3669c33270c9 89 case '2':{for (int i=0;i<5;i++)beep(morseArray[28][i]);wait(dash); break;} case '3':{for (int i=0;i<5;i++)beep(morseArray[29][i]);wait(dash); break;}
sinx 0:3669c33270c9 90 case '4':{for (int i=0;i<5;i++)beep(morseArray[30][i]);wait(dash); break;} case '5':{for (int i=0;i<5;i++)beep(morseArray[31][i]);wait(dash); break;}
sinx 0:3669c33270c9 91 case '6':{for (int i=0;i<5;i++)beep(morseArray[32][i]);wait(dash); break;} case '7':{for (int i=0;i<5;i++)beep(morseArray[33][i]);wait(dash); break;}
sinx 0:3669c33270c9 92 case '8':{for (int i=0;i<5;i++)beep(morseArray[34][i]);wait(dash); break;} case '9':{for (int i=0;i<5;i++)beep(morseArray[35][i]);wait(dash); break;}
sinx 0:3669c33270c9 93 case ' ':{for (int i=0;i<5;i++)beep(morseArray[36][i]);wait(dash); break;}
sinx 0:3669c33270c9 94
sinx 0:3669c33270c9 95 }
sinx 0:3669c33270c9 96 }
sinx 0:3669c33270c9 97
sinx 0:3669c33270c9 98 void beep(int type)
sinx 0:3669c33270c9 99 {
sinx 0:3669c33270c9 100 switch(type)
sinx 0:3669c33270c9 101 {
sinx 0:3669c33270c9 102 case 0:{
sinx 0:3669c33270c9 103 led=1;
sinx 0:3669c33270c9 104 wait(dot);
sinx 0:3669c33270c9 105 led=0;
sinx 0:3669c33270c9 106 wait(dot);
sinx 0:3669c33270c9 107 break;
sinx 0:3669c33270c9 108 }
sinx 0:3669c33270c9 109 case 1:{
sinx 0:3669c33270c9 110 led=1;
sinx 0:3669c33270c9 111 wait(dash);
sinx 0:3669c33270c9 112 led=0;
sinx 0:3669c33270c9 113 wait(dot);
sinx 0:3669c33270c9 114 break;
sinx 0:3669c33270c9 115 }
sinx 0:3669c33270c9 116 case 2:{
sinx 0:3669c33270c9 117 led=0;
sinx 0:3669c33270c9 118 wait(space);
sinx 0:3669c33270c9 119 break;
sinx 0:3669c33270c9 120 }
sinx 0:3669c33270c9 121 case 3:{
sinx 0:3669c33270c9 122 break;
sinx 0:3669c33270c9 123 }
sinx 0:3669c33270c9 124 }
sinx 0:3669c33270c9 125 }