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 library doesn't have emotions so it can't like or dislike anything.
But I'm 100% sure that arrays will work fine.
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
}
}