Hi! So I'm making a robot that uses multiple servos to move arms and whatnot so I would like to create a function that makes an array of servos and attach them to pins. As well I would need another function to rotate the servos either to a set degree or from a max/ min to the min/ max. I tried but its giving quite a few errors, mainly variables not being declared properly. I tried using the 'public' prefix but that only resulted in another error.
#include <Servo.h> //Includes the servo library
void draw() {
Serial.begin(9600); //Starts console (used for prints)
initServos(10, 10); //runs the function to initialize the servos
}
void loop() {
}
void initServos(int numServos, int servoPin1) { //Initializes the servos
int servoPins[numServos]; //creates an *empty* public array the size of numServos
for (int i = 0; i < numServos; i++) {
servoPins[i] = (servoPin1 + i); //fills the servoPins array with ***consectuive*** pins that the servos will be attached to
}
Servo servo[numServos]; //creates an array of servos
for (int i = 0; i < numServos; i++) {
servo[i].attach(numServos + i); //attaches all the servos to its respected pin
}
}
void rotateServo(int servoNum, int degree) { //rotates the servo to a certain position ***Two Constructors***
servo[sevoNum].write(degree); //rotates the servo
}
void rotateServo(int servoNum, int maxDegree, int minDegree) { //rotates the servo from its max to min or min to max ***Three Constructors***
//if the servo is at it's minimum degree turn it to the max
if (servo[servoNum].read() == minDegree) {
servo[seroNum].write(maxDegree);
//else if the servo is at it's maximum degree turn it to the min
} else if (servo[servoNum].read == maxDegree) {
servo[servoNum.write(minDegree);
}
}
The comments are more for me to remember what I did rather than be useful to you, sorry.