Servo library? Servo ignoring my delays, the first time only

Hey all, problem with the Servo commands....

For some reason (It's attaching for the first time?) it ignores the delay in Q the first time, but not any time after....

Thoughts?

 case 'q':case'Q':    servo1.attach(9); servo2.attach(10);servo1.write(20);delay(350);servo2.write(180);Serial.println("Claws enabled");break;
// For some reason it fires BOTH instantly, even with more delays
      
      
      case 'e':case'E':    servo2.write(0);delay(500);servo1.write(180);Serial.println("Close");break;

Why are you attaching the servos here? They should be attached in setup().

The statements all strung together like that are hard to read. They should run down the page, not across.

When you initially attach the servo it will move to whatever position it's currently set to if it isn't already there, usually 90 degrees, but note that you can set the desired position before the attach. Your delay is likely being executed correctly, but you may be mistaking this possible initial move on attach for 'It's ignoring my delay'. Hard to be sure just seeing that fragment of code.

Sounds good.... why would anyone ever detach a servo?

I assumed I'd want to attach/detach them to save power? (So wouldn't want them attached at setup)

Sounds good.... why would anyone ever detach a servo?

I assumed I'd want to attach/detach them to save power?

I think you answered your own question. However, servos can drift when they are not powered. If that matters, you can't detach them.

So then it can be done to save power....

mine would not drift, but shouldn't move when powered up without that delay, because they collide (Layer over each other when closed)

(And if I make them attach on their own line, they jump to 90 degrees for no reason (Never featured in code)

  case 'q':case'Q':    servo1.attach(9); servo2.attach(10);Serial.println("Claws enabled");break; //Jump to 90 and whack into each other,
           case 'r':case'R':    servo1.write(20);delay(350);servo2.write(180);break;
           case 'e':case'E':    servo2.write(0);delay(500);servo1.write(180);break;

And if I make them attach on their own line, they jump to 90 degrees for no reason

There is a reason. You just don't understand it.

You can, and should, call servo.write() before servo.attach(). Then, when you do call servo.attach(), they will go to the position they were told to go to, instead of wherever they feel like.

It's really not that random. Look at the servo.write() code, and see what it does.

Or alternatively, read reply #2 again in light of what you now observe - perhaps it will make more sense now.

oh REPLY # 2, oops, yeah I understand,

Nothing in, Servo.cpp, uint8_t Servo::attach
Tells it to jump to 90 degrees for no reason...

"calling servo.write() before servo.attach()"

Servo write 1, servo attach 1, DELAY (10000000) Servo write 2, servo attach 2

Now this DOES work, but I'd still rather be attaching them separately instead of being forced to move them when I attach,
I insist there's something wrong there... nothing should be making it jump to a position that was never declared,

Thanks Paul!

Insist all you like, but read the source for the library again, and pay attention to the default position

Am I not looking in the right place?

uint8_t Servo::attach(int pin)
{
  return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
}

uint8_t Servo::attach(int pin, int min, int max)
{
  if(this->servoIndex < MAX_SERVOS ) {
    pinMode( pin, OUTPUT) ;                                   // set servo pin to output
    servos[this->servoIndex].Pin.nbr = pin;  
    // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 
    this->min  = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS
    this->max  = (MAX_PULSE_WIDTH - max)/4; 
    // initialize the timer if it has not already been initialized 
    timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
    if(isTimerActive(timer) == false)
      initISR(timer);    
    servos[this->servoIndex].Pin.isActive = true;  // this must be set after the check for isTimerActive
  } 
  return this->servoIndex ;
}

I insist there's something wrong there... nothing should be making it jump to a position that was never declared,

In the servo library code I think the default position is 1500, and the default high and low limits are also in the library code.

Am I not looking in the right place?

Close. Look at what happens when the timer fires, starting with initISR().

yeah I'm sorry :slight_smile:

The librarys are way out of my understanding :frowning:

Thanks for trying though :confused: