Timers, PWMs and servos

Hi everyone,

So my sketch is now complete (or so I thought).

I sent it to my MEGA 2560 but I’m now experiencing some problems, especially with the display part (to a 20x4 LCD) but the overall structure doesn’t work either…

Now first of all, I did this :

#include <TimerOne.h>

void setup()
{
  Timer1.initialize(500000);
  Timer1.attachInterrupt(display_t_f);
}

display_t_f() being a function that displays a certain number of datas on the LCD screen.
I didn’t want this function to be in the regular loop and waste CPU time so I wanted to just call it every 0,5seconds.

Except that if I put this function, it just doesn’t work, if I remove the attach interrupt and put it in the main void loop() it works correctly (up to the display problems).

I also have servos attached on pin 10 and 11 (using servo library)
And PWM on pin 44 to control a H-bridge.

Is any of it interfering with the classic attachinterrupt function?

Thanks for your help,

Marc

Is any of it interfering with the classic attachinterrupt function?

No.

Servos use timers. PWM uses timers. You are trying to use a timer. There are only a limited number of timers to be used.

When a timer is used for servos, it is not available for PWM or for you to use.

Hi Paul,

So the answer would be yes it is interfering? Right?

Just to make sure I understood correctly what you said...s

So the answer would be yes it is interfering? Right?

Yes, and no. There is "interference" because you are trying to use the same timer for multiple purposes. But, that has nothing to do with attachInterrupt, which is NOT being "interfered" with.

Hi Paul,

What I don't understand is this, in the servo library reference, I read this :

On the Mega, up to 12 servos can be used without interfering with PWM functionality;

So I assumed it was ok...

So I assumed it was ok...

Well, it's OK by me.

Enough of the handwaving. Where is your code, and what IS your problem?

marc426:
So I assumed it was ok…

The problem is you’re trying to use the same timers for multiple things.

I didn’t want this function to be in the regular loop and waste CPU time so I wanted to just call it every 0,5seconds.

That logic is flawed. You certainly don’t need interrupts just to refresh a display twice a second, and your justification of not “wasting” CPU time is completely wrong. You’ve saved a negligible amount of CPU time but made your sketch considerably more complex and squandered one of your hardware timers for no good reason. If you want to refresh the display twice a second, put code in loop() to do that - don’t muck about with timers and interrupts.

If you want to refresh the display twice a second, put code in loop() to do that

Even better is to update the screen as the data to be shown changes. Don't redraw the screen every half second and repaint it exactly the same.

If the idea is to not update the screen more often that twice a second, then never mind. Timers are still not the answer, though. millis() is.