Is there a way to make a servo use a different timer

Is there a way to change the servo library to control them on a separate timer from pins 9 and 10?

I assume you are using an Uno.

Not easily. The library is written specifically for a 16 bit timer. This... http://www.cunningturtle.com/servo8bit-library-version-0-6-released/ ...is designed for an 8 bit timer. Theoretically, it could be made to work on timer 2 without too much effort.

The alternative is to get a bigger board; something with multiple 16 bit timers.

Take a look at this library http://forum.arduino.cc/index.php/topic,21975.0.html

Or you can control the servo by using a modified form of "blink without delay" which is how I do it when using motor shields!.

Mark

I am using a library called ServoTimer2 which, as its name suggests, uses Timer2.

I think I got it from here http://arduino.cc/forum/index.php/topic,21975.0.html

...R

So I am trying to get that .cpp file and the .h file from the servotimer2 put in the library. I’m not sure if I am doing it right. I copy/ pasted it into a blank arduino sketch and saved it. I went in and changed them to the appropriate names (ServoTimer.h and ServoTimer.cpp) and put them in their appropriate files (arduino>libraries>ServoTimer2> ). Now I have an example scretch runing one servo on pin 0-

#include <ServoTimer2.h>  // the servo library

// define the pins for the servos
#define rollPin  0

ServoTimer2 servoRoll;    // declare variables for up to eight servos

void setup() {
  servoRoll.attach(rollPin);     // attach a pin to the servos and they will start pulsing
   
}


// this function just increments a value until it reaches a maximum 
int incPulse(int val, int inc){
   if( val + inc  > 2000 )
      return 1000 ;
   else
       return val + inc;  
}

void loop()
{ 
 int val;
  
   val = incPulse( servoRoll.read(), 1);
   servoRoll.write(val);
   
   delay(10);   
}

and now I am getting the Error compiling message:

C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:6:26: error: WConstants.h: No such file or directory
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp: In function ‘void __vector_9()’:
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:36: error: ‘LOW’ was not declared in this scope
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:36: error: ‘digitalWrite’ was not declared in this scope
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:43: error: ‘HIGH’ was not declared in this scope
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:43: error: ‘digitalWrite’ was not declared in this scope
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp: In member function ‘uint8_t ServoTimer2::attach(int)’:
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:65: error: ‘OUTPUT’ was not declared in this scope
C:\Users\Mark\Documents\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:65: error: ‘pinMode’ was not declared in this scope

Maybe that modified blink sketch would be helpful. Not to mention I may learn something (that is why I am doing this after all). Could you show me an example of the blink sketch controlling a servo?

To run a servo you send it a control pulse of between 500 and 2500 micro seconds. Exact timings differ for different servos but 1500 micro seconds normally center the servo. You need to leave a large gap between pulses. And you may need to send the pulse more than once to get the servo into the correct position.

If you do use this method you must ensure that no path through your code takes longer than 500 micro seconds. So no delay() calls at all.

http://www.servocity.com/html/how_do_servos_work_.html

Mark

@bp

In servotimer2.cpp comment out the line relating to wconstants.h

extern "C" {
  // AVR LibC Includes
  #include <inttypes.h>
  #include <avr/interrupt.h>
  //#include "WConstants.h"
}

In servotimer2.h make sure that there is an #include for Arduino.h

#include <inttypes.h>
#include <wiring.h>
#include <Arduino.h>

The library has never been updated since it was implemented for versions of the IDE before 1.0 and the way things are done has changed since then, hence the need for the changes.

I had forgotten about the ServoTimer2 library being out of date. I had found an updated version and I think it came from this website http://sourceforge.net/projects/pgahtow/files/Arduino%20(v1.0)%20libaries/

I usually make a note of this sort of thing, but if I did I can't find it now.

...R

Problem solved!!! Those little modifications to the library code made the difference. Hurdle cleared! Thank you.