combine two servos into one

hello, I'm trying to control two servos with one function rather than writing both out.

I've tried doing:

Servo ser = serX, serY;

serX,serY.write();

as well as a "ser" class but I keep getting error messages. how would I go about this?

laser_turret_with_buttons.ino (407 Bytes)

C and C++ does not allow you to make up your own context as you go along. Please check out the documentation to see if you find such in the documentation.

Paul

Servo ser(serX,serY) {
  serX;
  Servo serY;
}

What exactly are you trying to do here ?
It looks like a function that promises to return a Servo object but never returns anything but what is going on in the body of the function ?
serX;does nothing and
  Servo serY;defines a local servo named serY when you already have a global one of the same name

More details of what you are trying to do please. Are you hoping to write to one servo and have 2 of them move ? If so, then a simple function (or in line code) could do that

"serX" was just a code mistake; I was just throwing together an example.

I want to control two servos with one function, but later use them both individually. That's why I want to combine them both into one identifier

instead of:
serX.write(90);
serY.write(90);

I want:
Servo ser = serX, serY;
ser.write(90);

which should move both servos but apparently it doesn't

Servo ser = serX, serY;

is meaningless

Just write a function that moves both servos

void moveBothServos(byte position)
{
  serX.write(position);
  serY.write(position);
}

Chorles30fps:
instead of:
serX.write(90);
serY.write(90);

I want:
Servo ser = serX, serY;
ser.write(90);

which should move both servos but apparently it doesn't

No it shouldn't, this makes zero sense. I would suggest you take some coding lessons, I'm afraid c++ doesn't allow for making up stuff on the go.

You could define a multiple_servos class which contains multiple servo objects. But considering the complexity of that, stick to UKHeliBob's solution.

You could just connect both to the same output pin.

johnwasser:
You could just connect both to the same output pin.

Chorles30fps:
I want to control two servos with one function, but later use them both individually. That's why I want to combine them both into one identifier

Maybe something like this TwoServos object? This example compiles but I did not test it. It should sweep the X servo, then the Y servo, then both.

#include <Servo.h>
const byte ServoXPin = 8;
const byte ServoYPin = 9;

class TwoServos
{
  public:
    TwoServos(Servo &s1, Servo &s2)
    {
      servo1 = &s1;
      servo2 = &s2;
    };
    
    void write(int pos)
    {
      servo1->write(pos);
      servo2->write(pos);
    };


  private:
    Servo *servo1;
    Servo *servo2;
};


Servo serx, sery;
TwoServos ser(serx, sery);


void setup()
{
  serx.attach(ServoXPin);
  sery.attach(ServoYPin);
}


void loop()
{
  // Sweep x:
  for (int pos = 0; pos <= 180; pos += 1)   // goes from 0 degrees to 180 degrees
  {
    // in steps of 1 degree
    serx.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (int pos = 180; pos >= 0; pos -= 1)   // goes from 180 degrees to 0 degrees
  {
    serx.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }


  // Sweep y:
  for (int pos = 0; pos <= 180; pos += 1)   // goes from 0 degrees to 180 degrees
  {
    // in steps of 1 degree
    sery.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (int pos = 180; pos >= 0; pos -= 1)   // goes from 180 degrees to 0 degrees
  {
    sery.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }


  // Sweep both:
  for (int pos = 0; pos <= 180; pos += 1)   // goes from 0 degrees to 180 degrees
  {
    // in steps of 1 degree
    ser.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (int pos = 180; pos >= 0; pos -= 1)   // goes from 180 degrees to 0 degrees
  {
    ser.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}