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:
Fri Oct 23 14:01:51 2015 +0000
Revision:
1:f5b33cb527ce
Parent:
0:3669c33270c9
with sound;

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