VarSpeed Servo

Hello,
I have a code built from the sweep example, and I'm trying to move a number of servos in unison. The motion is working fine, but the delay(); that slows it down in the sweep example isn't doing that in this code. When I use delay(); it actually offsets the motion so that the servos are no longer in unison. Thank you so much and I'll provide as much info as possible to get some advice. Here is the code:

#include <Servo.h> 
 
Servo RR;
Servo RL;
Servo MR;
Servo ML;
Servo FR;
Servo FL;
 
int pos = 90;     
const int fanPin =  1;
void setup() 
{ 
  pinMode(fanPin, OUTPUT);
  
  RR.attach(5);
  RL.attach(9);
  MR.attach(4);
  ML.attach(10);
  FR.attach(3);
  FL.attach(11);
  
  
} 
 
void loop(){
  digitalWrite(fanPin, HIGH);

  
  allLift();
  delay(200);
  
  allLower();
  delay(200);
 }

void allLift(){
  liftleft();
  liftright();
  }

void allLower(){
  lowerleft();
  
  lowerright();
}
  
void liftleft(){
  for(pos = 90; pos >=45; pos -= 1) 
  {                                  
    FL.write(45);
    //delay(15); 
                            
  }} 
void liftright(){ 
  for(pos = 90; pos <=135; pos += 1) 
  {                                   
    FR.write(135);               
    //delay(15);                       
  } 
  }

  

void lowerright(){ 
 for(pos =135; pos >=90; pos -=1); { 
                                
    FL.write(90);              
    //delay(15);
    FR.write(90);
    //delay(15);
    }
} 

void lowerleft(){
  for(pos = 45; pos <=90; pos +=1); { 
                                    
    FL.write(90);              
    //delay(15);
    FR.write(90);
   // delay(15);   
  }
  }

That likely because you can't use delay, which is a blocking function. You will actually need to use something like the "Blink Without Delay" code example to get rid of the blocking issue. You might look into one of the various "multi-servo" control libraries/examples that are out there, which will show you exactly what you need to implement.

TrogX:
it actually offsets the motion so that the servos are no longer in unison.

.
This code

void allLift(){
  liftleft();
  liftright();
  }

starts moving the left servo before the right one although you don't notice because the time for the servos to move is a lot longer than the time for the Arduino to get from one line to the next.

When you add the delay()s to your code it makes this obvious.

What you need to do is interleave the sweep instructions - something like this

void allLift(){
  for(pos = 90; pos >=45; pos -= 1)
  {                                 
    FL.write(pos);
    FR.write(90-pos);
    delay(15);
  }
 }

As a separate issue it may be a good idea to replace your use of delay() with millis() as illustrated in Several Things at a Time

...R

Unfortunately, that didn't solve the issue. I interleaved it like you said, and loaded code with just that function on the two servos to test it. It was off time still. Is there a way I could just call a speed variable and tell it to move at that speed within the functions that I've created above? The motion works there, it's just too fast.

Look into the VarSpeedServo library

TrogX:
Unfortunately, that didn't solve the issue. I interleaved it like you said, and loaded code with just that function on the two servos to test it. It was off time still.

Without seeing your code how can I comment.

And what, exactly, do you mean by "off-time"?

...R

void allLift(){
  for(pos = 90; pos >=45; pos -= 1)
  {                                 
    FL.write(pos);
    FR.write(90-pos);
    delay(15);
  }
 }

What, exactly, do you mean by not seeing the code? This is the code. The code that you offered as an example. Like I said, I "interleaved" it...Just like you said. Meaning I used your code example to test the technique. By 'off time,' I mean FL servo moves, delay, FR servo moves. The same thing I meant when I explained the first time.

TrogX:
What, exactly, do you mean by not seeing the code? This is the code. The code that you offered as an example. Like I said, I "interleaved" it...Just like you said. Meaning I used your code example to test the technique.

Many people "say" they used the same code when they make important variations to it.

By 'off time,' I mean FL servo moves, delay, FR servo moves. The same thing I meant when I explained the first time.

I wonder if the problem has to do with the angles from which the two servos start.

Post your complete program.

...R

That's a good point. I've been sending a code to home all the servos at 180 degrees before attaching the horns to account for that. After trying the interleaving, among other things, I've decided to start looking into using VarSpeedServo. I've been away from that part of the project for a bit to work on printing out a housing for it all, though, so no new code yet.

TrogX:
so no new code yet.

The complete code you used for Reply #6 will be a help.

...R

#include <Servo.h>

Servo FR;
Servo FL;

int pos = 90;

void setup()
{

FR.attach(5);
FL.attach(9);
}

void loop(){
allLift();
}

void allLift(){
for(pos = 90; pos >=45; pos -= 1)
{
FL.write(pos);
FR.write(90-pos);
delay(15);
}
}

Please use the code button </> so your code is easy to copy to a text editor

#include <Servo.h>
 
Servo FR;
Servo FL;

int pos = 90;

void setup()
{
 
  FR.attach(5);
  FL.attach(9);
  
  FL.write(90);   //  <----- new
  FR.write(0);    //  <----- new
}


void loop(){
  allLift();
  }


void allLift(){
  for(pos = 90; pos >=45; pos -= 1)
  {                                 
    FL.write(pos);
    FR.write(90-pos);
    delay(15);
  }
 }

Try adding the two lines I have marked with <---- new to get the servos into the positions that your lift code expects.

...R

