Servo name in variable - how to use it?

Hi,

how can I address a servo which objects name I have in a variable?

What works:
MyServo.write(90);

What doesn't work:
String MyVar=MyServo;
[MyServo].write(90); <-- That's what I need the syntax/solution.

The error I get with the code above:
expected primary-expression before '[' token

I need it to get the servo name from Serial input.

Thanks a lot.
Robert

The runtime code has no idea what you "called" anything in the source, and knows only its address.
What are you trying to do?

AWOL:
What are you trying to do?

I want to submit Servo name and angle in degree via serial com to the arduino and have the servo set accordingly.

Like:
serial submit = MyServo1;90
Arduino= MyServo1.write(90);

serial submit = MyServo2;180
Arduino= MyServo2.write(180);

My code:
inputString = "MyServo1;90" <-- comes from a different function

int semicolon=inputString.indexOf(';');
String IServo=inputString.substring(1,semicolon+1);
int IPos=convertStringtoInt(inputString.substring(semicolon+1));

I can seperate the serial input so I have the servo name in one (IServo) variable and the angle in another (IPos).
Here I am stuck.

Robert

You either need to explicitly associate a string value (please, not a String) with each servo, or simplify, and just have an array of servo objects, and then simply provide the index of the servo you want to operate on.

AWOL:
You either need to explicitly associate a string value (please, not a String) with each servo

You mean using a case statement with cases for each possible input and than hard code the servo object name? That's what I try to prevent. I want to minimize the amount of code.
Or do I get this wrong?

AWOL:
and just have an array of servo objects, and then simply provide the index of the servo you want to operate on.

Do you have a code example of how this works?

Thanks
Robert

robvoi:

AWOL:
You either need to explicitly associate a string value (please, not a String) with each servo

You mean using a case statement with cases for each possible input and than hard code the servo object name? That's what I try to prevent. I want to minimize the amount of code.
Or do I get this wrong?

You cannot use a "case" for this, as "case" only works on numbers. Use "C strings" (not the String class) and the strcmp() function.

Or just label your servos A and B. Why do you need eight characters?

Or use an array of servo objects and then you just need to pass the index value of the one you wish to move.

I use servo names like L1U and L1L for "leg 1 upper servo" and "leg 1 lower servo". Just to have the code more readable than having eight servos just numbered. MyServo1 was just for the example code
Does someone have an example for the array of servos approach?

Otherwise I might just use if statements for this function. Not as nice as a one liner, which I hoped for. But I need it only for one function. For the rest my human readable servonames do fine.
I just wonder if there is no way to directly use the servo name - which I have in the variable.

Robert

I just wonder if there is no way to directly use the servo name - which I have in the variable.

Where in the variable?

An array of servo objects is declared and handled just like an array of any other datatype.

Servo test code that parses a received string for two servos. You should be able to modify the code to take a sent sting with a servo identifier and position (like L10900), parse the string, and position the desired servo.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}