Hello,
I'm building a controller for a little freezer box. Basically all it needs to do, is to measure temperature and show it on an oled screen and set the target temp with a rotary encoder. The measuring part is not written yet, because I don't have the informations about the sensor yet. So when you power it up, it shows the temperature. When you click on the rotary encoder it brings you to the settings mode, where you can then set the target temperature by rotating the knob. By clicking again, it submits the target temperature. So far it works BUT when I click the button, then change the value and wait like 2 or 3 seconds the WDT triggers and resets. I have no idea what's causing this I'm more into electronics than into programming so I would appreciate your help a lot! I use the ESPRotary Library and the Adafruit Libs for my SSD1306 oled screen. To make it easier for you to understand the problem, here's a short video:
and here is my code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "ESPRotary.h"
#define ROTARY_PIN1 12
#define ROTARY_PIN2 14
#define CLICKS_PER_STEP 4
#define MIN_POS -20
#define MAX_POS 20
#define START_POS 0
#define INCREMENT 1
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define LOGO_WIDTH 128
#define LOGO_HEIGHT 64
ESPRotary r;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const unsigned char logo_bmp [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x0f, 0xc0, 0x7c, 0x00, 0x00, 0x00,
0x7f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x0f, 0xc0, 0x7c, 0x00, 0x00, 0x00,
0x7f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x0f, 0x80, 0xfc, 0x00, 0x00, 0x00,
0x7f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x1f, 0x80, 0xf8, 0x00, 0x00, 0x00,
0x7f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x1f, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x0f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x1f, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0x81, 0xf0, 0xfe, 0x3f, 0x1f, 0xff, 0x07, 0xff, 0xe0,
0x00, 0x1f, 0xff, 0xff, 0xf8, 0x3f, 0xff, 0x83, 0xf1, 0xfc, 0x3e, 0x1f, 0xff, 0x1f, 0xff, 0xc0,
0x00, 0x1f, 0x07, 0xff, 0xf0, 0x7f, 0xff, 0x83, 0xe3, 0xf8, 0x7e, 0x3f, 0xfe, 0x3f, 0xff, 0xc0,
0x00, 0x3f, 0x0f, 0xff, 0xf0, 0x7f, 0xff, 0x03, 0xe7, 0xf0, 0x7e, 0x3f, 0xfe, 0x3f, 0xff, 0xc0,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, 0x7e, 0x0f, 0xf3, 0xff, 0xbf, 0xfb, 0xfe, 0x7f, 0xfe,
0x07, 0xe1, 0xf8, 0x3f, 0xe3, 0xf8, 0xfe, 0x7c, 0x0f, 0xe3, 0xff, 0x1f, 0xf1, 0xfc, 0x7f, 0xfe,
0x07, 0xe1, 0xf8, 0x7f, 0xc1, 0xf0, 0x7c, 0x7c, 0x0f, 0xc0, 0xfe, 0x0f, 0xe0, 0xf8, 0x1f, 0xfe,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(9600);
delay(50);
pinMode(13, INPUT);
r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
r.setChangedHandler(rotate);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
startup();
display.setTextSize(4);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
showTemp();
if (digitalRead(13) == LOW) {
setTemp();
}
}
void setTemp(void) {
delay(500);
display.setTextSize(4);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.fillRect(0, 60, 128, 10, SSD1306_WHITE);
display.display();
do {
r.loop();
} while (digitalRead(13) != LOW);
delay(500);
}
void showTemp(void) {
display.clearDisplay();
display.setCursor(0, 10);
int tempval = analogRead(A0);
long temp = map (tempval, 0, 1023, -20, 20);
display.print(temp);
display.print((char)247);
display.print("C");
display.display();
}
void startup(void) {
display.clearDisplay();
display.drawBitmap(
(display.width() - LOGO_WIDTH ) / 2,
(display.height() - LOGO_HEIGHT) / 2,
logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
display.display();
delay(1500);
String str = "Modell 650";
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(128-(str.length())/2, 10);
display.print(F("Modell 650"));
display.display();
delay(1500);
}
void rotate(ESPRotary& r) {
int pos = r.getPosition();
Serial.println(pos);
display.clearDisplay();
display.fillRect(0, 60, 128, 10, SSD1306_WHITE);
display.setCursor(0, 10);
display.print(pos);
display.print((char)247);
display.print("C");
display.display();
}