PWM possible in ISR?

Hi, I've been working on a project where I need the pwm to vary after 1 second intervals depending on the serial input coming in. I don't really like using delays all over the code and was thinking of using timer interrupts.

So my question is : If I raise a timer interrupt using timer 1 on the 328p or 32u4, can I vary the PWM in the ISR using timer 2? Alternatively, if I'm missing out a really easy solution on how to change pwm values at one second intervals without using the delay, please let me know. The values ramp up at one second intervals for a certain number of seconds (varies according to mode) and then ramps down over a few more seconds (again variable) sort of trying to create a pulsating effect while the mode or set points can change at any time. So it can't be in something like a simple for loop. Based on serial inputs, the pwm set points, and other variables will be varying.

I hope I've done a half decent job of trying to explain the issue I'm facing. The pwm frequency doesn't matter as long as it is under 10k.

Use millis() instead of delay for timing.

For example, study the "blink without delay" program that comes with the Arduino IDE. Be sure that all time-related variables are unsigned long. Tutorial here.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

Thanks guys, this is precisely what I was looking for.

Although now I'm curious as to whether you can, in reality use PWM using timer 2 in an ISR called by timer 1.

tehemton: Thanks guys, this is precisely what I was looking for.

Although now I'm curious as to whether you can, in reality use PWM using timer 2 in an ISR called by timer 1.

Why wouldn't you be able to? Is something special about an ISR that you can't write to a timer register?

use PWM using timer 2 in an ISR called by timer 1

You must be confused about something!

Once set up, the timers generate the commanded PWM signals continuously, and need no further input. You can [u]change[/u] the timer registers in control of the output at any time, from any part of the program.