Servo order

Hello!
I need help with servo order!

if (v1_dist_cm > 10 && v2_dist_cm <= 10) {
  vaks.write(64,50,true);
  delay(100);
  sedeklis.write(90,50,true);
  delay(20000);
 }
 
 else {
  delay(100);
  vaks.write(155,30,true);
  sedeklis.write(0,30,true);
 }

in "else" I need to close first "sedeklis", but if I change the order of "vaks" and "sedeklis" nothing is happening! Servo with name "vaks" is closing first anyway. Can anyone help me?

With only that snippet of code we can't see where your write() functions come from or what they are doing. Anything we say will just be guessing.

Why not just put a nice long delay() after the write to the one you want to complete its move first?

Steve

Here is code,so how i can control them?

 #include <VarSpeedServo.h>

#include <Ultrasonic.h>


// Pins used by your servos. Adjust according to your connections.
#define pin_servo1 6
#define pin_servo2 7

// Pins used by your ultrasonic sensors. Adjust according to your connections.
#define pin_trigger1 11
#define pin_trigger2 13
#define pin_echo1 10
#define pin_echo2 12

float v1_dist_cm, v1_dist_in, v2_dist_cm, v2_dist_in;

VarSpeedServo vaks;
VarSpeedServo sedeklis;
Ultrasonic ultra1(pin_trigger1, pin_echo1);
Ultrasonic ultra2(pin_trigger2, pin_echo2);

void setup()
{
     vaks.attach(pin_servo1);
     sedeklis.attach(pin_servo2);

     sedeklis.write(0,255,true);
     vaks.write(155,255,true);

}

void loop()
{
  // Read ultrasonic values
   long microsec1 = ultra1.timing();
   long microsec2 = ultra2.timing();

   // Distance in inches and in centimeters
   v1_dist_cm = ultra1.convert(microsec1, Ultrasonic::CM);
   v1_dist_in = ultra1.convert(microsec1, Ultrasonic::IN);
   v2_dist_cm = ultra2.convert(microsec2, Ultrasonic::CM);
   v2_dist_in = ultra2.convert(microsec2, Ultrasonic::IN);

   // According to your readings, you move the servos:



 if (v1_dist_cm > 10 && v2_dist_cm <= 10) {
  vaks.write(64,50,true);
  delay(100);
  sedeklis.write(90,50,true);
  delay(20000);
 }
 
 else {
  delay(100);
  sedeklis.write(0,30,true);
  vaks.write(155,30,true);
 }




   if (v1_dist_cm <= 10 && v2_dist_cm > 10) {
      vaks.write(64,50,true);
      delay(20000);
      
   }


else  { 
    vaks.write(155,30,true);


  }

}

What type of servos are you using? Because "open" and "close" don't really mean anything in servo terms. Do you mean "complete the movement you asked for"?

So what exactly happens when you run the code above with the two write() commands in sequence? You say the one you call second(vaks) runs and finishes before the one that was called first (sedeklis) starts moving?

Are you sure you've actually uploaded the code to the Arduino? I've never seen anything like that happen and I've used VarSpeedServo fairly often.

Steve

Doesn't Varspeedservo have a wait method?
No need for delays.

  myservo1.write(LEF, SPEED1);     
  myservo2.write(LEF, SPEED2);
  myservo1.wait(); // wait for servo 1 to finish
  myservo2.wait();  // wait for servo 2 to finish

vinceherman:
Doesn't Varspeedservo have a wait method?
No need for delays.

That's exactly the same thing that setting "true" in the 3rd parameter in the write() does. I think we're trying to work out why it seems not to work for the OP.

Steve

slipstick:
What type of servos are you using? Because "open" and "close" don't really mean anything in servo terms. Do you mean "complete the movement you asked for"?

So what exactly happens when you run the code above with the two write() commands in sequence? You say the one you call second(vaks) runs and finishes before the one that was called first (sedeklis) starts moving?

Are you sure you've actually uploaded the code to the Arduino? I've never seen anything like that happen and I've used VarSpeedServo fairly often.

Steve

Before, ultrasonic sensor gives data to Arduino. If distance of the object is less then 10, first servo(vaks) starts to move with delay(100), than moves second sensor(sedeklis). After delay(20000) I need to move first "sedeklis" only than "vaks". But here is a problem it is working incorrrectly. "Vaks" is returning into a strat position first, than "sedeklis" is returning to strat. That`s my problem. I need a diffirent order of returning!

What you describe is NOT what your code is doing.

You read the ultrasonic sensors once at the top of loop(). Then if sensor1 distance > 10 AND sensor2 distance is <=10 you move vaks, wait 100ms, then move sedeklis, then wait 20 seconds with delay(20000).

After 20 seconds it continues down the code and doesn't run the "else" part because the "if" is still true.

I suggest you put serial.print()s at the top of loop() and in BOTH "if" and BOTH "else" clauses then you'll see which bits of code actually run.

Steve

slipstick:
What you describe is NOT what your code is doing.

You read the ultrasonic sensors once at the top of loop(). Then if sensor1 distance > 10 AND sensor2 distance is <=10 you move vaks, wait 100ms, then move sedeklis, then wait 20 seconds with delay(20000).

After 20 seconds it continues down the code and doesn't run the "else" part because the "if" is still true.

I suggest you put serial.print()s at the top of loop() and in BOTH "if" and BOTH "else" clauses then you'll see which bits of code actually run.

Steve

If the part "if" would be true, than servo will not close. I just trigger servos with an object, than I remove it from sensor

But you only read the ultrasonic sensors once every 20 seconds or so...that's what the delay(20000) causes.

If you're just going to ignore my advice then you're on your own. Good luck.

Steve