Morse Code Communication Using Arduino
Translation of Morse code to text.
Devices & Components
Arduino Uno Rev3
LED (generic)
Buzzer
Pushbutton switch 12mm
Jumper wires (generic)
Breadboard (generic)
Resistor 330 ohm
Software & Tools
Arduino IDE
Project description
Code
Morse Code Source CODE
c_cpp
Now you ready to upload Sketch Simply Open Your Arduino IDE and go to toolbar File->Open->MorseCode.io and click upload button
1/* 2 This Program is for demonstration of MORSE CODE Communication 3 which was use to send information secretly using codes of combinations dots . and dashes - 4 Thanks to open source community 5 6 By Jalal Mansoori 7*/ 8 9 10#define SIZE 26 11const int ledPin=8; 12const int speakerPin=12; 13const int dotButton=2; 14const int dashButton=7; 15 16String morseCode=""; 17String text=""; 18int characterAscii=0; 19int startPos=0, endPos=0; 20int startPos1=0, endPos1=0; 21String characterCode=""; 22int dashButtonState=0; 23int dotButtonState=0; 24 25 26//Array of MorseCode for letters of English Language A to Z 27String letters[SIZE]={ 28 29// A to I 30".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 31// J to R 32".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", 33// S to Z 34"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." 35}; 36 37 38void setup() { 39 // put your setup code here, to run once: 40//Getting things Ready 41pinMode(ledPin, OUTPUT); 42pinMode(dotButton, INPUT); 43pinMode(dashButton, INPUT); 44Serial.begin(9600); 45 46Serial.println("*************************************************************"); 47Serial.println(" Demonstration of Morse Code "); 48Serial.println("************************************************************* "); 49Serial.println("\ 50Instructions"); 51Serial.println("1. First Write Your Morse code"); 52Serial.println("2. When you are done Write 1 on above input box and Press Enter or click Send Button "); 53Serial.println("3. For Space between letters write 2 and Press Enter "); 54Serial.println("4. For Space between words write 3 and Press Enter "); 55 56Serial.println("5. Thats all Translation of Morse Code will then be Shown "); 57 58Serial.println("\ 59\ 60Enter Your Morse Code Here "); 61 62} 63 64 65void loop() { 66 // put your main code here, to run repeatedly: 67 68while(Serial.available() > 0 ) 69{ 70 int ascii=Serial.read(); 71 72 switch(ascii) 73 { 74 case 49: // 49 is Ascii value of 1 75 76 Serial.print("\ 77"); 78 morseCode.concat('#');// Placeing # at the end of morseCode to simplify further processing 79 80 Serial.print("\ 81Your Morse code Translation : "); 82 83 endPos1=morseCode.indexOf('#'); 84 85 while(endPos1 < morseCode.length() ) 86 { 87 extractLetters(morseCode.substring(startPos1, endPos1)); // This function would extract Letter as name suggest and would convert code to text SIMPLE! 88 startPos1=endPos1+1; 89 if(startPos1 == morseCode.length() ) 90 { 91 break; 92 } 93 endPos1= morseCode.indexOf('#', startPos1); 94 } 95 startPos1=0; 96 endPos1=0; 97 98 text=""; // For New Translation 99 morseCode=""; 100 Serial.println("\ 101\ 102Enter Your Morse Code Here "); 103 104 105 break; 106 107 case 50: // 50 is Ascii value of 2 108 109 morseCode.concat("@"); 110 Serial.print("@"); 111 delay(200); 112 113 break; 114 115 case 51: // 51 is Ascii value of 3 116 117 morseCode.concat("#"); 118 Serial.print("#"); 119 delay(200); 120 121 break; 122 123 } 124 125} 126 127process(); 128 129} 130 131void turnONLedSpeaker() 132{ 133 //Turn ON LED 134 digitalWrite(ledPin, HIGH); 135 tone(speakerPin, 4699, 300); // tone(speakerPin, frequency, duration in milliSec) 136 137} 138 139void process() 140{ 141 142 dotButtonState=digitalRead(dotButton); 143dashButtonState=digitalRead(dashButton); 144 145 146 147 148 if(dashButtonState == HIGH) 149 { 150 turnONLedSpeaker(); 151 152 morseCode.concat("-"); // Storing code in variable morseCode with the help of concatenation function 153 Serial.print("-");//Prints User entered Code 154 delay(200); 155 } 156 else if(dotButtonState == HIGH) 157 { 158 turnONLedSpeaker(); 159 160 morseCode.concat("."); 161 Serial.print("."); 162 delay(200); 163 164 } 165 else 166 { 167 //Turn OFF LED 168 digitalWrite(ledPin, LOW); 169 170 } 171 172} 173 174char convertIntoText(String characterCode) 175{ 176 characterAscii=65; 177 178 for(int index=0; index<SIZE; index++) 179 { 180 if(characterCode == letters[index]) 181 { 182 return characterAscii; 183 } 184 characterAscii++; 185 } 186 187} 188 189void extractLetters(String words) 190{ 191 words.concat('@'); // Placeing @ at the end of word to simplify further processing 192 193 endPos=words.indexOf('@'); 194 195 196 //Loop to extracting single character morse Code from string of word 197 while( endPos<words.length() ) 198 { 199 characterCode=words.substring(startPos, endPos); 200 201 //Now CharacterCode will now convert in text 202 203 text.concat(convertIntoText(characterCode)); 204 205 startPos=endPos+1; 206 characterCode=""; 207 208 // if condition is just to terminate loop when our extracting single character code is complete thats all 209 if(startPos == words.length() ) 210 { 211 break; 212 } 213 214 endPos=words.indexOf('@', startPos); 215 216 } 217 218 219 Serial.print(text); 220 Serial.print(" "); 221 startPos=0; 222 endPos=0; 223 text=""; 224 225} 226 227
Morse Code Source CODE
c_cpp
Now you ready to upload Sketch Simply Open Your Arduino IDE and go to toolbar File->Open->MorseCode.io and click upload button
1/* 2 This Program is for demonstration of MORSE CODE Communication 3 which was use to send information secretly using codes of combinations dots . and dashes - 4 Thanks to open source community 5 6 By Jalal Mansoori 7*/ 8 9 10#define SIZE 26 11const int ledPin=8; 12const int speakerPin=12; 13const int dotButton=2; 14const int dashButton=7; 15 16String morseCode=""; 17String text=""; 18int characterAscii=0; 19int startPos=0, endPos=0; 20int startPos1=0, endPos1=0; 21String characterCode=""; 22int dashButtonState=0; 23int dotButtonState=0; 24 25 26//Array of MorseCode for letters of English Language A to Z 27String letters[SIZE]={ 28 29// A to I 30".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 31// J to R 32".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", 33// S to Z 34"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." 35}; 36 37 38void setup() { 39 // put your setup code here, to run once: 40//Getting things Ready 41pinMode(ledPin, OUTPUT); 42pinMode(dotButton, INPUT); 43pinMode(dashButton, INPUT); 44Serial.begin(9600); 45 46Serial.println("*************************************************************"); 47Serial.println(" Demonstration of Morse Code "); 48Serial.println("************************************************************* "); 49Serial.println("\ 50Instructions"); 51Serial.println("1. First Write Your Morse code"); 52Serial.println("2. When you are done Write 1 on above input box and Press Enter or click Send Button "); 53Serial.println("3. For Space between letters write 2 and Press Enter "); 54Serial.println("4. For Space between words write 3 and Press Enter "); 55 56Serial.println("5. Thats all Translation of Morse Code will then be Shown "); 57 58Serial.println("\ 59\ 60Enter Your Morse Code Here "); 61 62} 63 64 65void loop() { 66 // put your main code here, to run repeatedly: 67 68while(Serial.available() > 0 ) 69{ 70 int ascii=Serial.read(); 71 72 switch(ascii) 73 { 74 case 49: // 49 is Ascii value of 1 75 76 Serial.print("\ 77"); 78 morseCode.concat('#');// Placeing # at the end of morseCode to simplify further processing 79 80 Serial.print("\ 81Your Morse code Translation : "); 82 83 endPos1=morseCode.indexOf('#'); 84 85 while(endPos1 < morseCode.length() ) 86 { 87 extractLetters(morseCode.substring(startPos1, endPos1)); // This function would extract Letter as name suggest and would convert code to text SIMPLE! 88 startPos1=endPos1+1; 89 if(startPos1 == morseCode.length() ) 90 { 91 break; 92 } 93 endPos1= morseCode.indexOf('#', startPos1); 94 } 95 startPos1=0; 96 endPos1=0; 97 98 text=""; // For New Translation 99 morseCode=""; 100 Serial.println("\ 101\ 102Enter Your Morse Code Here "); 103 104 105 break; 106 107 case 50: // 50 is Ascii value of 2 108 109 morseCode.concat("@"); 110 Serial.print("@"); 111 delay(200); 112 113 break; 114 115 case 51: // 51 is Ascii value of 3 116 117 morseCode.concat("#"); 118 Serial.print("#"); 119 delay(200); 120 121 break; 122 123 } 124 125} 126 127process(); 128 129} 130 131void turnONLedSpeaker() 132{ 133 //Turn ON LED 134 digitalWrite(ledPin, HIGH); 135 tone(speakerPin, 4699, 300); // tone(speakerPin, frequency, duration in milliSec) 136 137} 138 139void process() 140{ 141 142 dotButtonState=digitalRead(dotButton); 143dashButtonState=digitalRead(dashButton); 144 145 146 147 148 if(dashButtonState == HIGH) 149 { 150 turnONLedSpeaker(); 151 152 morseCode.concat("-"); // Storing code in variable morseCode with the help of concatenation function 153 Serial.print("-");//Prints User entered Code 154 delay(200); 155 } 156 else if(dotButtonState == HIGH) 157 { 158 turnONLedSpeaker(); 159 160 morseCode.concat("."); 161 Serial.print("."); 162 delay(200); 163 164 } 165 else 166 { 167 //Turn OFF LED 168 digitalWrite(ledPin, LOW); 169 170 } 171 172} 173 174char convertIntoText(String characterCode) 175{ 176 characterAscii=65; 177 178 for(int index=0; index<SIZE; index++) 179 { 180 if(characterCode == letters[index]) 181 { 182 return characterAscii; 183 } 184 characterAscii++; 185 } 186 187} 188 189void extractLetters(String words) 190{ 191 words.concat('@'); // Placeing @ at the end of word to simplify further processing 192 193 endPos=words.indexOf('@'); 194 195 196 //Loop to extracting single character morse Code from string of word 197 while( endPos<words.length() ) 198 { 199 characterCode=words.substring(startPos, endPos); 200 201 //Now CharacterCode will now convert in text 202 203 text.concat(convertIntoText(characterCode)); 204 205 startPos=endPos+1; 206 characterCode=""; 207 208 // if condition is just to terminate loop when our extracting single character code is complete thats all 209 if(startPos == words.length() ) 210 { 211 break; 212 } 213 214 endPos=words.indexOf('@', startPos); 215 216 } 217 218 219 Serial.print(text); 220 Serial.print(" "); 221 startPos=0; 222 endPos=0; 223 text=""; 224 225} 226 227
Downloadable files
Circuit Designing on Breadboard
Instructions for Digital Pins From Arduino to breadboard: -> pin D2 is connected to one leg of pushButton1 . -> pin D7 is connected to one leg of pushButton2. -> pin D8 is connected to +ive terminal leg LED through resistor -> And Finally pin D12 is connected to +ive terminal leg Buzzer through resistor And other Connection Look at Schematic Picture and YOU ARE DONE !
Circuit Designing on Breadboard
Comments
Only logged in users can leave comments