23
23
#define WIFI_SSID " SSID"
24
24
#define WIFI_PASSWORD " PASSWORD"
25
25
26
+ const int shortMillis = 500 ;
27
+ const int longMillis = shortMillis * 3 ;
28
+
26
29
// We define morse . to be binary 0 and - to be binary 1. The longest letter
27
30
// is 5 morse elements long so we would have a sparse array of 2^5=32. But
28
31
// we need to add a leading 1 to ensure that .- and ..- are not the same value.
@@ -35,7 +38,7 @@ Adafruit_SSD1306 display(oledResetPin);
35
38
36
39
const int morseButtonPin = 2 ;
37
40
38
- void updateDisplay (const String& message, const char & indicator);
41
+ void updateDisplay (const String& message, const char & indicator, int code );
39
42
void initializeMorseToChar ();
40
43
41
44
void setup () {
@@ -59,21 +62,18 @@ void setup() {
59
62
Serial.println (WiFi.localIP ());
60
63
}
61
64
62
- const int shortMillis = 500 ;
63
- const int longMillis = shortMillis * 3 ;
64
-
65
65
String currentMessage;
66
66
int currentLetter;
67
67
void loop () {
68
68
// Wait while button is up.
69
69
int upStarted = millis ();
70
70
while (digitalRead (morseButtonPin) == HIGH) {
71
71
if (millis () - upStarted > longMillis) {
72
- updateDisplay (currentMessage, ' w' );
72
+ updateDisplay (currentMessage, ' w' , currentLetter );
73
73
} else if (millis () - upStarted > shortMillis) {
74
- updateDisplay (currentMessage, ' l' );
74
+ updateDisplay (currentMessage, ' l' , currentLetter );
75
75
} else {
76
- updateDisplay (currentMessage, ' ' );
76
+ updateDisplay (currentMessage, ' ' , currentLetter );
77
77
}
78
78
delay (1 );
79
79
}
@@ -99,11 +99,11 @@ void loop() {
99
99
// Wait while button is down.
100
100
while (digitalRead (morseButtonPin) == LOW) {
101
101
if (millis () - pressStarted > longMillis) {
102
- updateDisplay (currentMessage, ' -' );
102
+ updateDisplay (currentMessage, ' -' , currentLetter );
103
103
} else if (millis () - pressStarted > shortMillis) {
104
- updateDisplay (currentMessage, ' .' );
104
+ updateDisplay (currentMessage, ' .' , currentLetter );
105
105
} else {
106
- updateDisplay (currentMessage, ' ' );
106
+ updateDisplay (currentMessage, ' ' , currentLetter );
107
107
}
108
108
delay (1 );
109
109
}
@@ -114,20 +114,25 @@ void loop() {
114
114
}
115
115
}
116
116
117
- void updateDisplay (const String& message, const char & indicator) {
117
+ void updateDisplay (const String& message, const char & indicator, int code ) {
118
118
display.clearDisplay ();
119
119
display.setTextSize (2 );
120
120
display.setTextColor (WHITE);
121
121
display.setCursor (0 ,0 );
122
122
display.println (message);
123
123
124
124
display.setTextSize (1 );
125
+ display.setCursor (0 , display.height () - 10 );
126
+ const int mask = 1 ;
127
+ while (code > 1 ) {
128
+ display.print ((code&mask) ? ' -' : ' .' );
129
+ code = code >> 1 ;
130
+ }
125
131
display.setCursor (display.width () - 10 , display.height () - 10 );
126
132
display.print (indicator);
127
133
display.display ();
128
134
}
129
135
130
-
131
136
void initializeMorseToChar () {
132
137
morseToChar[B101] = ' a' ;
133
138
morseToChar[B11000] = ' b' ;
0 commit comments