Hello, all.
I'm very happy to have found a servo library that has variable speed. A funny thing is happening though. The code that would work with Servo.h, where I call functions in sequence, isn't working here. The slow speed is fantastic, but I'm having difficulty sweeping the servos between positions on a loop like I was with the original library. It stops at the first position in the loop, without going further. Any suggestions?

#include <VarSpeedServo.h>







VarSpeedServo RR;
VarSpeedServo RL;
VarSpeedServo MR;
VarSpeedServo ML;
VarSpeedServo FR;
VarSpeedServo FL;

//int pos =90;
int fanPin = 1;


void setup(){
  pinMode(fanPin, OUTPUT);
  digitalWrite(fanPin, HIGH);
  RR.attach(5);
  RL.attach(9);
  MR.attach(4);
  ML.attach(10);
  FR.attach(3);
  FL.attach(11);
  RR.slowmove(90,255);
  RL.slowmove(90,255);
  MR.slowmove(90,255);
  ML.slowmove(90,255);
  RR.slowmove(90,255);
  RL.slowmove(90,255);
   }

void loop(){
  RR.slowmove(90,255);
  RL.slowmove(90,255);
  MR.slowmove(90,255);
  ML.slowmove(90,255);
  RR.slowmove(90,255);
  RL.slowmove(90,255);
  delay(150);
  yawRight();
  delay(150);
 // yawLeft();
  
  }

 
 
 void yawRight(){
 
  FR.slowmove (65, 15);
  FL.slowmove (65, 15);
  MR.slowmove (65, 15);
  ML.slowmove (65, 15);
  RR.slowmove (65, 15);
  RL.slowmove (65, 15);
  
  
 }
 void yawLeft(){
  FR.slowmove (115, 15);
  FL.slowmove (115, 15);
  MR.slowmove (115, 15);
  ML.slowmove (115, 15);
  RR.slowmove (115, 15);
  RL.slowmove (115, 15);
  }

So I've started a VarSpeedServo code. The speed is working fine, but now I can't seem to get it to actually go through a sequence of functions. It just pauses after the first movement. I've tried various delays and configurations, the latest of which is included here. Is there something you have to do differently for the VarSpeed library? The examples online are all configured differently, it's kind of hard to make sense of.

#include <VarSpeedServo.h>







VarSpeedServo RR;
VarSpeedServo RL;
VarSpeedServo MR;
VarSpeedServo ML;
VarSpeedServo FR;
VarSpeedServo FL;

//int pos =90;
int fanPin = 1;


void setup(){
  pinMode(fanPin, OUTPUT);
  digitalWrite(fanPin, HIGH);
  RR.attach(5);
  RL.attach(9);
  MR.attach(4);
  ML.attach(10);
  FR.attach(3);
  FL.attach(11);
  RR.slowmove(90,255);
  RL.slowmove(90,255);
  MR.slowmove(90,255);
  ML.slowmove(90,255);
  RR.slowmove(90,255);
  RL.slowmove(90,255);
   }

void loop(){
  RR.slowmove(90,255);
  RL.slowmove(90,255);
  MR.slowmove(90,255);
  ML.slowmove(90,255);
  RR.slowmove(90,255);
  RL.slowmove(90,255);
  delay(150);
  yawRight();
  delay(150);
 // yawLeft();
  
  }

 
 
 void yawRight(){
 
  FR.slowmove (65, 15);
  FL.slowmove (65, 15);
  MR.slowmove (65, 15);
  ML.slowmove (65, 15);
  RR.slowmove (65, 15);
  RL.slowmove (65, 15);
  
  
 }
 void yawLeft(){
  FR.slowmove (115, 15);
  FL.slowmove (115, 15);
  MR.slowmove (115, 15);
  ML.slowmove (115, 15);
  RR.slowmove (115, 15);
  RL.slowmove (115, 15);
  }

You are now 'forking' your problem into two different possible solutions.
If you like to do that, that's your choice.

But Robin is still busy helping you with the first apporach.
Wouldn't it be better to keep to that approach until you've exhausted it, and perhaps after that see if you can use that library ?
Right now you have to divide your concentration to those two approaches, which can only lead to mistakes (my opinion).

I have exhausted the first approach. The last suggestion for that also was unsuccessful. I decided to use VarSpeedServo, like another member suggested, and that's where I am. I don't understand what you mean by 'forking.'

And where does one find this library? (The precise version you are using to be exact)

TrogX:
I have exhausted the first approach. The last suggestion for that also was unsuccessful.

That does not mean that it cannot be made successful. As you have not told us anything about your tests it is difficult to help further. Debugging requires patience.

I don't understand what you mean by 'forking.'

Like a fork in a road - dividing your attention (or ours).

...R

Ok.. My logic here is this: With Servo.h, there has so far been quite a lot of troubleshooting involved to slow the servo speed. So, I tested VarSpeedServo.h and found that it is extremely straightforward in terms of positioning the servos as well as controlling the speed. Much more so than Servo.h. But then I found that I was not able to call a sequence of different movements in the same way as Servo.h. I looked through the .cpp file and the .h file to try to find out what I need to do. I obviously only have a rudimentary knowledge of programming (which is why I came here in the first place), so it was a little over my head. I then turned to the same thread where I found the suggestion to use VarSpeedServo.h hoping to find help in understanding it.

Robin2:
As you have not told us anything about your tests

I actually have been.

TrogX:
I actually have been.

I am not familiar with the VarSpeed servo library and if it works for you it is quite sensible to use it.

When I said "you have not told us anything about your tests" I meant that you did not say something like "I tried this code and this happened, and I experimented with it this way, and this other thing happened" etc etc. That is the sort of information that is needed to figure out a solution to the problem.

...R