Help optimizing a function

econjack:
I'm probably not seeing something, but if you are calling the functions to get the max or min value in the array and those values are constantly updated, send the most recent value in and have a variable defined in each with the static storage class:

float get_array_max_value(float val){

static long tempMax = -100000F;

if (val >= tempMax)
      tempMax = val;
  return tempMax;
}

float get_array_min_value(float val){
   static long tempMin = 100000F;

if (val <= tempMin )
      tempMin = val;
  return tempMin ;
}




If you are only updating the last value in the array, why read the first 4 values when you know which one is the max value from a previous call? What am I missing?

I wasn't aware of static, but I'm not sure this is what I need. I need to know the max and min values from the last 5 readings, not from the start of the program.

jremington:
What do you mean by "a long time"? It should take no longer than a millisecond or so to determine the min and max values of an array of just a few elements. (5 elements? -- as suggested by the declaration "const int recording_time = 5;" ?)

If you want to tell where your program is stuck, sprinkle some serial.print statements throughout the code, saying "I'm here" and print out the current time using millis().

I agree, although my code is really inefficient it seems weird it could actually cause a delay, but my tests show that is the case. Maybe I'll create a thread later about that problem but for now it would be nice to just improve this part of the code a little.