Creating a servo array using functions

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.

  Servo servo[numServos]; //creates an array of servos

That array is local to the function, and will go out of scope when the function ends. Make that array global, if you wish to use the array in another function.

As well I just realized that the 'rotateServo' function with 3 constructors is bad, please ignore it.

PaulS:

  Servo servo[numServos]; //creates an array of servos

That array is local to the function, and will go out of scope when the function ends. Make that array global, if you wish to use the array in another function.

I'm sorry, but how would I do this?

As well I am getting another error where it says 'initServos' not declared. Same code posted in the top

Google "C++ scope" and you will learn all about it, including how to create global variables.

Joshbr32:
I'm sorry, but how would I do this?

Global variables are declared outside any function - usually at the top of an Arduino program before the setup() function.

...R