Assigning Variables to Servos

Hi,
Is it possible to assign a variable to a servo. So that I could change what that variable meant. So say that my variable is named "ServoVariable" (very original). Could I control "Servo1", by calling on ServoVariable if I had declared that Servo1 is equal to ServoVariable. But than in another part of the program separate from the part where I declared ServoVariable as being Servo1, but rather ServoVariable was now equal to "Servo2".

Is that possible?
And if so, how is it done?

Would this work?

Servo ServoVariable;
Servo Servo1;
Servo Servo2;

if (1 > 2){
 ServoVariable = Servo1;
 ServoVariable.write(90); 
}

if (1 < 2){
 ServoVariable = Servo2;
 ServoVariable.write(120); 
}
1 Like
if (1 > 2){

Doesn't seem very likely, does it?

Can you say what it is you want to do, not how you think it ought to be implemented?

Assign symbols (values) to the meanings "Servo1" and "Servo2" then have a variable that is assigned one of these meanings. You might want a "ServoNone" as well. You might want to look up the "enum" keyword, it does this sort of thing. Old school C programmers like myself use #define.

Here's an example from a project of mine:

#define kUNDEFINED         0x00 /* (kind == kUNDEFINED is invalid) */
#define kSYMBOL            0x01
#define kSMALLINT          0x02
/*                         0x03 is free */
#define kREF               0x04
#define kLIST              0x05
#define kSTRUCT            0x06
#define kCHOICEPOINT       0x07
#define kBYTECODEADDRESS   0x08
#define kFUNCTOR           0x09
#define kLARGEINTNEGATIVE  0x0a
#define kLARGEINTPOSITIVE  0x0b
#define kVECTOR            0x0c
#define kINVALID           0x0d  /* (kind >= kLAST is invalid) */


int is_vector(unsigned int kind)
{
   return kind == kVECTOR;
}

Can you see how to do something similar with your code?

The 1 > 2 was just as an example.
I'm trying to write as little code as possible. So I can write a function, that can be used throughout the whole code and just change what the "ServoVariable" is declared as to change what it does.

Someone who can explain enums pretty well for the beginner is the author of this piece:
http://www.cs.utah.edu/~germain/PPS/Topics/C_Language/enumerated_types.html

Thank you very much bwat!
So in the Arduino language would I create an enumerated type by doing the following:

enum Servo1 
{
  Servo 2;  
  Servo 3;
  Servo 4;
};

Sounds like you need to look at passing parameters to functions

Mark

Lc324:
Thank you very much bwat!
So in the Arduino language would I create an enumerated type by doing the following:

enum Servo1 

{
  Servo Claw;    //Servo object for claw
  Servo Bicep;  //Servo object for the "Bicep"
  Servo Rotate;  //Servo object to rotate entire arm
};

No that's very wrong. I think you need to learn the basics. A good book like The C Programming Language - Wikipedia (2nd Edition) will help you.

What did I do incorrectly?

See reply #4

If you create two instances of servo such as

Servo myServo1;
Servo myServo2;

You could then have a function to move a servo that is called like this

moveServo(myServo1, 120);

and the content of the function would be something like this.

void moveServo(Servo servoName, byte angle) {
    servoName.write(angle);
}

You could also create an array of Servo instances instead of giving each a separate name

Servo myServoArray[2];

...R

Thank you very much Robin2!
This is my code (I'm aware it's not valid):

#include<Servo.h>
Servo Servo1;
Servo Servo2;

int val;

void setup(){
  Servo1.attach(2);
  Servo2.attach(4);
  Serial.begin (9600); 
  val = Serial.read();
}

void moveServo (Servo servoName, byte angle){
    servoName.write(angle); 
}

void test () {     
 switch(val) {     
          case 'a':
           moveServo( , 10);
           break;
          case 'b':
           moveServo( , 20);
           break;
          case 'c':
           moveServo( , 30);
           break;
          }
}


void loop(){
  if(val == 10){
   moveServo(Servo1, );
   test(); 
  }
  if(val == 20){
   moveServo(Servo2, );
   test(); 
  }
}

The problem I having is that I'm only wanting to define the angle in the switch case. And then the Servo used in the if statements. So the switch case angles stay the same, but the servo used changes.

The spaces in between the "moveServo( , 10);" is me leaving the servo part blank, for it to be defined later. And the "moveServo(Servo2, );" is leaving for the next statement "test();" to define the angle.

How can you achieve this result?

The problem I having is that I'm only wanting to define the angle in the switch case.

The problem is that you have unrealistic expectations. If you want to define a "current servo", and have an moveTheCurrentServo() calls move the current servo, you need to approach the problem completely differently.

You've been given enough hints to do that. There is nothing magic about a Servo instance that makes it different from an int.

Some snippets to give you some ideas.

Servo currentServo;

currentServo = Servo1; // Servo1 is a really stupid name.

moveCurrentServo(27);

currentServo = Servo2;

moveCurrentServo(45);

void moveCurrentServo(int pos)
{
   currentServo.write(pos);
}

You could have a pointer do that.

Servo s1;
Servo s2;
Servo* p;
s1.attach(PIN1); // some arbitrary pin number
s2.attach(PIN2); // some arbitrary pin number

p = &s1; // now p points to s1
p->write(180); // write 180 to s1

p = &s2; // now p points to s2
p->write(0); // write 0 to s2

You could have a reference do that, or an array index, or . . .

Which is why it is important to know what the OP wants to do, not how they think it should be done.

Lc324:
The problem I having is that I'm only wanting to define the angle in the switch case. And then the Servo used in the if statements. So the switch case angles stay the same, but the servo used changes.

If the servos are defined as an array (I showed how earlier) then you can use the index to the array to choose which one to use. For example

moveServo(1, 30);
void moveServo(byte servoID, byte angle) {
    myservo[servoID].write(angle);
}

...R

Not sure as to what the maim question is, but below is some servo test code for multiple servos.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}