/
MorseCode.ino
129 lines (120 loc) · 3.01 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
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
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long t1; //used to measure the time
unsigned long t2; //used to measure the time
unsigned long len; //difference between t2 and t1
//AlphaNumeric Characters in Morse
String morseAlpha[] = {".-", "-...", "-.-.", "-..", ".",
"..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-",
".--", "-..-", "-.--", "--..", "E"};
String morseNumeric[] = {"-----", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "E"};
int flag = 0;
int flag1 = 0;
int inPin = 7;
String inp = "";
String out = "";
void setup() {
//init the display
lcd.begin(16, 2);
//init the button
pinMode(inPin, INPUT_PULLUP);
}
void loop() {
// wait untill there is two changes in the input
while(digitalRead(inPin) == HIGH) {/*wait*/}
t1 = millis();
while(digitalRead(inPin) == LOW) {/*wait*/}
t2 = millis();
//measurse the difference between the clicks
len = t2 - t1;
if(len > 50) {
//the switch is not perfect and will bounce
//so we need an epsilon to make an accrurate reading
if(len > 5000) {
//Clear the screen and reset the values
lcd.clear();
out = "";
inp = "";
} else {
//Translate the signal to either a . or a -
inp += sigToAlpha();
//print out the input to the lcd
printLCDTop(inp);
}
}
//wait for more letters
while((millis() - t2) < 500) {
if(digitalRead(inPin) == LOW) {
flag = 1;
break;
}
}
//if no letters were found then display the alphanumeric
//that was entered in morse
if(!flag) {
lcdClearTop();
convert();
}
flag = 0;
}
char sigToAlpha() {
if(len < 600 && len > 50) {
return '.';
}
return '-';
}
void convert() {
//if it is the escape sequence for space or period
if(inp == ".-.-.-"){
out += '.';
printLCDBottom();
return;
} else if(inp == "----") {
out += " ";
printLCDBottom();
return;
}
//iterate over the alpha and numeric lists
//to see if the letter is in either and display the new string.
int i = 0;
while(morseAlpha[i] != "E") {
if(morseAlpha[i] == inp) {
out += char('A' + i);
printLCDBottom();
inp = "";
return;
}
i++;
}
i = 0;
while(morseNumeric[i] != "E") {
if(morseNumeric[i] == inp) {
out += char('0' + i);
printLCDBottom();
inp = "";
return;
}
i++;
}
}
//print a string to the top row
void printLCDTop(String x) {
lcd.setCursor(0, 0);
lcd.print(x);
}
//print a string to the bottom row
void printLCDBottom() {
lcd.setCursor(0, 1);
lcd.print(out);
}
//clear just the top row
void lcdClearTop() {
lcd.home();
for(int i = 0; i < 16; i++) {
lcd.print(" ");
}
lcd.home();
}