#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
// size of each character in pixel: 6 x 8
// each line can display 21 characters at most with text size of 1
#define CHAR_WIDTH 6
#define CHAR_HEIGHT 8
unsigned int textSize = 1;
#define RESET_BUTTON 4
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
unsigned int stepCounts = 0;
bool firstRun = true;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("SSD1306 allocation failed!");
for(;;);
}
pinMode(RESET_BUTTON, INPUT);
oled.clearDisplay();
oled.setTextSize(textSize);
oled.setTextColor(SSD1306_WHITE);
// date
oled.setCursor(0, 0);
oled.print("124-06-2005");
// temperature
char ch = 248; // degree symbol
String temp = "33" + String(ch) + "C";
oled.setCursor(SCREEN_WIDTH - temp.length() * CHAR_WIDTH * textSize, 0);
oled.print(temp);
// total steps
unsigned int steps = 1234;
String totalSteps = "Total steps: " + String(steps);
oled.setCursor((SCREEN_WIDTH - totalSteps.length() * CHAR_WIDTH * textSize) / 2, 10);
oled.print(totalSteps);
// current round
oled.drawFastHLine(0, 24, 128, SSD1306_WHITE);
oled.drawFastVLine(0, 24, SCREEN_HEIGHT - 24, SSD1306_WHITE);
oled.drawFastVLine(127, 24, SCREEN_HEIGHT - 24, SSD1306_WHITE);
oled.display();
delay(2000);
}
void loop() {
int btnResetState = digitalRead(RESET_BUTTON);
if (btnResetState == HIGH) {
String startTime = "Start at 07:10";
oled.setCursor((SCREEN_WIDTH - startTime.length() * CHAR_WIDTH * textSize) / 2, 30);
oled.print(startTime);
String counting = "Counting: " + String(stepCounts);
oled.setCursor((SCREEN_WIDTH - counting.length() * CHAR_WIDTH * textSize) / 2, 50);
oled.print(counting);
stepCounts++;
}
else {
if (!firstRun) {
String endTime = "End at 07:33";
oled.setCursor((SCREEN_WIDTH - endTime.length() * CHAR_WIDTH * textSize) / 2, 40);
oled.print(endTime);
}
firstRun = false;
stepCounts = 0;
}
oled.display();
delay(1000); // this speeds up the simulation
}