Include multiple servos in one array

type or paste code here#include <Servo.h>
int servo_Count = 4;
#define SERVO_01 24 //Job A
#define SERVO_02 25 //Job B
#define SERVO_03 26 //Job C
#define SERVO_04 27 //Job D


char const * servos[] = {"servo01", "servo02", "servo03", "servo04"};
int pin[]             = {SERVO_01, SERVO_02, SERVO_03, SERVO_04};

int pos = 0;    // variable to store the servo position
int servo_Number = 2;
void setup() {
  Serial.begin(9600);
  for (int x = 0; x < servo_Count; x++) {
    Servo servos[x];
    servos[x].attach(pin[x]);
    Serial.print(x);
  }
}

void loop() {

  for (pos = 0; pos <= 75; pos += 25) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servos[servo_Number].write(pos);              // tell servo to go to position in variable 'pos'
    delay(615);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 80; pos >= 0; pos -= 80) { // goes from 180 degrees to 0 degrees
    servos[servo_Number].write(pos);              // tell servo to go to position in variable 'pos'
    delay(300);                       // waits 15ms for the servo to reach the position
  }

}

I am planning on using at least 30 servos for my project. Is there any way to manage them using arrays instead of configuring all of them line by line? I tried different thigs and I always have a similar error : exit status 1
request for member 'write' in 'servos[servo_Number]', which is of non-class type 'const char*'

The above code is supposed to load the servo name and its pin assignment within arrays to later be managed as needed by using a constant value.
Is this even possible? Can anyone, please, provide any suggestion?
Thanks

Servo servo[10];

will create 10 instances of Servos.

That is not the issue I have. I am suspecting servo library doesn't like handling servos within arrays but I am not 10% sure

That is a very low threshold of being sure.
The clue is in your error message

you are setting up an array of "char const *" not the array of servos
@noiasca told you how to create an array of servos but you ignored him. :-1:

servo library doesn't have emotions so it can't like or dislike anything.
But I'm 100% sure that arrays will work fine. :wink:

yes I know I have only connected 2 servos, but I guess you get the point...

/*
   https://forum.arduino.cc/t/include-multiple-servos-in-one-array/1032516

   to be deleted after 2022/10
*/

#include <Servo.h>


constexpr uint8_t openPin = 4;     // GPIO for a movement
constexpr uint8_t closePin = 3;    // GPIO for another movement
constexpr uint8_t servoAPin = 7;   // GPIO for Servo A
constexpr uint8_t servoBPin = 8;   // GPIO for Servo B
constexpr uint8_t servoCPin = 9;   // GPIO for Servo
constexpr uint8_t servoDPin = 11;   // GPIO for Servo

//char const * servos[] = {"servo01", "servo02", "servo03", "servo04"};
uint8_t pin[]  {servoAPin, servoBPin, servoCPin, servoDPin};

constexpr size_t servocount = sizeof(pin) / sizeof(pin[0]);

int servo_Number = 2;

Servo servo[servocount];

void setup() {
  Serial.begin(9600);
  for (int x = 0; x < servocount; x++) {
    servo[x].attach(pin[x]);
    Serial.print(x); Serial.print(' ');
  }
  Serial.println();
}

void loop() {

  for (int pos = 0; pos <= 75; pos += 25) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo[servo_Number].write(pos);              // tell servo to go to position in variable 'pos'
    delay(615);                       // waits 15ms for the servo to reach the position
  }
  for (int pos = 80; pos >= 0; pos -= 80) { // goes from 180 degrees to 0 degrees
    servo[servo_Number].write(pos);              // tell servo to go to position in variable 'pos'
    delay(300);                       // waits 15ms for the servo to reach the position
  }
}

The servo arrays you are creating, are local to the enclosing for loop,
so they will not even survive the next iteration.

Obviously, these objects can not be used in loop.

Thanks man!! It worked like a charm.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.