WDT reset on oled and rotary script

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,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0xff, 0xff, 0xfe, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 
  0x03, 0xe0, 0xf8, 0x3f, 0xf7, 0xfd, 0xff, 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();
}

I can't see in where your code where you kick the dog to keep it alive and I also can't see where you set up the watchdog timer. These things are not automatic.
See:-
https://circuits4you.com/2018/01/24/tutorial-on-arduino-watchdog-timer-setup/

There is another explanation however, I have noticed that sometimes these rotary encoders short out the power and ground briefly when handled, and this short can trigger an reset. The way I have got round this is to but a 100R resistor in series with the positive supply lead into the encoder.

I noticed on the video that you put the encoder down.

1 Like

The watchdog timer on ESP8266 chips is automatic, you don't need to set it up there. Only on Arduino's. And thanks for the tip with the resistor I will try that!

It's not due to power interrupts.. The resistor doesn't help unfortunately it must be something in the code.

But you need to keep resetting it don't you, otherwise what is the point of having one?
If it is truly automatic and resets at the start of the loop function, then your code is delaying that return to the loop function, because it is waiting for the encoder button press, then that is your problem.

1 Like

Ahh I get what you mean yeah that makes sense. And yes it's builtin in the main loop function. Hm how can I fix this do you have any advise for me?

Look at the yield() function, likely the while loop that is waiting for the button press is causing the WDT to time out.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.