Code Morse Translator
In this project, we enter a phrase and it's translated into a Morse code with sound and light.
Components and supplies
6
Jumper wires (generic)
1
Buzzer, Piezo
2
Resistor 220 ohm
2
LED (generic)
1
Arduino 101
1
Breadboard (generic)
Project description
Code
Morse Code Project
arduino
1/* 2 Morse Code Project 3 4 This code will loop through a string of characters and convert these to morse code. 5 It will blink two LED lights and play audio on a speaker. 6 */ 7 8 9//**************************************************// 10// Type the String to Convert to Morse Code Here // 11//**************************************************// 12char stringToMorseCode[] = "Arduino Morse Code Project"; 13 14// Create variable to define the output pins 15int led12 = 12; // blink an led on output 12 16int led6 = 6; // blink an led on output 6 17int audio8 = 8; // output audio on pin 8 18int note = 1200; // music note/pitch 19 20/* 21 Set the speed of your morse code 22 Adjust the 'dotlen' length to speed up or slow down your morse code 23 (all of the other lengths are based on the dotlen) 24 25 Here are the ratios code elements: 26 Dash length = Dot length x 3 27 Pause between elements = Dot length 28 (pause between dots and dashes within the character) 29 Pause between characters = Dot length x 3 30 Pause between words = Dot length x 7 31 32 http://www.nu-ware.com/NuCode%20Help/index.html?m... 33*/ 34int dotLen = 100; // length of the morse code 'dot' 35int dashLen = dotLen * 3; // length of the morse code 'dash' 36int elemPause = dotLen; // length of the pause between elements of a character 37int Spaces = dotLen * 3; // length of the spaces between characters 38int wordPause = dotLen * 7; // length of the pause between words 39 40// the setup routine runs once when you press reset: 41void setup() { 42 // initialize the digital pin as an output for LED lights. 43 pinMode(led12, OUTPUT); 44 pinMode(led6, OUTPUT); 45} 46 47// Create a loop of the letters/words you want to output in morse code (defined in string at top of code) 48void loop() 49{ 50 // Loop through the string and get each character one at a time until the end is reached 51 for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++) 52 { 53 // Get the character in the current position 54 char tmpChar = stringToMorseCode[i]; 55 // Set the case to lower case 56 tmpChar = toLowerCase(tmpChar); 57 // Call the subroutine to get the morse code equivalent for this character 58 GetChar(tmpChar); 59 } 60 61 // At the end of the string long pause before looping and starting again 62 LightsOff(8000); 63} 64 65// DOT 66void MorseDot() 67{ 68 digitalWrite(led12, HIGH); // turn the LED on 69 digitalWrite(led6, HIGH); 70 tone(audio8, note, dotLen); // start playing a tone 71 delay(dotLen); // hold in this position 72} 73 74// DASH 75void MorseDash() 76{ 77 digitalWrite(led12, HIGH); // turn the LED on 78 digitalWrite(led6, HIGH); 79 tone(audio8, note, dashLen); // start playing a tone 80 delay(dashLen); // hold in this position 81} 82 83// Turn Off 84void LightsOff(int delayTime) 85{ 86 digitalWrite(led12, LOW); // turn the LED off 87 digitalWrite(led6, LOW); 88 noTone(audio8); // stop playing a tone 89 delay(delayTime); // hold in this position 90} 91 92// *** Characters to Morse Code Conversion *** // 93void GetChar(char tmpChar) 94{ 95 // Take the passed character and use a switch case to find the morse code for that character 96 switch (tmpChar) { 97 case 'a': 98 MorseDot(); 99 LightsOff(elemPause); 100 MorseDash(); 101 LightsOff(elemPause); 102 break; 103 case 'b': 104 MorseDash(); 105 LightsOff(elemPause); 106 MorseDot(); 107 LightsOff(elemPause); 108 MorseDot(); 109 LightsOff(elemPause); 110 MorseDot(); 111 LightsOff(elemPause); 112 break; 113 case 'c': 114 MorseDash(); 115 LightsOff(elemPause); 116 MorseDot(); 117 LightsOff(elemPause); 118 MorseDash(); 119 LightsOff(elemPause); 120 MorseDot(); 121 LightsOff(elemPause); 122 break; 123 case 'd': 124 MorseDash(); 125 LightsOff(elemPause); 126 MorseDash(); 127 LightsOff(elemPause); 128 MorseDot(); 129 LightsOff(elemPause); 130 break; 131 case 'e': 132 MorseDot(); 133 LightsOff(elemPause); 134 break; 135 case 'f': 136 MorseDot(); 137 LightsOff(elemPause); 138 MorseDot(); 139 LightsOff(elemPause); 140 MorseDash(); 141 LightsOff(elemPause); 142 MorseDot(); 143 LightsOff(elemPause); 144 break; 145 case 'g': 146 MorseDash(); 147 LightsOff(elemPause); 148 MorseDash(); 149 LightsOff(elemPause); 150 MorseDot(); 151 LightsOff(elemPause); 152 break; 153 case 'h': 154 MorseDot(); 155 LightsOff(elemPause); 156 MorseDot(); 157 LightsOff(elemPause); 158 MorseDot(); 159 LightsOff(elemPause); 160 MorseDot(); 161 LightsOff(elemPause); 162 break; 163 case 'i': 164 MorseDot(); 165 LightsOff(elemPause); 166 MorseDot(); 167 LightsOff(elemPause); 168 break; 169 case 'j': 170 MorseDot(); 171 LightsOff(elemPause); 172 MorseDash(); 173 LightsOff(elemPause); 174 MorseDash(); 175 LightsOff(elemPause); 176 MorseDash(); 177 LightsOff(elemPause); 178 break; 179 case 'k': 180 MorseDash(); 181 LightsOff(elemPause); 182 MorseDot(); 183 LightsOff(elemPause); 184 MorseDash(); 185 LightsOff(elemPause); 186 break; 187 case 'l': 188 MorseDot(); 189 LightsOff(elemPause); 190 MorseDash(); 191 LightsOff(elemPause); 192 MorseDot(); 193 LightsOff(elemPause); 194 MorseDot(); 195 LightsOff(elemPause); 196 break; 197 case 'm': 198 MorseDash(); 199 LightsOff(elemPause); 200 MorseDash(); 201 LightsOff(elemPause); 202 break; 203 case 'n': 204 MorseDash(); 205 LightsOff(elemPause); 206 MorseDot(); 207 LightsOff(elemPause); 208 break; 209 case 'o': 210 MorseDash(); 211 LightsOff(elemPause); 212 MorseDash(); 213 LightsOff(elemPause); 214 MorseDash(); 215 LightsOff(elemPause); 216 break; 217 case 'p': 218 MorseDot(); 219 LightsOff(elemPause); 220 MorseDash(); 221 LightsOff(elemPause); 222 MorseDash(); 223 LightsOff(elemPause); 224 MorseDot(); 225 LightsOff(elemPause); 226 break; 227 case 'q': 228 MorseDash(); 229 LightsOff(elemPause); 230 MorseDash(); 231 LightsOff(elemPause); 232 MorseDot(); 233 LightsOff(elemPause); 234 MorseDash(); 235 LightsOff(elemPause); 236 break; 237 case 'r': 238 MorseDot(); 239 LightsOff(elemPause); 240 MorseDash(); 241 LightsOff(elemPause); 242 MorseDot(); 243 LightsOff(elemPause); 244 break; 245 case 's': 246 MorseDot(); 247 LightsOff(elemPause); 248 MorseDot(); 249 LightsOff(elemPause); 250 MorseDot(); 251 LightsOff(elemPause); 252 break; 253 case 't': 254 MorseDash(); 255 LightsOff(elemPause); 256 break; 257 case 'u': 258 MorseDot(); 259 LightsOff(elemPause); 260 MorseDot(); 261 LightsOff(elemPause); 262 MorseDash(); 263 LightsOff(elemPause); 264 break; 265 case 'v': 266 MorseDot(); 267 LightsOff(elemPause); 268 MorseDot(); 269 LightsOff(elemPause); 270 MorseDot(); 271 LightsOff(elemPause); 272 MorseDash(); 273 LightsOff(elemPause); 274 break; 275 case 'w': 276 MorseDot(); 277 LightsOff(elemPause); 278 MorseDash(); 279 LightsOff(elemPause); 280 MorseDash(); 281 LightsOff(elemPause); 282 break; 283 case 'x': 284 MorseDash(); 285 LightsOff(elemPause); 286 MorseDot(); 287 LightsOff(elemPause); 288 MorseDot(); 289 LightsOff(elemPause); 290 MorseDash(); 291 LightsOff(elemPause); 292 break; 293 case 'y': 294 MorseDash(); 295 LightsOff(elemPause); 296 MorseDot(); 297 LightsOff(elemPause); 298 MorseDash(); 299 LightsOff(elemPause); 300 MorseDash(); 301 LightsOff(elemPause); 302 break; 303 case 'z': 304 MorseDash(); 305 LightsOff(elemPause); 306 MorseDash(); 307 LightsOff(elemPause); 308 MorseDot(); 309 LightsOff(elemPause); 310 MorseDot(); 311 LightsOff(elemPause); 312 break; 313 default: 314 // If a matching character was not found it will default to a blank space 315 LightsOff(Spaces); 316 } 317} 318 319/* 320 Unlicensed Software: 321 322 This is free and unencumbered software released into the public domain. 323 324 Anyone is free to copy, modify, publish, use, compile, sell, or 325 distribute this software, either in source code form or as a compiled 326 binary, for any purpose, commercial or non-commercial, and by any 327 means. 328 329 In jurisdictions that recognize copyright laws, the author or authors 330 of this software dedicate any and all copyright interest in the 331 software to the public domain. We make this dedication for the benefit 332 of the public at large and to the detriment of our heirs and 333 successors. We intend this dedication to be an overt act of 334 relinquishment in perpetuity of all present and future rights to this 335 software under copyright law. 336 337 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 338 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 339 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 340 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 341 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 342 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 343 OTHER DEALINGS IN THE SOFTWARE. 344 345 For more information, please refer to 346*/
Comments
Only logged in users can leave comments