Hi
I'm trying to make an array of Servo instances scale depending on the size of an array of structs. The larger code, and this MRE, both compile successfully, and the larger code works just fine, if the value numChan is a constant at compile time. But that leads me to editing a constant, and editing the array, if I change the number of lines, which is suboptimal.
I'd like the value of numChan to be pre-determined by the number of lines present in the array-of-structs, but the compiler gives me an error if the size of the Servo array when compiling isn't a constant. Amusingly, it effectively is, because the compiler will have just finished resolving Turnouts through it's initialization, so array size is known. Funny, hah!
Is this happening because sizeof() has some limitation in this context? Compiler gurus?
As can be seen in the code, it would be even tidier if the Servo reference could be incorporated into the Struct (see commented out member srvo), but that may be a 'bridge too far'.
There is probably a way, but I'm not getting it on this dreary Christmas Eve morning. Any suggestions will be welcome. FYI, the code in setup() can safely be ignored, although some form of allocation for the servo instance within the loop in setup() might be the solution. The use of Turnouts needs to be there in setup() or the compiler happily throws away everything as unused, preventing observation of memory usage changes when the array of structs has a row added or removed. Ultimately, I'd like this array of structs to be drawn from EEPROM, hence my interest in it's size. Code and Error message:
#include <Servo.h>
struct Turnout {
const uint8_t servo_Pin;
const uint8_t button_Pin;
const uint8_t LED_Pin;
uint8_t lastButtonState;
uint8_t turnoutState;
uint8_t servo_Home;
uint8_t servo_Away;
uint32_t lastTimeButtonStateChanged;
//Servo srvo;
};
struct Turnout Turnouts[] = {
{8, A5, 9, 1, 0, 45, 135, 0},
{10, A6, 11, 1, 0, 45, 135, 0},
{12, A7, 14, 1, 0, 45, 135, 0}
};
//now calculate our loop count for for loops
uint8_t numChan = sizeof(Turnouts) / sizeof(Turnouts[0]); //defines how many servos we're driving
//And lastly, create Servo instances; ideally, this would also be folded into the Turnout struct as shown above, but I can live with it separate. This is the line that causes the compile error
Servo Servos[numChan] = {};
void setup() {
for (int chan = 0; chan < numChan; chan++) {
pinMode(Turnouts[chan].LED_Pin, OUTPUT);
Servos[chan].attach(Turnouts[chan].servo_Pin);//attach servo
}
}
void loop() {
}
Arduino: 1.8.15 (Windows 10), Board: "Arduino Nano, ATmega328P"
structtest:25:21: error: array bound is not an integer constant before ']' token
Servo Servos[numChan] = {};
^
C:\Users\CAMSYSCA4\Documents\Arduino\structtest\structtest.ino: In function 'void setup()':
structtest:30:5: error: 'Servos' was not declared in this scope
Servos[chan].attach(Turnouts[chan].servo_Pin);//attach servo ^~~~~~
C:\Users\CAMSYSCA4\Documents\Arduino\structtest\structtest.ino:30:5: note: suggested alternative: 'Servo'
Servos[chan].attach(Turnouts[chan].servo_Pin);//attach servo ^~~~~~ Servo
Multiple libraries were found for "Servo.h"
Used: C:\Users\CAMSYSCA4\Documents\Arduino\libraries\Servo
Not used: C:\Program Files (x86)\Arduino\libraries\Servo
Using library Servo at version 1.2.1 in folder: C:\Users\CAMSYSCA4\Documents\Arduino\libraries\Servo
exit status 1
array bound is not an integer constant before ']' token