ArianKS
February 13, 2020, 11:16pm
1
Hi all,
I have a function:
void changeRecordgLED (int pinNumber, int state) {
digitalWrite (pinNumber, state);
switch (pinNumber) {
case gLEDPin [0]:
bitWrite (sensorActuatorStatusCurrent, 5, state);
break;
case gLEDPin [1]:
bitWrite (sensorActuatorStatusCurrent, 6, state);
break;
case gLEDPin [2]:
bitWrite (sensorActuatorStatusCurrent, 7, state);
break;
}
sensorActuatorStatusPrior = sensorActuatorStatusCurrent;
}
and the relevant variable:
int gLEDPin [3] = {4, 9, 5};
And I get this error:
"the value of 'gLEDPin' is not usable in a constant expression"
I'm lost. I'd appreciate any help on this.
gfvalvo
February 13, 2020, 11:30pm
2
The index may be constant, but the array you're indexing into isn't.
ArianKS
February 13, 2020, 11:32pm
3
Thank you for your reply.
Isn't, for example, gLED[0] a constant (4, in this case)?
gfvalvo
February 13, 2020, 11:37pm
4
But the compiler knows you can change that value at any time while the program is running.
This is your promise that you won't / can't do that:
const int gLEDPin [3] = {4, 9, 5};
ArianKS
February 13, 2020, 11:41pm
5
Thank you!
Though I'm still getting the same error somehow.
gfvalvo
February 13, 2020, 11:47pm
6
Hard to know why without having a complete code to copy / paste into the Arduino and seeing the same error you are.
ArianKS
February 13, 2020, 11:52pm
7
Got it. I've attached the full code (it was too long for copy and pasting).
V1.Scratch3.ino (18.3 KB)
variables.h (1.72 KB)
gfvalvo
February 14, 2020, 12:11am
8
OK, go with:
constexpr int gLEDPin [3] = {4, 9, 5};
That tells the compiler it can calculate the values at compile-time.
BTW, 99.9% of that code was unrelated to your problem. That made it clutter that I had to remove because I don't have the required libraries installed.
Next time, post an MCVE
ArianKS
February 14, 2020, 12:18am
9
Thank you!
And thank you for the link!
The error is resolved now!