Function Call !!

Hi all ...
wish all of you doing well, and appreciate the help and support in advance ...
well; i am trying to make 3 servos move is a sequence.
therfor i thought if i make a function and use it is easier than making every time a servo check where is the location now and move smooth step-by-step in a certain speed to acheive the target degree.
i dont know where is my mistake ...
please help ...

#include <Servo.h>

int pos = 0;   

Servo firstservo; 
Servo secondservo; 
Servo thirdservo;

void setup() {
  firstservo.attach(7); 
  secondservo.attach(6);
  thirdservo.attach(5);
}

void stepservo(Servo& ServoName, int Moveto, int myspeed){
  int StartFrom = ServoName.read();
  if(Moveto >= StartFrom){
    for (int pos = StartFrom; pos <= Moveto; pos += 1){
      ServoName.write(pos);
      delay(myspeed);            
      }
    }else{
    for (int pos = StartFrom; pos >= Moveto; pos -= 1){
      ServoName.write(pos);
      delay(myspeed);            
      }                                 
    }
}

void loop() {

  stepservo(thirdservo, 90, 15);
  delay(1000);
  stepservo(firstservo, 90, 15);
  delay(1000);
  stepservo(secondservo, 90, 15);

}

:frowning: i dont know where is my mistake ...

i dont know where is my mistake ...

I do. You didn't tell us what the code actually does, or if it even compiles. You didn't tell us what you expect the code to do. You didn't give us any clues needed to help you.

Hello ab4net

And what is the problem?

Regards,
bidouillelec

Oh ...
i wanted to move the servos to a certain angle.
no errors when i compile.
example :
when i want to move serve 1 to the angle : 90
i want to use the function as following :
stepservo(firstservo, 90, 15);
where the 15 is 15ms delay in each step

whats happening now is NOTHING
it dosent move the servos
while i tried a direct write(); and every thing connected well when it comes to connections.

it dosent move the servos

In the code, once the servos have been moved to a position they are not moved again.

How are the servos powered, bearing in mind that they use quite a large current and the Arduino can only supply a limited amount.

What is the value that you read from the servo, in the function? It makes NO sense to try to use the value in a variable when you have no clue what that value is.

The read() method only returns the last value written to the servo. The first time you call the function, you have NOT written anything to the servo, so it does NOT make sense to ask the servo where it is.

great ... thanks ...
i asked the arduino to put out to the serial screen
and got that already in the needed angle :slight_smile:
i moved a deferent values and all working fine

perfect ....

thanks alot guys ...

Hello ab4net

I tried your sketch with two servos and it runs fine here?

Regards,
bidouilleelec