PWM with changing duty cycle

Hi there,

For a project I am working on, I need to be able to change the PWM duty cycle after each pulse.I currently connect a second pin to the PWM output to set as an interrupt, were I then call ANALOG_WRITE to change the duty cycle. Is there a way to implement this solely in firmware? (Trigger a command at each rising edge of the PWM signal).

Thanks.

@tnyqu, using the interrupt method is one way. However, the Tone library uses a hardware timer mode called Output Compare to fire an interrupt every time the PWM output counter reaches the preset count. This interrupt is then used to reload the counter for the next cycle. You could adapt the code for your own application by looking at the Tone code in the open source library.

Sorry to dig this up again, but are those still the best methods of accomplishing this? I have a very similar problem, and would like to be able to do something as simple as using attachSystemInterrupt on the same timer as the pwm, however analogWrite() and attachSystemInterrupt() seem to override each other.

Thanks