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.
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
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
}
}