I am currently trying to program a Tea Timer based on this project: GitHub - lluisgl7/tea-timer
So far I tested the components in single sketches, so I'm sure that the OLED, servo, button and potentiometer are working.
However, if I try to put everything together in one project, my D1 mini is just blinking but not showing anything on the display
#include <U8g2lib.h>
#include <Servo.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
//state identifiers:
#define MENU 0
#define INPROCESS 1
#define DONE 2
const int buttonPin = D8;
const int servoPin = D6;
const int potPin = A0; // selection potentiometer
const int servoHighPosition = 150;
const int servoLowPosition = 70;
const int servoSpeedDelay = 20; // decrease this value to increase the servo speed
unsigned long steepingTime;
unsigned long startTime;
long timeLeft;
volatile int state;
Servo servo;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(0, buttonISR, CHANGE);
servo.attach(servoPin);
state = MENU;
u8g2.begin();
u8g2.enableUTF8Print();
u8g2.setFontDirection(0);
}
void loop() {
switch (state) {
case MENU: moveServoTo(servoHighPosition);
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_fur11_tf);
u8g2.setCursor(1, 15);
u8g2.print("Start");
while (state == MENU) {
steepingTime = 30000 * map(analogRead(potPin), 0, 1023, 1, 20);
u8g2.setCursor(10, 15);
u8g2.print(millisToTime(steepingTime));
u8g2.sendBuffer();
delay(200);
}
break;
case INPROCESS: moveServoTo(servoLowPosition);
startTime = millis();
u8g2.clearBuffer();
u8g2.setCursor(1, 15);
u8g2.print("Stop");
while (state == INPROCESS) {
timeLeft = steepingTime - (millis() - startTime);
if (timeLeft > 0) {
u8g2.setCursor(10, 15);
u8g2.print(millisToTime(timeLeft));
u8g2.sendBuffer();
}
else state = DONE;
delay(500);
}
break;
case DONE: u8g2.clearBuffer();
u8g2.setCursor(1, 15);
u8g2.print("Ready");
u8g2.setCursor(10, 15);
u8g2.sendBuffer();
moveServoTo(servoHighPosition);
u8g2.clearBuffer();
u8g2.setCursor(1, 15);
u8g2.print("Menu");
u8g2.setCursor(10, 15);
u8g2.sendBuffer();
while (state == DONE);
break;
}
}
void buttonISR() {
static unsigned long lastInterruptTime = 0; //used to debounce button input
unsigned long interruptTime = millis();
if (interruptTime - lastInterruptTime > 500) { //long debounce time to allow long presses
switch (state) {
case MENU: state = INPROCESS;
break;
case INPROCESS: state = MENU;
break;
case DONE: state = MENU;
break;
}
}
lastInterruptTime = interruptTime;
}
void moveServoTo(int finalPosition) { //move the servo slowly to the desired position
int currentPosition = servo.read();
if (finalPosition > currentPosition) {
for (int i = currentPosition; i <= finalPosition; i++) {
servo.write(i);
delay(servoSpeedDelay);
}
}
else if (finalPosition < currentPosition) {
for (int i = currentPosition; i >= finalPosition; i--) {
servo.write(i);
delay(servoSpeedDelay);
}
}
}
String millisToTime(long milliseconds) {
unsigned long minutes = (milliseconds / 1000) / 60;
unsigned long seconds = (milliseconds / 1000) % 60;
String minutesString = String(minutes);
String secondsString = String(seconds);
if (minutes < 10) minutesString = "0" + minutesString;
if (seconds < 10) secondsString = "0" + secondsString;
return minutesString + ":" + secondsString;
}