I'm sorry if this is a dumb question or too general. I've been researching for hours and can't find an answer for the Mega (only for the Due).
I want to reset the PWM pulse stream on an interrupt triggered by a digital input pin, which is driven by a zero crossing detector connected to a full wave rectified 120Hz 12v signal. In other words, I want to start the PWM timer from zero each time there's a zero crossing, so that it produces a pulse with a certain timing during that period of the AC sine wave.
My idea was to set up a PWM output with a lower frequency, say 30Hz and let it produce the pulse I need, and then detect the zero crossing in an interrupt and write zero to the timer registers (TCNT1 and TCNT2?). Since the period of the PWM is longer than the period of the AC sine wave, I should get the beginning of one PWM cycle (one high-low transition) before the timer gets reset.
It doesn't work though. The PWM simply stops outputting.
If it's important, this is meant to control low voltage lighting. The output pulse from the PWM pin will turn a triac on, which then turns off at the next zero crossing. The PWM output will be inverted (low then high) so that I can delay the low-high transition as desired. I've written a solution that works by polling micros() to produce the pulses I need, but using PWM seemed so much more elegant and would involve simpler code.
If anyone has any ideas or insight I would much appreciate it.
TL;RD - I want to generate a pulse from a PWM pin when an interrupt is fired by a digital input pin. I know this will work with the circuitry I'm using because I can generate the needed pulse by polling micros() and it satisfies what I need. I just want to do it with PWM if possible.