Use the servo name

Hey guys.
I wonder if you can help me.
My question is whether I can compare the name of a servant. That is, I in the method get a servant and I would like to make an "if" function with the name of that servant. In other words, if the servant's name was X I would do Y.

void read_serial_byte_set_servo(Servo& servo, bool invert, int n) {
      // I want this function



That would be nice but it is not possible.
Here is the source of the Servo library:

I think these are the possibilities:

  • An array with Servo objects and use the index. That is what everyone does.
  • Your own struct with a name and a Servo object.
  • Your own class around the Servo class and add a name to it.

The answer to your question is no, you cannot refer to a servo by name like that

The fact that you want to use a name seems to indicate that you have more than one servo. If that is the case then you can create an array of servos and refer to them by number. If you give the servo numbers variable names you could do something like this (untested)

#include <Servo.h>

enum servoNames {UP, DOWN, LEFT}; //0, 1 , 2

Servo servos[3];
const byte servoPins[] = {10, 11, 12};

void setup()
  for (int s = 0 ; s < 3; s++)
  moveServo(UP, 0);
  moveServo(DOWN, 90);
  moveServo(LEFT, 180);

void loop()

void moveServo(byte servoNum, byte angle)

Thanks for the answers. I'll try another way by creating a variable.

It seems like a slightly odd thing to want to do. The code calling the function knows which servo it is passing, so if you want the behavior to vary, you can make the decision there and call an appropriate function without having to check again in that function which servo was passed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.