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);
 }
}
asked Jun 17, 2019 at 23:36
3
  • 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". Commented Jun 17, 2019 at 23:54
  • 2
    try void toMove(Servo& obj,int inPosition, int forPosition){ Commented Jun 18, 2019 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. Commented Nov 15, 2019 at 19:47

2 Answers 2

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);
}
answered Jun 18, 2019 at 1:15
5
  • a function prototype a line before the function definition? (and a line before the comment about it?) Commented Jun 18, 2019 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);. Commented Jun 18, 2019 at 8:44
  • Yeah, that was an editing 'oops'. Originally I'd put toMove() below loop(). Clearly, the declaration is no longer needed. Commented Jun 18, 2019 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! Commented Jun 19, 2019 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). Commented Jun 19, 2019 at 21:43
0

You have not use attach() function:

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

See how to use servo motor

answered Nov 15, 2019 at 4:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.