-
Notifications
You must be signed in to change notification settings - Fork 0
/
morsecode.ino
69 lines (66 loc) · 1.14 KB
/
morsecode.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
const int buzzerPin = 8;
const String morseCodes[] = {
".-", // A
"-...", // B
"-.-.", // C
"-..", // D
".", // E
"..-.", // F
"--.", // G
"....", // H
"..", // I
".---", // J
"-.-", // K
".-..", // L
"--", // M
"-.", // N
"---", // O
".--.", // P
"--.-", // Q
".-.", // R
"...", // S
"-", // T
"..-", // U
"...-", // V
".--", // W
"-..-", // X
"-.--", // Y
"--.." // Z
};
// setup
void setup() {
pinMode(buzzerPin, OUTPUT);
}
// loop
void loop() {
String inputText = "Hello World";
playMorseCode(inputText);
delay(1000);
}
void playMorseCode(String text) {
for (int i = 0; i < text.length(); i++) {
char c = toupper(text[i]);
if (c == ' ') {
delay(700);
} else {
String morse = morseCodes[c - 'A'];
for (int j = 0; j < morse.length(); j++) {
if (morse[j] == '.') {
playDot();
} else if (morse[j] == '-') {
playDash();
}
delay(200);
}
delay(400);
}
}
}
void playDot() {
tone(buzzerPin, 1000, 100);
delay(150);
}
void playDash() {
tone(buzzerPin, 1000, 300);
delay(150);
}