I'm using the servo library within an application. The servo example works fine. However, in my app, I am using Timer0 which seems to interfere. So, the simplified code is :
#include <Servo.h>
...
Servo myservo;
...
void setup() {
...
myservo.attach(9);
...
}
void loop()
...
myservo.write(180);
delay(15000);
}
Everything is fine. However, if I add a handler for timer0:
SIGNAL(TIMER0_COMPA_vect) {
}
The servo "pulses", about every second (going from approx centre position to 40 degrees off centre).
I have looked in servo.cpp and servo.h and can't see any reason why Timer0 should interfere with the code.
Any ideas? Thanks in advance!
How do you think servo gets its timing?
Mark
It doesn't look like the Servo library uses Timer 0.
@OP: Just adding that function does this? Or is there other code as well?
Just adding that function breaks it.
If Timer0 is being used by the servo code, I would expect a compiler error (duplicate definition).
And no, I can't see any reference to Timer 0 in either servo.cpp or servo.h
Sorry for the delay, I've been trying to isolate it down to the most basic sketch I can.
Following sketch moves a servo back and forth, once every two seconds. Uncomment the line :
//GPS.begin(9600);
and the servo behaves strangely pulsing once a second.
Clearly, there is interference, but I can't figure out why the GPS code affects Pin 9. I have also tried digital 12 with the same result.
#include <Servo.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Math.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);
void setup()
{
// UNCOMMENT NEXT LINE TO SEE 1 SEC PULSING
//GPS.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
myservo.write(35);
delay(2000);
myservo.write(135);
delay(2000);
}
Any help would be appreciated.
Code tags would be appreciated.
Code tags? Do you mean comments?
#include <Servo.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Math.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);
void setup()
{
// UNCOMMENT NEXT LINE TO SEE 1 SEC PULSING
//GPS.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
myservo.write(35);
delay(2000);
myservo.write(135);
delay(2000);
}
Thanks for that.
Well, you are now saying that using GPS causes the problem, not Timer 0?
Can you please give a link to the exact Adafruit GPS library you are using? I would like to compile it and observe the results.
Yes. There is a lot going on in the sketch and I thought it was Timer 0. I have no idea why taking that out made any difference.
Anyway, that's why I cut it down to an absolute minimum.
The library can be downloaded from : GitHub - adafruit/Adafruit_GPS: An interrupt-based GPS Arduino library for no-parsing-required use
Thanks for your help.
Which Arduino are you running this on?
Even with the GPS line commented out, on my Uno I see a one-second gap, which is I presume what you are talking about:

Likewise, it's an Uno.
What do I need to get a trace like that? All I can go on at the moment is the servo "pulsing", so it would be good to get a trace where I can see what is going on.
I started a thread here (yes, I know it's sort of a cross-post) but I wanted help from the motor guys.
I cut down your code even more:
#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
myservo.write(35);
}
It's still pulsing. So I just don't know. Maybe you aren't supposed to keep writing to the servo all the time.
What do I need to get a trace like that?
An oscilloscope.
OK, well that was a lengthy red herring. My scope generated gaps that weren't really there.
How do you have this servo wired/powered?
I hooked up a small servo, powered independently, and it seemed to work fine, even with the GPS line active.