2323#define WIFI_SSID " SSID"
2424#define WIFI_PASSWORD " PASSWORD"
2525
26+ const int shortMillis = 500 ;
27+ const int longMillis = shortMillis * 3 ;
28+
2629// We define morse . to be binary 0 and - to be binary 1. The longest letter
2730// is 5 morse elements long so we would have a sparse array of 2^5=32. But
2831// we need to add a leading 1 to ensure that .- and ..- are not the same value.
@@ -35,7 +38,7 @@ Adafruit_SSD1306 display(oledResetPin);
3538
3639const int morseButtonPin = 2 ;
3740
38- void updateDisplay (const String& message, const char & indicator);
41+ void updateDisplay (const String& message, const char & indicator, int code );
3942void initializeMorseToChar ();
4043
4144void setup () {
@@ -59,21 +62,18 @@ void setup() {
5962 Serial.println (WiFi.localIP ());
6063}
6164
62- const int shortMillis = 500 ;
63- const int longMillis = shortMillis * 3 ;
64-
6565String currentMessage;
6666int currentLetter;
6767void loop () {
6868 // Wait while button is up.
6969 int upStarted = millis ();
7070 while (digitalRead (morseButtonPin) == HIGH) {
7171 if (millis () - upStarted > longMillis) {
72- updateDisplay (currentMessage, ' w' );
72+ updateDisplay (currentMessage, ' w' , currentLetter );
7373 } else if (millis () - upStarted > shortMillis) {
74- updateDisplay (currentMessage, ' l' );
74+ updateDisplay (currentMessage, ' l' , currentLetter );
7575 } else {
76- updateDisplay (currentMessage, ' ' );
76+ updateDisplay (currentMessage, ' ' , currentLetter );
7777 }
7878 delay (1 );
7979 }
@@ -99,11 +99,11 @@ void loop() {
9999 // Wait while button is down.
100100 while (digitalRead (morseButtonPin) == LOW) {
101101 if (millis () - pressStarted > longMillis) {
102- updateDisplay (currentMessage, ' -' );
102+ updateDisplay (currentMessage, ' -' , currentLetter );
103103 } else if (millis () - pressStarted > shortMillis) {
104- updateDisplay (currentMessage, ' .' );
104+ updateDisplay (currentMessage, ' .' , currentLetter );
105105 } else {
106- updateDisplay (currentMessage, ' ' );
106+ updateDisplay (currentMessage, ' ' , currentLetter );
107107 }
108108 delay (1 );
109109 }
@@ -114,20 +114,25 @@ void loop() {
114114 }
115115}
116116
117- void updateDisplay (const String& message, const char & indicator) {
117+ void updateDisplay (const String& message, const char & indicator, int code ) {
118118 display.clearDisplay ();
119119 display.setTextSize (2 );
120120 display.setTextColor (WHITE);
121121 display.setCursor (0 ,0 );
122122 display.println (message);
123123
124124 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+ }
125131 display.setCursor (display.width () - 10 , display.height () - 10 );
126132 display.print (indicator);
127133 display.display ();
128134}
129135
130-
131136void initializeMorseToChar () {
132137 morseToChar[B101] = ' a' ;
133138 morseToChar[B11000] = ' b' ;
0 commit comments