Resolved: Help with array

Hello Learned Gentlemen

May I get some help on why something like the below won't work and perhaps how to accomplish it the correct way?

When compliling I get the following error:
assignment of read-only location 'analogueRead*'*
```
*const int var[] = {0,1,2,3,4,5,6,7};

void loop () {
for(i = 0; i < 8; i++); {
  var[i] = analogRead(i);
}
}*
* *In composing this topic I actually figured out the error!* *var is set as a const!* *
*int var[] = {0,1,2,3,4,5,6,7};

void loop () {
for(i = 0; i < 8; i++); {
  var[i] = analogRead(i);
}
}*
```
Fixed!

TheDanger:

int var[] = {0,1,2,3,4,5,6,7};

void loop () {
for(i = 0; i < 8; i++); {
 var[i] = analogRead(i);
}
}




Fixed!

Nope.
It only compiles now, it will not work as expected...
Hint: imagine the second time loop is executed, which analog ports will be used?

I was misleaded by the superfluous initialization.

for(i = 0; i < 8; i++); {
  var[i] = analogRead(i);
}

What code will be executed for each step of the for loop ?

I stripped back the code to the part causing the issue. Thus it looks like it doesn't do much

int var[8];

Is that better Whandall?

I would prefer it.

A definition of the variable i would help also.

next time I'll post the code as is! I actually renamed it for simplicity in the forum. You can probably guess why :slight_smile:

  for (int in = 0; in < 8; in++) {
    analogueRead[in] = analogRead(in);        //         <-------This line here!
    batV[in] = (analogueRead[in] * powerV) / 1024;
    Serial.print(" A"), Serial.print((in + 1)), Serial.print(":"), Serial.print(analogueRead[in]);
    Serial.print(" B"), Serial.print((in + 1)), Serial.print(":"), Serial.print(batV[in]);
    Serial.println();

    int val = map(analogueRead[in], 0, 1023, 0, 255);

    if (batV[in] >= 4.00) {
      strip.setPixelColor(in, 0, val, 0);
      //      strip.show();
    }
    if (batV[in] >= 3.00 && batV[in] <= 4.00) {
      strip.setPixelColor(in, 0, 0, val);
      //     strip.show();
    }
    if (batV[in] < 3.00) {
      strip.setPixelColor(in, val, 0, 0);
      //      strip.show();
    }
    strip.show();
    if (in == 7) {
      Serial.println();
    }
  }

Your code could be neater and avoid multiple tests for values if it were something like this (untested)

byte r = 0;
byte g = 0;
byte b = 0;

if (batV[in] >= 4.00)
{
  g = val;
}
else if (batV[in] >= 3.00)
{
  b = val;
}
else
{
  r = val;
}
strip.setPixelColor(in, r, g, b);