"Compilation error: assigning to an array from an initializer list" When changing values of an array

I am writing a script where I initialize an array, and then later enter a new set of values into it.

// Setting the sensor color ranges
  float blueBounds[3][2] = {{110, 195},                           // Blue LED
                        {34, 124},                            // Green LED
                        {0, 103}};                            // Red LED

  float redBounds[3][2] = {{33, 158},                             // Blue LED
                       {38, 126},                             // Green LED
                       {126, 174}};                           // Red LED

  float greenBounds[3][2] = {{64, 143},                           // Blue LED
                         {56, 169},                           // Green LED
                         {13, 99}};                           // Red LED
  float calibrationSample = 20;
  float blueVals[20];
  float greenVals[20];
  float redVals[20];
  float blueSTD;
  float greenSTD;
  float redSTD;
  float blueMin;
  float blueMax;
  float greenMin;
  float greenMax;
  float redMin;
  float redMax;

  // Resetting values
  redSum = 0;
  greenSum = 0;
  blueSum = 0;
  redAve = 0;
  greenAve = 0;
  blueAve = 0;

  // Gather data ******************************************************************************************
  Serial.println("");
  Serial.println("Blue, Green, Red");
  for (int i = 0; i <= calibrationSample; i++) {

    //*** set LED color to blue ***//
    digitalWrite(bluePin, LOW);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);

    delay(delayTime);                                   // wait for photo tranistor to come to steady state

    blueVals[i] = analogRead(colorPin);                 // read phototransistor and store value
    blueSum = blueSum + blueVals[i];                    // Update the sum
    Serial.print(blueVals[i]); Serial.print(", ");      // print stored value.

    //*** set LED color to green ***//
    digitalWrite(bluePin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, HIGH);

    delay(delayTime);                                  // wait for photo tranistor to come to steady state

    greenVals[i] = analogRead(colorPin);               // read phototransistor and store value
    greenSum = greenSum + greenVals[i];                // Update the sum
    Serial.print(greenVals[i]); Serial.print(", ");    // print stored value.

    //*** set LED color to Red ***//
    digitalWrite(bluePin, HIGH);
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, LOW);

    delay(delayTime);                                 // wait for photo tranistor to come to steady state

    redVals[i] = analogRead(colorPin);                // read phototransistor and store value
    redSum = redSum + redVals[i];                     // Update the sum
    Serial.println(redVals[i]);                       // print stored value.
  }
  // turn off LED and wait for another cycle
  digitalWrite(bluePin, HIGH);
  digitalWrite(greenPin, HIGH);
  digitalWrite(redPin, HIGH);

  // Interpret the data *******************************************************************************************
  // Calculate the averages
  blueAve = blueSum / calibrationSample;
  greenAve = greenSum / calibrationSample;
  redAve = redSum / calibrationSample; 

  // Calculate the standard deviations
  for(int i = 0; i <= calibrationSample; i++){
    blueSTD = blueSTD + sq(blueVals[i] - blueAve);
    greenSTD = greenSTD + sq(greenVals[i] - greenAve);
    redSTD = redSTD + sq(redVals[i] - redAve);
  }
  blueSTD = sqrt(blueSTD / calibrationSample);
  greenSTD = sqrt(greenSTD / calibrationSample);
  redSTD = sqrt(redSTD / calibrationSample);

  // Calculate the color ranges
  blueMin = blueAve - 3*blueSTD;
  blueMax = blueAve + 3*blueSTD;
  greenMin = greenAve - 3*greenSTD;
  greenMax = greenAve + 3*greenSTD;
  redMin = redAve - 3*redSTD;
  redMax = redAve + 3*redSTD;

  // Set the calibration data
  switch(Iteration){
    case 0:
      blueBounds = {{blueMin, blueMax},     // Blue LED
                     {greenMin, greenMax},   // Green LED
                     {redMin, redMax}};      // Red LED
    
    case 1:
      greenBounds = {{blueMin, blueMax},     // Blue LED
                     {greenMin, greenMax},   // Green LED
                     {redMin, redMax}};      // Red LED
    
    case 2:
      redBounds = {{blueMin, blueMax},     // Blue LED
                   {greenMin, greenMax},   // Green LED
                   {redMin, redMax}};      // Red LED
  }

The color sensor calibration has a default set of values I am using that I then want to edit if I choose to recalibrate the sensor. The problem flags at the end of the script where I enter new values into these arrays giving the error in the title.
I have done some research and I think the problem is that the compiler thinks I am trying to re-initialize the array, but I guess you can't do that?
How do I resolve this issue?

Assign new values to the array's elements with individual assignment statements.

is there a more concise way to do that then writing 18 lines?

Your guess is correct

You need to initialise the array elements individually. However, there are other problems with the sketch such as these variables not being declared

redSum = 0;
greenSum = 0;
blueSum = 0;
redAve = 0;
greenAve = 0;
blueAve = 0;

I stopped looking but there may be other problems

They're initialized in another part of the project. This is a project with hundreds of lines of code so I didn't post the entire thing.

Not seeing the whole sketch makes giving anything but general help

I know, however, what I needed was an understanding of how an aspect of the language works so I could resolve an error, so general advice.
Two of you have said I need to change each entry in the array individually. This is a lot of manual and hard to follow code to write. Is there not a more concise way to do so? - Other than a for() loop because I am uncertain how I would do this with a for() loop given how each entry is a uniquely created value that can't be iterated though.

memcpy

I was going to suggest using memcpy() but you would need to put the source values into an array first so you might just as well put them in the target array in the first place

You can define your array as class and than define a custom assignment method for it.

By the way, why all this variables are float?

Because using integer division would be a problem. I would like to convert all those values to integers from their floats after the calculations have been done but in my research it seems like that is a lot more trouble than its worth.

As suggested above, you can make your own data structures, e.g.,

struct Bounds {
  uint8_t min;
  uint8_t max;
};

struct Colour {
  Bounds red;
  Bounds green;
  Bounds blue;
};

Usage:

Colour colours[] {{{1, 2}, {2, 3}, {3, 4}}, {{4, 5}, {5, 6}, {6, 7}}};

void setup() {
  Serial.begin(9600);

  Serial.println(colours[0].red.min);
  colours[0] = {{7, 8}, {8, 9}, {9, 10}};
  Serial.println(colours[0].red.min);
}

Output:

1
7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.