Hello forum users! After trying endlessly to get this function working correctly I am at the verge of throwing in the towel. This function is performing a color matching rountine. The error seems to be due to the large “const float” arrays. This function is part of a larger program. When I include this function in the program, the program complies but does not function. The arduino mega board just continues to reset. If I reduce the size of the arrays to ~4 entires it works fine. I dont know if the issue is with this function or the rest of the code but I figure I can start with the last thing added, which is this function.
Any general suggestions would great.
void TC_colormatch(int red, int green, int blue){ //pass RGB valves that will be converted to HSL and matched to “estimate”
float h = red; float s = green; float l = blue;
float TC_value;
rgb2hsl(h,s,l); //this replace the current hsl address values with real HSL
Serial.print("hue "); Serial.println(h); Serial.print("sat "); Serial.println(s); Serial.print("lum "); Serial.println(l); Serial.print("\n");
h = h*6.2832; //convert h from 0-1 to 0-2pi (h*2pi)
Serial.println("matching...");
// these estimates are being used to find where the measured color sample(h,s,l) match.
const float h_estimate[] = {0.4233, 0.3781, 0.3305, 0.2805, 0.2282, 0.2011, 0.1729, 0.1132, 0.1007, 0.0879, 0.0748, 0.0654, 0.0558, 0.0462, 0.0414, 0.0366, 0.0317, 0.0052, 6.2611, 6.2333};
const float S_estimate[] = {0.5054, 0.4921, 0.4798, 0.4687, 0.4588, 0.4499, 0.4413, 0.4253, 0.4207, 0.4162, 0.4118, 0.4094, 0.4071, 0.4048, 0.4024, 0.4, 0.3976, 0.3928, 0.3883, 0.3841};
const float L_estimate[] = {0.7294, 0.7137, 0.698, 0.6824, 0.6667, 0.6706, 0.6745, 0.6824, 0.6771, 0.6719, 0.6667, 0.6641, 0.6614, 0.6588, 0.6562, 0.6536, 0.651, 0.6484, 0.6458, 0.6431};
const float TC_test[] = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3, 3.1, 3.2, 3.3, 3.4, 3.5};
const int est_size = sizeof(h_estimate)/sizeof(float); //determines the size of the array
float mag_vector[est_size]; //create an array for the difference vectors
for (int i = 0; i<est_size; i++){
mag_vector[i] = sqrt(S_estimate[i]*S_estimate[i]+s*s - 2*S_estimate[i]*s*cos(h_estimate[i]-h));
}
int n; int min_location; //used to determine the minium location in the array mag_vector
float minval = 3.402823466e+38f; //maxium value of a float;
for(n=0; n<est_size-1; n++){ //step through all values of the array
if(mag_vector[n] < minval){ //check to see if indexed value is less than previous
minval = mag_vector[n]; //if less assign to minval
min_location = n; // cature the location of the minium value
}
}
TC_value = TC_test[min_location]; //get the value of “TC_test” at min_location
Serial.print("Value = ");Serial.println(TC_value);Serial.print("\n\n");
}