This project uses two Arduino UNOs, each with its own computer, to exchange messages in Morse code. Laser diodes send pulses in Morse and photoresistors receive them. The two "pagers" use the same set of code and can both send and receive messages, just like texting. Unlike similar projects, this one allows all visible ASCII characters with less code.
1 / 2 • I tethered the everything to the breadboard with wires to keep them in one piece. This makes it easier to align the lasers and photoresistors.
Code breakdown:
- Morse time values: The Morse dot is 30 milliseconds long, the dash is 60, and the underscore is 90. The element space, for separating Morse elements, is 30. The character space, for separating the combinations of elements that represent an ASCII character, is 30. The message space, for separating back-to-back messages, is 60. These values can be changed without interfering with the rest of the code.
- ASCII char array: There are 96 visible characters in ASCII, starting with space (32). The Morse char array contains 96 chars made of combinations of . - _ in order of their permutations, ex. ".", "-", "_", "..", ".-", "._", "-.", "--", "-_"
- Intro loop: Serial prints directions for the user and the laser turns on so it can be aligned with the other pager's photoresistor. It asks for a username, which will be sent to the other user later. Once a username is input, it is added to a String, which is trimmed of leading and trailing spaces and printed. The laser turns off and the loop cannot run again.
- Transmitter loop: When Serial reads a message, a while loop begins that prints it with "Me:" and checks if it is the first message sent. If yes, the username is added to the beginning of the text with a colon. The sending text goes through a for loop that subtracts 32 from the ASCII value of each character. This number is the index of a char in the ASCII array, and the Morse elements in that char are added to a String with a space afterward. This loops for the number of characters in the text. A for loop checks each element in the Morse String and turns on the laser if it is a dot, dash, or underscore and delays by their respective times, or keeps the laser off for a character space delay if it is a space. Then it delays by an element space and a message space if the String is over.
- Receiver loop: When the photoresistor detects light above the laser threshold, a while loop begins that checks if the light level is below the threshold after a delay of dot length plus 5 milliseconds for buffer. If yes, a dot is added to a Morse String. If no, it checks again after another 30 millisecond delay. If yes, a dash is added. If no, an underscore is added and another delay occurs to fill the underscore length. If the photoresistor detects laser brightness again after an element space delay, a dot delay occurs and the loop restarts. If no, it adds a space to the Morse String and checks again after a character and element space delay. If yes, it adds a dot delay and the loop restarts. If still no, the loop ends and a for loop counts the number of spaces in the Morse String. An array the length of the number of spaces is declared and another for loop makes its values the index of each space in order. Another for loop adds the substring of Morse elements between each space to a String and searches for it in the Morse char array. The index of the char plus 32, an ASCII character, is added to the received text String. This for loop runs until it reaches the amount of spaces in the Morse String. The received text String is trimmed of leading and trailing spaces and, if it is the first received message, it is printed to Serial and the substring from index 0 to the first colon is added to a String as the other user's name. If it is not the first received message, it prints the username String and then the message.
Comments
Please log in or sign up to comment.