Indexing a constant from an array somehow does not "count" as constant

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.

The index may be constant, but the array you're indexing into isn't.

Thank you for your reply.

Isn't, for example, gLED[0] a constant (4, in this case)?

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};

Thank you!

Though I'm still getting the same error somehow.

Hard to know why without having a complete code to copy / paste into the Arduino and seeing the same error you are.

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)

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

Thank you!

And thank you for the link!

The error is resolved now!