Components and supplies
Limit Switch, 5 A
Resistor 1k ohm
Breadboard (generic)
Active Buzzer
Jumper wires (generic)
Arduino UNO
LED (generic)
Apps and platforms
Arduino IDE
Project description
Code
Code
c_cpp
1// Needs Fifo.h (Queue functions) copied into the sketch folder 2#include "Fifo.h" 3Fifo<char> symbolQueue( 160 ) ; 4 5const uint8_t led = 2; 6const uint8_t ledPin = 3; 7const uint8_t buz = 4; 8const uint8_t buzPin = 5; 9const int inputPin = 6; 10 11String code = ""; 12 13// To increase speed, change '50' to a smaller number.. 14// To decrease speed, change '50' to a larger number.. 15const unsigned long MsecDot = 50; 16const unsigned long MsecDash = MsecDot * 3; 17const unsigned long MsecSym = MsecDot; 18const unsigned long MsecLtr = MsecDot * 3; 19const unsigned long MsecWord = MsecDot * 7; 20 21String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", 22 ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", 23 "-----" , ".----" , "..---" , "...--" , "....-" , "....." , "-...." , "--..." , "---.." , "----." , "E" };// Morse letters (26) followed by numbers(10) 24 25void convertor(){ 26 27 int i = 0; 28 if (code == ".-.-.-") 29 { 30 Serial.print("."); 31 } 32 else 33 { 34 while (letters[i] != "E") 35 { 36 if (letters[i] == code) 37 { 38 if ( i >= 0 && i <= 25 ) Serial.print(char('A' + i)); 39 else if ( i >= 26 && i <= 35 ) Serial.print(char('0' + i - 26 )); 40 break; 41 } 42 i++; 43 } 44 if (letters[i] == "E") 45 { 46 Serial.println(""); 47 } 48 } 49 code = ""; 50} 51 52bool getKey() { 53 // get debounced key press 54 static bool currentKeyState = HIGH ; 55 static bool newKeyState = HIGH ; 56 static uint32_t newKeyStateAtMs = millis() ; 57 58 bool dr = digitalRead(inputPin) ; 59 if ( dr != newKeyState ) { 60 newKeyState = dr ; 61 newKeyStateAtMs = millis() ; 62 } 63 if ( currentKeyState != newKeyState && millis() - newKeyStateAtMs > 25 ) { 64 // debounce: accept only a mature change ( > X ms ) 65 currentKeyState = newKeyState ; 66 } 67 return currentKeyState ; 68} 69 70void decoder() { 71 72 static bool lastKey = HIGH ; 73 static uint32_t lastTranstionAtMs = millis() ; 74 static bool inLetter = false ; 75 static bool inWord = false ; 76 77 uint32_t ms = millis() ; 78 79 digitalWrite( ledPin, !getKey() ) ; 80 digitalWrite( buzPin, !getKey() ) ; 81 82 if ( getKey() != lastKey ) { 83 // transition 84 if ( lastKey == LOW ) { 85 // we were in a symbol - determine if it was a '.' or a '-' 86 code += ( ms - lastTranstionAtMs < MsecDot * 2 ? '.' : '-' ) ; 87 } else { 88 // we were in a space 89 inLetter = true ; 90 inWord = true ; 91 } 92 lastTranstionAtMs = ms ; 93 lastKey = getKey() ; 94 } 95 96 if ( getKey() == HIGH ) { 97 // we are in a space 98 if ( inLetter && ms - lastTranstionAtMs > MsecLtr ) { 99 // flush out letter 100 convertor() ; 101 inLetter = false ; 102 } 103 if ( inWord && ms - lastTranstionAtMs > MsecWord ) { 104 // flush out word 105 Serial.print(" " ) ; 106 inWord = false ; 107 } 108 } 109} 110 111void charToMorse( char mChar ) { 112 // converts input character to a set of morse symbols which are pushed onto a 113 // queue together with an end of character marker '|' 114 115 uint8_t offset ; // offset into table letters[] based on ascii code of char 116 bool inTable = false ; 117 118 if ( mChar >= 'a' && mChar <= 'z' ) { 119 offset = mChar - 97 ; 120 inTable = true ; 121 } 122 else if ( mChar >= 'A' && mChar <= 'Z' ) { 123 offset = mChar - 64 ; 124 inTable = true ; 125 } 126 else if ( mChar >= '0' && mChar <= '9' ) { 127 offset = (mChar - 48) + 26 ; 128 inTable = true ; 129 } 130 else {} 131 132 if ( inTable ) { 133 // Serial.print( letters[ offset ] ) ; 134 for ( uint8_t i = 0 ; i < letters[ offset ].length() ; i++ ) symbolQueue.push( letters[ offset ].charAt( i ) ) ; 135 symbolQueue.push( '|' ) ; 136 } 137 else if ( mChar == ' ' ) { 138 // Serial.print( '|' ) ; 139 symbolQueue.push( '|' ) ; 140 } 141} 142 143void setup() { 144 Serial.begin(9600); 145 pinMode(led, OUTPUT); 146 pinMode(ledPin, OUTPUT); 147 pinMode(buz, OUTPUT); 148 pinMode(buzPin, OUTPUT); 149 pinMode(inputPin, INPUT_PULLUP); 150 Serial.println("Start"); 151 symbolQueue.flush() ; 152} 153 154void loop() { 155 enum state_t { inDot, inDash, inSpace, inWaiting } ; 156 static state_t state = inWaiting ; 157 static uint32_t inStateAtMs = millis() ; 158 159 // get input from user, analyse, convert to morse symbols and queue 160 if (Serial.available()) { 161 char inChar = Serial.read() ; 162 charToMorse( inChar ) ; 163 } 164 165 // finite state machine - unload queue and sound buzzer as required 166 167 switch ( state ) { 168 169 case inWaiting : { 170 if ( ! symbolQueue.isempty() ) { 171 char inCh = symbolQueue.pop() ; 172 // debug 173 Serial.print( inCh ) ; 174 175 inStateAtMs = millis() ; 176 if ( inCh == '.' ) { 177 digitalWrite(led, HIGH); 178 digitalWrite(buz, HIGH); 179 state = inDot ; 180 } 181 else if ( inCh == '-' ) { 182 digitalWrite(led, HIGH); 183 digitalWrite(buz, HIGH); 184 state = inDash ; 185 } 186 else if ( inCh == '|' ) { 187 state = inSpace ; 188 } 189 } 190 break ; 191 } 192 193 case inDot : { 194 if ( millis() - inStateAtMs > MsecDot ) { 195 inStateAtMs = millis() ; 196 digitalWrite(led, LOW); 197 digitalWrite(buz, LOW); 198 state = inSpace ; 199 } 200 break ; 201 } 202 203 case inDash : { 204 if ( millis() - inStateAtMs > MsecDash ) { 205 inStateAtMs = millis() ; 206 digitalWrite(led, LOW); 207 digitalWrite(buz, LOW); 208 state = inSpace ; 209 } 210 break ; 211 } 212 213 case inSpace : { 214 if ( millis() - inStateAtMs > MsecLtr ) { 215 inStateAtMs = millis() ; 216 digitalWrite(led, LOW); 217 digitalWrite(buz, LOW); 218 state = inWaiting ; 219 } 220 break ; 221 } 222 } 223 224 decoder() ; 225} 226
Code
c_cpp
1// Needs Fifo.h (Queue functions) copied into the sketch folder 2#include 3 "Fifo.h" 4Fifo<char> symbolQueue( 160 ) ; 5 6const uint8_t led = 2; 7const 8 uint8_t ledPin = 3; 9const uint8_t buz = 4; 10const uint8_t buzPin = 5; 11const 12 int inputPin = 6; 13 14String code = ""; 15 16// To increase speed, change 17 '50' to a smaller number.. 18// To decrease speed, change '50' to a larger number.. 19const 20 unsigned long MsecDot = 50; 21const unsigned long MsecDash = MsecDot * 3; 22const 23 unsigned long MsecSym = MsecDot; 24const unsigned long MsecLtr = MsecDot 25 * 3; 26const unsigned long MsecWord = MsecDot * 7; 27 28String letters[] = 29 {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 30 ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", 31 ".-.", 32 "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", 33 "-----" 34 , ".----" , "..---" , "...--" , "....-" , "....." , "-...." , 35 "--..." , "---.." , "----." , "E" };// Morse letters (26) followed by numbers(10) 36 37 38void convertor(){ 39 40 int i = 0; 41 if (code == ".-.-.-") 42 43 { 44 Serial.print("."); 45 } 46 else 47 { 48 while (letters[i] 49 != "E") 50 { 51 if (letters[i] == code) 52 { 53 if ( i 54 >= 0 && i <= 25 ) Serial.print(char('A' + i)); 55 else if ( i >= 26 && i 56 <= 35 ) Serial.print(char('0' + i - 26 )); 57 break; 58 } 59 i++; 60 61 } 62 if (letters[i] == "E") 63 { 64 Serial.println(""); 65 66 } 67 } 68 code = ""; 69} 70 71bool getKey() { 72 // get debounced 73 key press 74 static bool currentKeyState = HIGH ; 75 static bool newKeyState 76 = HIGH ; 77 static uint32_t newKeyStateAtMs = millis() ; 78 79 bool dr = digitalRead(inputPin) 80 ; 81 if ( dr != newKeyState ) { 82 newKeyState = dr ; 83 newKeyStateAtMs 84 = millis() ; 85 } 86 if ( currentKeyState != newKeyState && millis() - newKeyStateAtMs 87 > 25 ) { 88 // debounce: accept only a mature change ( > X ms ) 89 currentKeyState 90 = newKeyState ; 91 } 92 return currentKeyState ; 93} 94 95void decoder() 96 { 97 98 static bool lastKey = HIGH ; 99 static uint32_t lastTranstionAtMs 100 = millis() ; 101 static bool inLetter = false ; 102 static bool inWord = false 103 ; 104 105 uint32_t ms = millis() ; 106 107 digitalWrite( ledPin, !getKey() ) ; 108 109 digitalWrite( buzPin, !getKey() ) ; 110 111 if ( getKey() != lastKey ) { 112 113 // transition 114 if ( lastKey == LOW ) { 115 // we were in a symbol 116 - determine if it was a '.' or a '-' 117 code += ( ms - lastTranstionAtMs < 118 MsecDot * 2 ? '.' : '-' ) ; 119 } else { 120 // we were in a space 121 122 inLetter = true ; 123 inWord = true ; 124 } 125 lastTranstionAtMs 126 = ms ; 127 lastKey = getKey() ; 128 } 129 130 if ( getKey() == HIGH ) { 131 132 // we are in a space 133 if ( inLetter && ms - lastTranstionAtMs > MsecLtr 134 ) { 135 // flush out letter 136 convertor() ; 137 inLetter = false 138 ; 139 } 140 if ( inWord && ms - lastTranstionAtMs > MsecWord ) { 141 // 142 flush out word 143 Serial.print(" " ) ; 144 inWord = false ; 145 } 146 147 } 148} 149 150void charToMorse( char mChar ) { 151 // converts input character 152 to a set of morse symbols which are pushed onto a 153 // queue together with an 154 end of character marker '|' 155 156 uint8_t offset ; // offset into 157 table letters[] based on ascii code of char 158 bool inTable = false ; 159 160 161 if ( mChar >= 'a' && mChar <= 'z' ) { 162 offset = mChar - 97 ; 163 inTable 164 = true ; 165 } 166 else if ( mChar >= 'A' && mChar <= 'Z' ) { 167 offset = 168 mChar - 64 ; 169 inTable = true ; 170 } 171 else if ( mChar >= '0' && mChar 172 <= '9' ) { 173 offset = (mChar - 48) + 26 ; 174 inTable = true ; 175 } 176 177 else {} 178 179 if ( inTable ) { 180 // Serial.print( letters[ offset ] ) 181 ; 182 for ( uint8_t i = 0 ; i < letters[ offset ].length() ; i++ ) symbolQueue.push( 183 letters[ offset ].charAt( i ) ) ; 184 symbolQueue.push( '|' ) ; 185 } 186 187 else if ( mChar == ' ' ) { 188 // Serial.print( '|' ) ; 189 symbolQueue.push( 190 '|' ) ; 191 } 192} 193 194void setup() { 195 Serial.begin(9600); 196 pinMode(led, 197 OUTPUT); 198 pinMode(ledPin, OUTPUT); 199 pinMode(buz, OUTPUT); 200 pinMode(buzPin, 201 OUTPUT); 202 pinMode(inputPin, INPUT_PULLUP); 203 Serial.println("Start"); 204 205 symbolQueue.flush() ; 206} 207 208void loop() { 209 enum state_t { inDot, inDash, 210 inSpace, inWaiting } ; 211 static state_t state = inWaiting ; 212 static uint32_t 213 inStateAtMs = millis() ; 214 215 // get input from user, analyse, convert to 216 morse symbols and queue 217 if (Serial.available()) { 218 char inChar = Serial.read() 219 ; 220 charToMorse( inChar ) ; 221 } 222 223 // finite state machine - unload 224 queue and sound buzzer as required 225 226 switch ( state ) { 227 228 case inWaiting 229 : { 230 if ( ! symbolQueue.isempty() ) { 231 char inCh = symbolQueue.pop() 232 ; 233 // debug 234 Serial.print( inCh ) ; 235 236 237 inStateAtMs = millis() ; 238 if ( inCh == '.' ) { 239 digitalWrite(led, 240 HIGH); 241 digitalWrite(buz, HIGH); 242 state = inDot ; 243 244 } 245 else if ( inCh == '-' ) { 246 digitalWrite(led, 247 HIGH); 248 digitalWrite(buz, HIGH); 249 state = inDash ; 250 251 } 252 else if ( inCh == '|' ) { 253 state = inSpace 254 ; 255 } 256 } 257 break ; 258 } 259 260 case inDot 261 : { 262 if ( millis() - inStateAtMs > MsecDot ) { 263 inStateAtMs 264 = millis() ; 265 digitalWrite(led, LOW); 266 digitalWrite(buz, 267 LOW); 268 state = inSpace ; 269 } 270 break ; 271 } 272 273 274 case inDash : { 275 if ( millis() - inStateAtMs > MsecDash ) { 276 inStateAtMs 277 = millis() ; 278 digitalWrite(led, LOW); 279 digitalWrite(buz, 280 LOW); 281 state = inSpace ; 282 } 283 break ; 284 } 285 286 287 case inSpace : { 288 if ( millis() - inStateAtMs > MsecLtr ) { 289 inStateAtMs 290 = millis() ; 291 digitalWrite(led, LOW); 292 digitalWrite(buz, 293 LOW); 294 state = inWaiting ; 295 } 296 break ; 297 } 298 299 } 300 301 decoder() ; 302} 303
Downloadable files
Diagram
Diagram
Diagram
I added a Push Button in the diagram. You can also use a limit switch..
Diagram
Diagram
Diagram
Documentation
Necessary File
Add this file to the source folder and rename it "Fifo.h"..
Necessary File
Necessary File
Add this file to the source folder and rename it "Fifo.h"..
Necessary File
Comments
Only logged in users can leave comments