Sicdoc
June 29, 2016, 3:20pm
1
I am building a robot which has two arms with two servos each(one for vertical axis and one for horizontal axis). The code I would like to use to control them is as follows..
void lookLeft() {
for (pos = centrePos; pos <= 150; pos += servoSpeed) {
vertServo.write(pos);
delay(15);
}
}
void lookRight() {
for (pos = centrePos; pos >= 20; pos -= servoSpeed) {
vertServo.write(pos);
delay(15);
}
}
void lookCentreRL() {
if (pos < centrePos + 5) {
for (pos = 20; pos <= centrePos; pos += servoSpeed) {
vertServo.write(pos);
delay(15);
}
}
if (pos > centrePos + 5) {
for (pos = 150; pos >= centrePos; pos -= servoSpeed) {
vertServo.write(pos);
delay(15);
}
}
}
How can I use the functions to control each servo without having to address each servo in the function it's self?
I was thinking something like "this_servo" do "said function".
Thanks for your time.
system
June 29, 2016, 3:26pm
2
The code I would like to use to control them is as follows..
Obviously, you don't, because that code moves one servo at a time.
How can I use the functions to control each servo without having to address each servo in the function it's self?
Pass the Servo instance to the function.
Sicdoc
June 29, 2016, 3:38pm
3
Would you be so kind as to provide an example of how to do this?
Thanks
system
June 29, 2016, 3:45pm
4
Sicdoc:
Would you be so kind as to provide an example of how to do this?
Thanks
void MoveMyServo(Servo &servoToMove)
{
for(byte pos = 0; pos<180; pos+=5)
{
servoToMove.write(pos);
}
}
Servo thisOne;
Servo thatOne;
void setup()
{
// attach the servos to pins
}
void loop()
{
MoveMyServo(thisOne);
delay(1000);
MoveMyServo(thatOne);
delay(1000);
}
Sicdoc
June 29, 2016, 3:48pm
5
Thank you very much. This helps a lot.
void MoveMyServo(Servo &servoToMove)
Paul, is it strictly necessary to pass the instance of the servo by reference ? It appears to work if you pass it by value but I may just have been lucky when I tested it.
system
June 29, 2016, 4:35pm
7
Paul, is it strictly necessary to pass the instance of the servo by reference ?
Probably not, since instances can't really be passed by value. But, I prefer to make it obvious.