I'm doing a project that uses multiple timers with millis() . After much troubleshooting as to why the time was sometimes off I came to the conclusion that a particular function was taking an inordinate amount of time to compute. The purpose of this functions is to check if the readings coming from a sensor have stabilized(allowing some variance) .
So, what is the trouble?
Lots of important code missing from your post.
I note in the function get_array_max_value(), max_value is not initialized to anything. Is that what you intended?
jremington:
So, what is the trouble?
Lots of important code missing from your post.
I note in the function get_array_max_value(), max_value is not initialized to anything. Is that what you intended?
Thanks for pointing that out. Can you tell me what else is missing? I took out some stuff that was irrelevant to the question. I guess I should say that I have this before the main loop:
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?
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().
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 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.
I haven't read everything here but I spotted this line in your code
if(millis() == thermocouple.timeout)
That is not the correct way to use millis() for 2 reasons.
First millis() does not return every successive value so it might never actually equal the timeout value - you should use >=
Second, the code will fail gloriously when millis() rolls over to 0.
Your test should be "millis() - prevMillis >= interval"
You need to post the entire code rather than little pieces that have no context. If it's a large program perhaps you can write a short sketch that illustrates the problem.