-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse_out.ino
208 lines (178 loc) · 4 KB
/
morse_out.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Morse Out
Display some morse output on the built-in LED
Pin 2 (SOUNT_OUT) provides a line-level (but LOUD) output to emit a 1K tone matching the dots and dashes
Uses LCD to display the message as well.
*/
#include <LiquidCrystal.h>
const int SOUND_OUT = 2;
const int LED = 13;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
static int BASE_RATE = 60; // in milliseconds. 40ms is about the fastest where the tones are distinct. 50 is very fast. 75 is reasonable. 100 might be good for learning.
static int SHORT = BASE_RATE;
static int LONG = BASE_RATE * 3;
static int LETTER_SPACE = BASE_RATE * 3;
static int WORD_SPACE = BASE_RATE * 7;
const int FREQ_1K = 1000;
static int FREQUENCY = FREQ_1K;
static int ERROR_FREQUENCY = 500;
static char DOT = '.';
static char DASH = '-';
static char SPACE_CHAR = ' ';
static char EOC = '$';
static char EOS = '\0';
static char* letters[26] = {
".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--.."
};
static char* numbers[10] = {
"-----",
".----",
"..---",
"...--",
"....-",
".----",
"-....",
"--...",
"---..",
"----.",
};
static char* SPACE = " ";
static char* PERIOD = ".-.-.-";
static char* QUESTION_MARK = "--..--";
static char* EXCLAMATION_POINT = "..--.";
static char* COLON = "---...";
static char* SINGLE_QUOTE = ".----.";
static char* DOUBLE_QUOTE = ".-..-.";
static char* EQUALS_SIGN = "-...-";
static char ILLEGAL_CHARACTER[1] = {EOS};
void setup() {
pinMode(LED, OUTPUT);
pinMode(SOUND_OUT, OUTPUT);
lcd.begin(16, 2);
lcd.print("MorseOut LCD");
delay(3000);
}
void loop() {
readString("sos morse code ftw ");
readString("morse code is cool. \t");
readString("cqd cqd cqd cqd cqd cqd de mgy mgy mgy mgy mgy mgy position 41.44n. 50.24w ");
readString("come at once. we have hit a berg. ");
readString("\t");
readString("the quick brown fox jumps over the lazy dogs.");
readString("\t");
}
void readString(char* input) {
for (int i = 0; input[i] != EOS; i++) {
char* myChar = getChar(input[i]);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(myChar);
lcd.setCursor(0,1);
lcd.print(input[i]);
show(myChar);
}
}
char* getChar(char ch) {
if (ch >= 'a' && ch <= 'z') {
return letters[ch - 'a'];
} else if (ch >= '0' && ch <= '9') {
return numbers[ch - '0'];
} else switch (ch) {
case '.':
return PERIOD;
case ' ':
return SPACE;
case '?':
return QUESTION_MARK;
case '!':
return EXCLAMATION_POINT;
case ':':
return COLON;
case '\'':
return SINGLE_QUOTE;
case '"':
return DOUBLE_QUOTE;
case '=':
return EQUALS_SIGN;
default:
error();
return ILLEGAL_CHARACTER;
}
}
void show(char* letter) {
for (int i = 0; letter[i] != EOS; i++) {
if (letter[i] == DOT) dot();
else if (letter[i] == DASH) dash();
else if (letter[i] == EOC) endOfCharacter();
else if (letter[i] == SPACE_CHAR) {
pause();
return;
}
}
endOfCharacter();
}
void dot() {
blink(SHORT, SHORT);
}
void dash() {
blink(LONG,SHORT);
}
void endOfCharacter() {
delay(LETTER_SPACE);
}
void pause() {
delay(WORD_SPACE);
}
void banner() {
for (int i = 0; i < 10; i++) {
blink(100, 100);
}
pause();
}
void error() {
for (int i = 0; i < 20; i++) {
blink(50, 50, ERROR_FREQUENCY);
}
}
void blink(int on, int off) {
blink(on, off, FREQUENCY);
}
void blink(int on, int off, int frequency) {
digitalWrite(LED, HIGH);
emitTone(on, frequency);
digitalWrite(LED, LOW);
delay(off);
}
void emitTone(int duration, int frequency) { // Emit 1K tone on pin SOUND_OUT
for (int i = 0; i < duration; i++) {
digitalWrite(SOUND_OUT, HIGH);
delayMicroseconds(frequency / 2);
digitalWrite(SOUND_OUT, LOW);
delayMicroseconds(frequency / 2);
}
}