Components and supplies
Resistor 221 ohm
Jumper wires (generic)
Rotary potentiometer (generic)
Buzzer
Arduino UNO
Breadboard (generic)
Standard LCD - 16x2 White on Blue
Apps and platforms
Arduino IDE
Arduino Web Editor
Project description
Code
Arduino file
arduino
Copy and paste it into your Arduino project editor to use it
1#include <LiquidCrystal.h> 2 3LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 4 5#define BUZZER 11 6 7#define SMALLER_DELAY 128 8 9PROGMEM const uint8_t morse_code_a_z[26] = { 10 0b01100000, // A .- 11 0b10010101, // B -... 12 0b10011001, // C -.-. 13 0b10010100, // D -.. 14 0b01000000, // E . 15 0b01011001, // F ..-. 16 0b10100100, // G --. 17 0b01010101, // H .... 18 0b01010000, // I .. 19 0b01101010, // J .--- 20 0b10011000, // K -.- 21 0b01100101, // L .-.. 22 0b10100000, // M -- 23 0b10010000, // N -. 24 0b10101000, // O --- 25 0b01101001, // P .--. 26 0b10100110, // Q --.- 27 0b01100100, // R .-. 28 0b01010100, // S ... 29 0b10000000, // T - 30 0b01011000, // U ..- 31 0b01010110, // V ...- 32 0b01101000, // W .-- 33 0b10010110, // X -..- 34 0b10011010, // Y -.-- 35 0b10100101, // Z --.. 36}; 37 38void send_bit(const bool& dash) { 39 lcd.print(dash ? '-' : '.'); 40 analogWrite(BUZZER, 63); 41 delay((dash ? 3 : 1) * SMALLER_DELAY); 42 digitalWrite(BUZZER, LOW); 43 delay(1 * SMALLER_DELAY); 44} 45 46uint8_t hpos = 0; 47 48void send_char(const char& c) { 49 // Send the character to the LCD 50 lcd.setCursor(hpos++, 0); 51 lcd.print(c); 52 53 // Position the cursor to the next line to start the .- sequence 54 lcd.setCursor(0, 1); 55 lcd.print(" "); 56 lcd.setCursor(0, 1); 57} 58 59void print_number(const char& number) { 60 if (!isDigit(number)) return; 61 62 send_char(number); 63 64 uint8_t num = number - '0'; 65 for (uint8_t i = 0 ; i < 5 ; i++) { 66 if (num == i) num += 10; 67 68 if (num < 6 + i) send_bit(false); 69 else send_bit(true); 70 } 71} 72 73void print_character(char character) { 74 if (!isAlpha(character)) return; 75 76 if (isLowerCase(character)) character += 'A' - 'a'; 77 78 send_char(character); 79 80 uint8_t code = morse_code_a_z[character - 'A']; 81 while (code != 0) { 82 switch (code >> 6) { 83 case 0: 84 break; 85 case 1: 86 send_bit(false); 87 break; 88 case 2: 89 send_bit(true); 90 break; 91 case 3: 92 Serial.print("Error while printing character: "); 93 Serial.println(character); 94 Serial.print("Got 3 in the 2 leftmost bits of code "); 95 Serial.println(code, BIN); 96 break; 97 default: 98 Serial.print("Error while printing character: "); 99 Serial.println(character); 100 Serial.print("Got ? in the 2 leftmost bits of code "); 101 Serial.println(code, BIN); 102 } 103 code <<= 2; 104 } 105} 106 107void print_whitespace() { 108 send_char(' '); 109 delay(3 * SMALLER_DELAY); 110} 111 112void print_char(const char& character) { 113 if (isWhitespace(character)) print_whitespace(); 114 else if (isAlpha(character)) print_character(character); 115 else if (isDigit(character)) print_number(character); 116 else if (isPunct(character)) { send_char(character); delay(1 * SMALLER_DELAY); } 117 else if (character == '\\0') {} 118 else if (character == '\\1') { /* This happens at the end of every string, don't know why */ } 119 else { 120 Serial.print("Error: unknown character "); 121 Serial.print(character); 122 Serial.print(" (0b"); 123 Serial.print((uint8_t)character, BIN); 124 Serial.println(")"); 125 send_char('?'); 126 analogWrite(BUZZER, 63); 127 delay(4 * SMALLER_DELAY); 128 digitalWrite(BUZZER, LOW); 129 delay(1 * SMALLER_DELAY); 130 } 131 132 delay(2 * SMALLER_DELAY); 133} 134 135void print_string(const char* characters, int len = 15) { 136 for (uint8_t pos = 0 ; pos <= len ; pos++) { 137 print_char(characters[pos]); 138 } 139} 140 141void reset_LCD() { 142 hpos = 0; 143 144 lcd.clear(); 145 lcd.noCursor(); 146 lcd.setCursor(0, 0); 147} 148 149void setup() { 150 // put your setup code here, to run once: 151 Serial.begin(9600); 152 153 lcd.begin(16, 2); 154 reset_LCD(); 155 156 pinMode(BUZZER, OUTPUT); 157} 158 159char* readStr() { 160 uint8_t idx = 0; 161 char* str = (char*)malloc(128 * sizeof(char)); 162 163 while (Serial.available() && idx < 128) { 164 str[idx] = Serial.read(); 165 idx++; 166 delay(2); 167 } 168 for ( ; idx < 128 ; idx++) { 169 str[idx] = '\\0'; 170 } 171 172 return str; 173} 174 175void serialEvent() { 176 while (Serial.available()) { 177 char* string; 178 Serial.setTimeout(-1); 179 string = readStr(); 180 Serial.print("Received: "); 181 Serial.println(string); 182 183 char substr[16]; 184 for (uint8_t c = 0 ; c < 128 && string[c] != '\\0' && string[c] != '\r' && string[c] != '\n' ; c += 16) { 185 for (uint8_t offset = 0 ; offset < 15 ; offset++) { 186 if (string[c + offset] != '\\0' && string[c + offset] != '\r' && string[c + offset] != '\n') { 187 substr[offset] = string[c + offset]; 188 } else { 189 substr[offset] = '\\0'; 190 } 191 } 192 193 reset_LCD(); 194 print_string(substr); 195 } 196 197 free((void*)string); 198 } 199 lcd.setCursor(0, 1); 200 lcd.print(" "); 201 lcd.setCursor(0, 0); 202} 203 204void loop() { 205} 206 207 208
Downloadable files
Fritzing file
This is the sketch of this project. Use it with Fritzing.
Fritzing file
Fritzing file
This is the sketch of this project. Use it with Fritzing.
Fritzing file
Comments
Only logged in users can leave comments
rajdakin
0 Followers
•0 Projects
Table of contents
Intro
7
0