-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIR_Morse.ino
304 lines (266 loc) · 6.13 KB
/
IR_Morse.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Michael Vinciguerra
// IR_Morse.ino
// Research
// 11/2/21
// Blinks the lab name "SML" in morse code on an IR LED which needs to be interpreted by a receiver
//*************************************************************************************************
// More accurate timing libraries for Morse Code
#define F_CPU 16000000L
#include <avr/delay.h>
// Code repurposed from: https://create.arduino.cc/projecthub/electropeak/how-to-make-a-morse-code-translator-with-arduino-d6ecc8?ref=user&ref_id=573543&offset=1
// For full Morse Code table, check out: https://en.wikipedia.org/wiki/Morse_code
const int IRLED = 2; // D2 pin used as pin to drive IR LED
const int unit_delay = 25; // time between succcessive morse code symbols
// Function creates the "." character in morse code
void dot() {
digitalWrite(IRLED, HIGH); // turn on the IR LED
_delay_ms(unit_delay);
digitalWrite(IRLED, LOW); // turn off the IR LED
_delay_ms(unit_delay);
}
// Function creates the "-" character in morse code
void dash() {
digitalWrite(IRLED, HIGH); // turn on the IR LED
_delay_ms(3 * unit_delay); // dashes need to be longer
digitalWrite(IRLED, LOW); // turn off the IR LED
_delay_ms(unit_delay);
}
void A() {
dot();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void B() {
dash();
dot();
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void C() {
dash();
dot();
dash();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void D() {
dash();
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void E() {
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void F_char() {
dot();
dot();
dash();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void G() {
dash();
dash();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void H() {
dot();
dot();
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void I() {
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void J() {
dot();
dash();
dash();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void K() {
dash();
dot();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void L() {
dot();
dash();
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void M() {
dash();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void N() {
dash();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void O() {
dash();
dash();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void P() {
dot();
dash();
dash();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void Q() {
dash();
dash();
dot();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void R() {
dot();
dash();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void S() {
dot();
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void T() {
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void U() {
dot();
dot();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void V() {
dot();
dot();
dot();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void W() {
dot();
dash();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void X() {
dash();
dot();
dot();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void Y() {
dash();
dot();
dash();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
void Z() {
dash();
dash();
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
// Typically this would be reserved for the number 5, but we will
// repurpose it for our application
void space() {
dot();
dot();
dot();
dot();
dot();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
// Typically this would be reserved for the number 4, but we will
// repurpose it for our application
void period() {
dot();
dot();
dot();
dot();
dash();
_delay_ms(4 * unit_delay); // used to indicate end of character
}
/* Use an array of function pointers to make writing arbitrary
* messages easy. Relies on knowledge of C++ pointers.
*/
void (*morse[])() {
A,
B,
C,
D,
E,
F_char,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
space,
period
};
void setup() {
pinMode(IRLED, OUTPUT);
digitalWrite(IRLED, LOW);
}
void loop() {
//String message = "In view a humble vaudevillian veteran cast vicariously as both victim and villain by the vicissitudes of Fate. ... The only verdict is vengeance. A vendetta held as a votive not in vain for the value and veracity of such shall one day vindicate the vigilant and the virtuous. ";
String message = "Hello world. ";
message = prep_message(message);
write_morse(message); // cast to upper case since those are the only supported letters
}
// Problem: Unicode characters for . and ' ' aren't in line with the rest.
// Solution: Since we aren't using '[' and '\' currently, use those since they
// come right after our upper case characters in the unicode table!
// Source: https://en.wikipedia.org/wiki/List_of_Unicode_characters
String prep_message(String message) {
message.toUpperCase();
message.replace(' ', '[');
message.replace('.', '\\');
return message;
}
void write_morse(String message) {
for (int i = 0; i < message.length(); i++) {
int index = int(message[i] - 'A');
(*morse[index])();
}
}