Changing Timer1 interval stops timer?

Hi,

I am using the TimerOne class to create PWM control for 8 servos. I'm not using the build in PWM function because that is not suitable for 8 servo's. In stead I have an array with pulse lengths and after each timer interrupt I need another interrupt with the new time as found in the array.

I thought using the Timer1 class woudl do the trick because I could just call Timer1.setPeriod after each interrupt to set the new interval time but whatever I try, as soon as I call Timer1.setPeriod the timer stops. First I thought this might be because I was setting the new time from the callback function and maybe that was a bad thing. But apparently even calling setPeriod from the loop or setup function will stop the timer. I've tried getting the timer running again with Timer1.start (and with calling Timer1.stop before changing the interval time), Timer1.initialize.

Whatever I try I can't keep the timer running when I change the interval time. Can someone help me out please?

Thanks!

Luuk:

The bottom line is that it looks like you can't use "Timer1.setPeriod" to change the interval time because the first thing it does is reset the clock register which, according to the datasheet, stops the timer.

You will have to write your own function, or modify the library, to deal with the clock select bits (the lowest three bits in TCCR1B) without first setting them to 0.

Don

why not just use the MegaServo library: http://www.arduino.cc/playground/Code/MegaServo

it uses timer1 to control up to 12 servos and a standard Arduino board.

Thank you! I will try the MegaServo library. I looks like it does exactly what I need. I will try it later today.

Now I don't have to write this function myself, I would still like to know how these timers work. I've been looking in the TimerOne source to see how it works, but it is not clear to me. Do you know where I can find more info on this or a simple understandable example?

I don't think there is a simple explanation of how to program timers at the register level. The reference document for programming timers is the Atmega168/328 datasheet, but this is written for engineers and is not easy to follow.

The MegaServo Library works great for me! Thanks guys. The only thing I don't understand is that for some reason when attaching a servo to a digital pin I have to lower the pin number with 2. For example, my board uses pins 6 - 13 as servo outputs but I have to tell the program it's 4 - 11. Does anyone know why? It's no problem, it's working so I'm happy. I guess the one who wrote it had reasons for this.

As for the timer, I will read the datasheet when I have some more time to figure out how it works. The technical talk in it does not scare me, it's just a lot to read. That's why I was hoping to find a simple example so I could save myself some time reading that many pages.

But I still think it's strange the TimerOne stops when adjusting the interval. Why would the maker add the setPeriod function if it does not work? That does not make sense to me.

hi Luuk,

Perhaps you are adding two to the pin number in your sketch, could you post the code that exhibits the behaviour you mention.

You don't need to read the entire datasheet, only the sections on the timer functionality you are interested in. But this material is heavy going even for experienced programmers.

I don't think you can say the code for TimerOne does not work just because it does not have functionality that you desire. It can be tricky detecting thresholds when the timer period is changed when running, and the designer may have felt the added complexity to handle this case was not needed by most users of the library.

I was just trying the example file that came with the MegaServo library and noticed the offset in pin numbers. I didn't look much further in it because I didn't have much time. I'll play with it this weekend and will try to finish my receiver.

The offset in that sketch is used to start the 12 pins from pin number 2. The Library does use whatever pin number you give it without offset.