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);
}
}
2 Answers 2
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?)2019年06月18日 07:22:43 +00:00Commented Jun 18, 2019 at 7:22
-
1As 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– Edgar Bonet2019年06月18日 08:44:50 +00:00Commented Jun 18, 2019 at 8:44 -
Yeah, that was an editing 'oops'. Originally I'd put
toMove()
belowloop()
. Clearly, the declaration is no longer needed.JRobert– JRobert2019年06月18日 11:59:04 +00:00Commented 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!Fabrízio Newton– Fabrízio Newton2019年06月19日 01:28:28 +00:00Commented 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).JRobert– JRobert2019年06月19日 21:43:38 +00:00Commented Jun 19, 2019 at 21:43
var i = 0;
looks like JavaScript, not "Arduino code".void toMove(Servo& obj,int inPosition, int forPosition){
inPosition
toforPosition
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 theforPosition
to the servo.