These are chat archives for esp8266/Arduino
I'm trying to create a AC mains dimmer that emulates a hue bridge for Alexa.
Hardware-wise, I have a zero-crossing interrupt at one GPIO that edge-interrupts at very half-wave, i.e. 100Hz in my case. The timer should then switch on the load after some delay which depends on the current bightness level. (0-10ms in e.g. 64 steps)
I've tried two approaches:
1) using NMI timer in single shot mode; upon a zero-crossing interrupt, first I'm setting timer by loading T1L directly, and turning the load off. The timer callback turns on the load after a time that depends on the desired brightness level.
2) using a free-running NMI timer in loop mode, with a resolution of n dimmer-levels (i.e. period time / levels, e.g. 64), and a volatile loop counter that increments every timer tick and is reset upon every zero crossing. Now, in the timer-isr, if the loop counter > desired dimmer level, the load is turned on.
Both approaches work but experience the same ~1-2ms jitter. Using the timer alone seems to work fine in either mode and is accurate, even with the web server active.
I've experimented with loading the timer in various places, tried to disable interrupts in "right" place and using compensation values for the timer to acciount for cpu cycles consumed, but I can't get it to work w/o jitter.