0

I have a project with 9 servo, I want to make a function to control each separate. Only that he is not accepting the servo as a parameter. Can someone tell me if it is possible to pass the servant as a parameter, and how? Thank you!! Example:

Servo s1,s2,s3;

void toMove(Servo obj,int inPosition, int forPosition){
var i = 0;
  for(i = inPosition; i<forPosition; i++){
    obj.write(i);
  }
}

void loop(){
  if(true){
     toMove(s1,0,180);
     toMove(s2,0,180);
  }
}
  • 1
    Where is the rest of your sketch? The servo include is missing, setup is missing, and var i = 0; looks like JavaScript, not "Arduino code". – VE7JRO Jun 17 '19 at 23:54
  • 2
    try void toMove(Servo& obj,int inPosition, int forPosition){ – Juraj Jun 18 '19 at 5:15
  • It doesn't make sense to write all values from inPosition to forPosition in increments to the servo. Since the period of the pulsed signal to the servo is much longer (multiple orders of magnitude) than the time the for-loop needs, the servo will (almost) never see any intermediate values. You can just write the forPosition to the servo. – the busybee Nov 15 '19 at 19:47
1

Yes, it is possible, and you've done it almost right. I made a minor correction in the toMove() function ("int" instead of "var"), and supplied the setup() function which you didn't show. It compiles without error. I didn't check it for correct operation as it looks like you had reduced your code to the minimum example necessary to demonstrate your question -- and thanks for that! Here is what I compiled:

#include <Servo.h>

#define SERVO1      9
#define SERVO2      10


/*****< External and Global Declarations >*****/
Servo s1, s2;
void toMove(Servo obj, int inPosition, int forPosition);


/*****< Local Function Prototypes >*****/


void toMove(Servo obj,int inPosition, int forPosition){
int i = 0;
  for(i = inPosition; i<forPosition; i++){
    obj.write(i);
  }
}

void setup()
{
    s1.attach(SERVO1);
    s2.attach(SERVO2);
}


void loop() {
   toMove(s1, 0, 180);
   toMove(s2, 0, 90);
}
  • a function prototype a line before the function definition? (and a line before the comment about it?) – Juraj Jun 18 '19 at 7:22
  • 1
    As noted by Juraj in a comment to the original question, it would be better to pass a reference to the servo, rather than copying the whole object: void toMove(Servo &obj, int inPosition, int forPosition);. – Edgar Bonet Jun 18 '19 at 8:44
  • Yeah, that was an editing 'oops'. Originally I'd put toMove() below loop(). Clearly, the declaration is no longer needed. – JRobert Jun 18 '19 at 11:59
  • I am simulating in Tinkercad, I copied its code and set up the project, with the arduino and the servo, it continues giving error, the Servo is not identified as type of variable for the function. Would you like to help me, the link to the project is: tinkercad.com/things/2w0xPqGEqI0-smooth-vihelmo/… Anyway thank you! – Fabrízio Newton Jun 19 '19 at 1:28
  • Have you tried @juraj & @ EdgarBonet's suggestion of passing by reference ( '&obj' instead of 'obj' in the function parameter list)? The compiler will accept it either way (at a 58 byte code penalty for passing the whole object), but perhaps Tinkercad won't accept passing the entire object (I couldn't try it - the link fails for me). – JRobert Jun 19 '19 at 21:43
0

You have not use attach() function:

 s1.attach(pin_1);
 s2.attach(pin_2);

See how to use servo motor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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