Hi folks,
as I’m not so familiar with object coding I’d like to ask questions.
I’d like to use an array to control several servos but want to “automate” the
setup.
And I want to control several servos using an rotary encoder depending on a selection
using momentary switches. As the routine is always the same for each servo, I’d like
to write a function.
So here some pseudo code to show what I think of:
#include <Servo.h>
#include <RotaryEncoder.h>
// servodata contains 1. servoobject number, 2. pin where connected, 3. angle to drive to
int servodata={
1,10,45,
2,11,30,
3,12,61
};
// using a counter to set the servos. Here number should be used to name the object
void setup() {
for (number=1; number < 3; number+=3){
servo-number.attach(number+1); //read as servo1.attach(10);
servo-number.write(servodata[number+2]; // read as servo1.write(45);
}
Serial.println("Setup complete");
delay(500);
}
// here the number of the momentary switch should be used to select the object
void loop() {
momentary=read switch;
switch(momentary){
// here a function should be called where other operation are done (for each servo the same)
case 1 : controlServo( servo-momentary ); break; // read as controlServo(servo1)
case 2 : controlServo( servo-momentary ); break;
default;
}
}
int controlServo( ChosenServo ){
... read rotary...
... ChosenServo.write...
return(1);
}
Some advice on how to combine the name of the servo object with a number and how to
consign a (numbered)object would be appreciated.