4

I am using the Arduino ServoTimer2 library (because I need to also use the VirtualWire library in the same sketch. I don't know if this problem also exists in the regular Servo library, but I thought it was worth mentioning.

Any time I call servo.attach(pin), the servo moves from whatever its current position is to the exact middle of its range (1500 microseconds). This is a problem because my Arduino needs to attach and detach the servo regularly and resetting back to the center defeats the purpose of my project.

I read that calling servo.write(xxxx) immediately before servo.attach(pin) would cause it to default to xxxx rather than 1500, but that doesn't seem to work for me (nor do I even understand how that would work if the pin isn't attached...). Any ideas?

*

Edit: My Solution

Thanks to djUniversal's answer, I got it working so that the servo doesn't move when attached. In case anyone else is looking to do the same, all I did was go into ServoTimer2.cpp and comment out the first 3 lines of the initISR method. It now looks like this:

static void initISR()
{
//    for(uint8_t i=1; i <= NBR_CHANNELS; i++) {  // channels start from 1
//        writeChan(i, DEFAULT_PULSE_WIDTH);  // store default values
//    }
    servos[FRAME_SYNC_INDEX].counter = FRAME_SYNC_DELAY;   // store the frame sync period

    Channel = 0;  // clear the channel index
    ISRCount = 0;  // clear the value of the ISR counter;

    /* setup for timer 2 */
    TIMSK2 = 0;  // disable interrupts
    TCCR2A = 0;  // normal counting mode
    TCCR2B = _BV(CS21); // set prescaler of 8
    TCNT2 = 0;     // clear the timer2 count
    TIFR2 = _BV(TOV2);  // clear pending interrupts;
    TIMSK2 =  _BV(TOIE2) ; // enable the overflow interrupt

    isStarted = true;  // flag to indicate this initialisation code has been executed
}

As soon as I did that and recompiled, the servo stopped moving immediately when it was attached and now moves only when it is written to.

2
  • I'm glad that worked for you. Remember until you actually write to the motor you will not have any stopping torque and it will wait at any position it has been manually pushed to while it was detached. Nov 2, 2014 at 22:24
  • @djUniversal Thank you, and yes that is exactly what I want it to do.
    – Garrett
    Nov 3, 2014 at 14:30

2 Answers 2

2

The ServoTimer2 defines a starting pulse with:

#define DEFAULT_PULSE_WIDTH  1500

in the .h file. Each time the servo object initialises this is used as the starting point for the channel. The regular Servo library has the same mechanism.

If you wanted to do what you are trying to do you would need to modify the library files to suit your needs as this is not possible otherwise.

Why is it that you need to keep detaching the servos? Is there something you can change there?

2
  • I need to keep detaching the servo because this project is designed to sit for long periods of time without moving and keeping the servo attached and actively locked into a certain position would draw excessive power and decrease its longevity. I will look at changing that value and get back.
    – Garrett
    Nov 2, 2014 at 19:06
  • 2
    Maybe try storing the value in your program and pass it back with the declaration of the object in the .attach command. If you get weird things happening you may also need to modify the lines which reference DEFAULT_PULSE_WIDTH in the .cpp file too. Nov 2, 2014 at 19:35
0

To be complete, for the standard servo library it works to initialize the servo with previous value (from EEPROM) and attach the pin after it:

    // set servo to remembered position!
    servo_9.write(pos);
    // attach pin after position setting, so restarting from previous position
    servo_9.attach(9);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.