ESP8266Timer and PWM --> wdt reset · Issue #8 · khoih-prog/ESP8266TimerInterrupt (original) (raw)
I try to create a "glow" at certain intervals. Current design is that I loop through my sinus-table with 60 ms and do a analogWrite(), stop at the end of the sinus-loop, then wait some time and then rinse repeat. Looping the 60 ms does not work soooo well, as you already wrote.
To run more smoothly I thought of using the ESP8266Timer to switch the 60 ms-pwm change. For demonstration the restart is done in a simple ticker-loop().
All I get is nothing near a glow but watchdog resets. "analogWrite();" seems to be the offender.
#include "ESP8266TimerInterrupt.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
#endif
const int sinus[] = { 0, 20, 45, 89, 134, 178, 221, 265, 308, 350, 391, 432,
472, 512, 550, 587, 623, 658, 691, 723, 754, 784,
812, 838, 863, 886, 907, 927, 945, 961, 976, 988,
999, 1007, 1014, 1019, 1022, 1023, 1023, 1023
};
const int sinuslen = sizeof(sinus) / sizeof(sinus[0]);
const int sinusschrittzeit = 60;
const long sinusdauer = sinusschrittzeit * sinuslen;
volatile int step = 0;
volatile int direction = 1;
ESP8266Timer ITimer;
void ICACHE_RAM_ATTR TimerHandler(void)
{
analogWrite(LED_BUILTIN, sinus[step]);
step += direction;
if (step < 0) {
step = 0;
direction = 1;
ITimer.disableTimer();
}
if (step > sinuslen - 1) {
step = sinuslen - 1;
direction = -1;
}
}
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
// Interval in microsecs
if (ITimer.attachInterruptInterval(60L * 1000L, TimerHandler))
{
ITimer.disableTimer();
Serial.println("\n\nStarting ITimer OK");
}
else
Serial.println("\n\nCan't set ITimer correctly. Select another freq. or interval");
}
void loop()
{
static unsigned long ticker = 0;
if (millis() - ticker > 6000) {
step = 0;
direction = 1;
ticker = millis();
ITimer.enableTimer();
}
}