Laser Pager / Morse Code Transceiver
Send texts with lasers in Morse code!
Components and supplies
2
Jumper wires (generic)
2
Photo resistor
2
Resistor 10k ohm
2
Breadboard (generic)
2
Laser Diode, 655 nm
2
Arduino UNO
Apps and platforms
1
Arduino IDE
1
Arduino IDE
Project description
Code
Laser Pager Code
arduino
Copy and paste to both computers. When you run it, put Serial in fullscreen and don't start texting until a username is entered on both ends.
1//stuff for laser and photoresistor 2const int laserPin = 2; 3const int photoPin = A0; 4const int lightLimit = 800; 5int photoVal; 6 7//stuff for intro 8String myName = ""; 9boolean introEnd = false; 10 11//stuff for transmitting texts 12String myText; 13String myMorse; 14String myMorseChar; 15int myTextNum = 0; 16 17//stuff for receiving texts 18String theirMorse; 19boolean messEnd = false; 20int spaces = 0; 21int spaceIndex; 22String theirMorseChar; 23String theirText; 24String theirName; 25int theirTextNum = 0; 26 27//time values for laser pulses 28const int dot = 30; 29const int dash = dot * 2; 30const int undscr = dot * 3; 31const int elemSpace = dot; 32const int charSpace = dot; 33const int messSpace = dot * 2; 34 35//array of Morse values in order of corresponding ascii characters starting with space (decimal 32) 36const char* asciiArray[] = { 37 38// space to / 39".", "-", "_", "..", ".-", "._", "-.", "--", "-_", "_.", "_-", "__", "...", "..-", ".._", ".-.", 40// 0 to 9 41".--", ".-_", "._.", "._-", ".__", "-..", "-.-", "-._", "--.", "---", 42// : to @ 43"--_", "-_.", "-_-", "-__", "_..", "_.-", "_._", 44// A to M 45"_-.", "_--", "_-_", "__.", "__-", "___", "....", "...-", "..._", "..-.", "..--", "..-_", ".._.", 46// N to Z 47".._-", "..__", ".-..", ".-.-", ".-._", ".--.", ".---", ".--_", ".-_.", ".-_-", ".-__", "._..", "._.-", 48// [ to ` 49"._._", "._-.", "._--", "._-_", ".__.", ".__-", 50// a to m 51".___", "-...", "-..-", "-.._", "-.-.", "-.--", "-.-_", "-._.", "-._-", "-.__", "--..", "--.-", "--._", 52// n to z 53"---.", "----", "---_", "--_.", "--_-", "--__", "-_..", "-_.-", "-_._", "-_-.", "-_--", "-_-_", "-__.", 54// { to ~ 55"-__-", "-___", "_...", "_..-" 56}; 57 58void setup() { 59 //initialize and turn on laser 60 pinMode(laserPin, OUTPUT); 61 digitalWrite(laserPin, HIGH); 62 63 //set Serial and print instructions 64 Serial.begin(9600); 65 Serial.println(F("Align the laser and sensor with another user's, then enter a username.")); 66} 67 68void loop() { 69 //intro keeps laser on until username input 70 while(introEnd == false) { 71 72 //when user sends something to Serial 73 if(Serial.available()>0) { 74 75 //new myName from Serial has leading and trailing whitespace removed 76 myName = Serial.readString(); 77 myName.trim(); 78 } 79 80 //when myName value has changed 81 if(myName != "") { 82 83 //turn off laser and allow next while loop 84 digitalWrite(laserPin, LOW); 85 introEnd = true; 86 87 //intro message with separating line 88 Serial.print(F("Hi, ")); 89 Serial.print(myName); 90 Serial.println(F("! Message away.")); 91 Serial.print(F("─────────────────────────────────────────────────────────")); 92 Serial.println(F("────────────────────────────────────────────────────────")); 93 } 94 } 95 96 //the meat and potatoes if yknow what I mean 97 while(introEnd == true) { 98 99 //transmitter loop starting when Serial gets something 100 while(Serial.available() > 0) { 101 102 //prints text sent to Serial for self 103 myText = Serial.readString(); 104 myText.trim(); 105 Serial.print(F("Me: ")); 106 Serial.println(myText); 107 Serial.println(); 108 109 //turns myName into sending format 110 myName += ": "; 111 112 //only sends username in first message, shortens time for subsequent messages 113 if(myTextNum == 0) { 114 myText = myName + myText; 115 } 116 117 //loop for constructing Morse code from myText 118 for(int i=0; i < myText.length(); i++) { 119 120 //subtract ascii value of each char in myMorseName by 32 for place in array and adds thus Morse to myMorse separated by spaces 121 myMorseChar = asciiArray[myText.charAt(i) - 32]; 122 myMorse += myMorseChar + " "; 123 } 124 125 //converts Morse characters to laser pulses 126 for(int i=0; i < myMorse.length(); i++) { 127 128 //repurposes myMorseChar for individual Morse elements 129 myMorseChar = myMorse.charAt(i); 130 131 //if character is a dot 132 if(myMorseChar == ".") { 133 digitalWrite(laserPin, HIGH); 134 delay(dot); 135 } 136 //if character is a dash 137 else if(myMorseChar == "-") { 138 digitalWrite(laserPin, HIGH); 139 delay(dash); 140 } 141 //if character is an underscore 142 else if(myMorseChar == "_") { 143 digitalWrite(laserPin, HIGH); 144 delay(undscr); 145 } 146 //if character is a space 147 else if(myMorseChar == " ") { 148 delay(charSpace); 149 } 150 //space between elements 151 digitalWrite(laserPin, LOW); 152 delay(elemSpace); 153 } 154 155 //clear myMorse, add 1 to sent message total and delay before next message 156 myMorse = ""; 157 myTextNum++; 158 delay(messSpace); 159 } 160 161 //receiver loop starting when laser brightness detected 162 while(analogRead(photoPin) > lightLimit) { 163 164 //reset boolean for ending Morse retrieval loop and delay by dot length + 5 165 messEnd = false; 166 delay(dot + 5); 167 168 //loop for getting Morse characters, starts when dot would end and ends when time off is longer than space pause 169 while(messEnd == false) { 170 171 //delay for dot time and add dot if laser is off by then, otherwise go to dash/undscr 172 if(analogRead(photoPin) < lightLimit) { 173 theirMorse += "."; 174 } 175 //delay for additional dot, add dash if off by then, otherwise add undscr and delay for another dot 176 else { 177 delay(dash - dot); 178 if(analogRead(photoPin) < lightLimit) { 179 theirMorse += "-"; 180 } 181 else { 182 theirMorse += "_"; 183 delay(undscr - dash); 184 } 185 } 186 //delay by element space for next element 187 delay(elemSpace); 188 189 //if laser is not back on after element space, delay and add dash space 190 if(analogRead(photoPin) < lightLimit) { 191 delay(charSpace + elemSpace); 192 theirMorse += " "; 193 194 //delay by dot if laser is back on, otherwise end loop 195 if(analogRead(photoPin) < lightLimit) { 196 delay(dot); 197 } 198 else { 199 messEnd = true; 200 } 201 } 202 //if laser is back on after element space, delay and restart loop 203 else { 204 delay(dot); 205 } 206 } 207 208 //count spaces in theirMorse to make size of spaceIndexArray 209 for(int i=0; i <= 95; i++) { 210 if(theirMorse[i] == ' ') { 211 spaces++; 212 } 213 } 214 int spaceIndexArray[spaces]; 215 216 //find positions of spaces in theirMorse and add to spaceIndexArray 217 for(int i=0; i <= spaces; i++) { 218 if(i == 0) { 219 spaceIndexArray[i] = theirMorse.indexOf(' '); 220 } 221 else { 222 spaceIndexArray[i] = theirMorse.indexOf(' ', spaceIndexArray[i - 1] + 1); 223 } 224 } 225 226 //extract Morse chars with substrings starting after previous space and ending on current space 227 for(int i=0; i <= spaces; i++) { 228 if(i == 0) { 229 theirMorseChar = theirMorse.substring(0, spaceIndexArray[i]); 230 } 231 else { 232 theirMorseChar = theirMorse.substring(spaceIndexArray[i - 1] + 1, spaceIndexArray[i]); 233 } 234 235 //find theirMorseChar in asciiArray and add corresponding char to theirText 236 for(int j=0; j <= 95; j++) { 237 if(theirMorseChar == asciiArray[j]) { 238 theirText += char (j + 32); 239 } 240 } 241 } 242 243 //trim theirText and only register message if it is not blank 244 theirText.trim(); 245 if(theirText.length() > 0) { 246 247 //if this is the first received message, print text normally 248 if(theirTextNum == 0) { 249 Serial.println(theirText); 250 Serial.println(); 251 252 //extract username with substring ending after first colon 253 theirName = theirText.substring(0, theirText.indexOf(": ") + 2); 254 } 255 256 //after first message, print name and new text 257 else { 258 Serial.print(theirName); 259 Serial.println(theirText); 260 Serial.println(); 261 } 262 263 //add 1 to received message total 264 theirTextNum++; 265 } 266 267 //reset theirMorse, spaces and theirText 268 theirMorse = ""; 269 spaces = 0; 270 theirText = ""; 271 } 272 } 273}
Downloadable files
Circuit Diagram
The basic circuit.
Circuit Diagram

Comments
Only logged in users can leave comments