I have some servos arranged in an array, so that they can be adressed by number:
// Define the servo objects
Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
// And put them in an array
Servo servo[] = {
servo0,
servo1,
servo2,
servo3,
servo4
};
I have a couple of questions:
How does one properly count the number of servos in this array? Using "sizeof" seems to returns 3 bytes per array item. Am I simply supposed to divide by 3?
Is there a way to reverse the process, i.e. define a constant "SERVO_COUNT 5" (or 6, or whatever) and have them instantiated and arrayed using a loop?
Your code looks wrong to me. You seem to be creating 5 servos, then creating an array of 5 more servos which you are initialising with copies of your original five servos, the result is ten servo objects in memory only five of which you will use. If you do something to servo1 if will not effect servo[1] as they are two different objects.
I got the earlier version of my code from somewhere in this forum, I can't remember where unfortunately. Anyway, your code works perfectly so thanks a lot!
How does one properly count the number of servos in this array? Using "sizeof" seems to returns 3 bytes per array item. Am I simply supposed to divide by 3?
No, you divide sizeof(array) by sizeof(array[0]). That is, you divide the size of the array by the size of one element of the array, to get the number of elements in the array.