-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse_detector.ino
189 lines (169 loc) · 4.29 KB
/
morse_detector.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
#include <Bounce2.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(-1);
char dot = '.';
char dash = '-';
int diff1, diff2, increment1, increment2;
int pos_time = 50;
String morse_string;
String sentance;
#define BUTTON_PIN 2
#define BUTTON_PIN1 3
int temp = 0;
int end_time1 = 5000, end_time2 = 5000;
int start_time1 = 0, start_time2 = 0;
int pos = 0, check1 = 0, check2 = 0;
int j=1;
static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
int prepos = 0; int p = 0;
Bounce debouncer1 = Bounce(); // Instantiate a Bounce object
Bounce debouncer2 = Bounce();
void setup()
{
debouncer1.attach(BUTTON_PIN, INPUT_PULLUP);
debouncer2.attach(BUTTON_PIN1, INPUT_PULLUP);
debouncer1.interval(5); //
debouncer2.interval(5);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
// display a line of text
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,0);
display.print("Decoading morse code..");
display.display();
Serial.begin(115200);
}
void displaystring(char x)
{
morse_string += x;
Serial.println(morse_string);
}
void morse(String x)
{
Serial.println(x);
for (int i = 0; i < 25; ++i)
{
if (letters[i] == x)
{
decoder(i);
break;
}
}
}
void decoder(int z)
{
String s[] = {"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
sentance += s[z];
display.clearDisplay();
display.display();
// display a line of text
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,0);
display.print(sentance);
display.display();
Serial.println(sentance);
}
void append()
{ check1 = millis() - pos_time;
if (check1 >= 2.5*end_time1 && check1 <=3.5*end_time1 || check1>=(2.5/3)*end_time2 && check1<=end_time2 )
{
morse(morse_string);
Serial.println(morse_string);
morse_string = "";
Serial.println(check1);
}
if (check1 >= 4.5* end_time1 && 5<=end_time1|| check1 >= (4.5/12)*end_time2 && check1<= (5/12)*end_time2)
{if(j==1){
sentance += " ";
Serial.println(sentance);
Serial.println(check1);
j=0;}
}
}
void loop() {
//////////////////
//ENCODER_UPDATE//
/////////////////
//---------------------------------------------------------------------------
debouncer1.update(); // Update the Bounce instance
debouncer2.update();
if ( debouncer1.fell() )
{
if (digitalRead(3) == HIGH)
{
++pos;
}
else
{
--pos;
}
}
if ( debouncer2.fell())
{
if (digitalRead(2) == LOW)
{
++pos;
}
else
{
--pos;
}
}
//------------------------------------------------------------------------------
if (increment1 == 1)
{
start_time1 = millis();
}
if (increment2 == -1)
{
start_time2 = millis();
}
if (pos == prepos)
{ append();
}
if (prepos != pos)
{check1=0;
j=1;
pos_time = millis();
Serial.print("pos :");
Serial.println(pos);
if (pos > prepos)
{
//Append dot
diff1 = pos - prepos;
increment2 = 0;
increment1 = increment1 + diff1;
Serial.print("increment1 :"); Serial.println(increment1);
if (increment1 >= 40)
{
increment1 = 0;
displaystring(dot);
end_time1 = millis() - start_time1; //Time for dot
Serial.print("Time Period dot :"); Serial.println(end_time1);
}
}
if (pos < prepos)
{
//Append dash
diff2 = pos - prepos;
increment1 = 0;
increment2 = increment2 + diff2;
Serial.print("increment2 :"); Serial.println(increment2);
if (increment2 <= -40)
{
increment2 = 0;
displaystring(dash);
end_time2 = millis() - start_time2; //Time for dash
Serial.print("Time Period dash :"); Serial.println(end_time2);
}
}
prepos = pos;
}
}