1414// limitations under the License.
1515//
1616
17- // FirebaseStream_ESP8266 is a sample that stream bitcoin price from a
18- // public Firebase and optionally display them on a OLED i2c screen.
19-
2017#include < FirebaseArduino.h>
2118#include < ESP8266WiFi.h>
2219#include < Adafruit_GFX.h>
@@ -38,7 +35,7 @@ Adafruit_SSD1306 display(oledResetPin);
3835
3936const int morseButtonPin = 2 ;
4037
41- void updateDisplay (const String& message);
38+ void updateDisplay (const String& message, const char & indicator );
4239void initializeMorseToChar ();
4340
4441void setup () {
@@ -63,13 +60,21 @@ void setup() {
6360}
6461
6562const int shortMillis = 500 ;
63+ const int longMillis = shortMillis * 3 ;
6664
6765String currentMessage;
6866int currentLetter;
6967void loop () {
7068 // Wait while button is up.
7169 int upStarted = millis ();
7270 while (digitalRead (morseButtonPin) == HIGH) {
71+ if (millis () - upStarted > longMillis) {
72+ updateDisplay (currentMessage, ' w' );
73+ } else if (millis () - upStarted > shortMillis) {
74+ updateDisplay (currentMessage, ' l' );
75+ } else {
76+ updateDisplay (currentMessage, ' ' );
77+ }
7378 delay (1 );
7479 }
7580 int upTime = millis () - upStarted;
@@ -83,35 +88,46 @@ void loop() {
8388 Serial.println (" Listening for new letter." );
8489 currentLetter = B1;
8590
91+ // 7 * short timing is a new word.
8692 if (upTime > shortMillis * 7 ) {
8793 currentMessage += " " ;
8894 }
8995 } // else the upTime < shortMillis we attribute to button bounce.
9096 Serial.println (currentMessage);
91- updateDisplay (currentMessage);
9297
9398 int pressStarted = millis ();
9499 // Wait while button is down.
95100 while (digitalRead (morseButtonPin) == LOW) {
101+ if (millis () - pressStarted > longMillis) {
102+ updateDisplay (currentMessage, ' -' );
103+ } else if (millis () - pressStarted > shortMillis) {
104+ updateDisplay (currentMessage, ' .' );
105+ } else {
106+ updateDisplay (currentMessage, ' ' );
107+ }
96108 delay (1 );
97109 }
98110 int pressTime = millis () - pressStarted;
99111 if (pressTime > shortMillis) {
100112 // Shift the binary representation left once then set last bit to 0 or 1.
101- // A long is 3 times a short
102- currentLetter = (currentLetter << 1 ) | (pressTime > shortMillis*3 );
113+ currentLetter = (currentLetter << 1 ) | (pressTime > longMillis);
103114 }
104115}
105116
106- void updateDisplay (const String& message) {
117+ void updateDisplay (const String& message, const char & indicator ) {
107118 display.clearDisplay ();
108119 display.setTextSize (2 );
109120 display.setTextColor (WHITE);
110121 display.setCursor (0 ,0 );
111122 display.println (message);
123+
124+ display.setTextSize (1 );
125+ display.setCursor (display.width () - 10 , display.height () - 10 );
126+ display.print (indicator);
112127 display.display ();
113128}
114129
130+
115131void initializeMorseToChar () {
116132 morseToChar[B101] = ' a' ;
117133 morseToChar[B11000] = ' b' ;
0 commit comments