diff --git a/temp.c b/temp.c index 8aa3956..e336f3d 100644 --- a/temp.c +++ b/temp.c @@ -115,7 +115,7 @@ struct { needs frequent updates, otherwise 250ms so that we don't waste time on unnecessary reads. */ -#define ANALOG_READ_INTERVAL ((TEMP_EWMA == 1.0) ? 25 : 0) +#define ANALOG_READ_INTERVAL ((TEMP_EWMA == 1.0) ? 25 : 1) /// Set up temp sensors. @@ -305,10 +305,7 @@ void temp_sensor_tick() { temp_sensor_t i = 0; for (; i < NUM_TEMP_SENSORS; i++) { - if (temp_sensors_runtime[i].next_read_time > 1) { - temp_sensors_runtime[i].next_read_time--; - } - else { + if (temp_sensors_runtime[i].next_read_time == 0) { uint16_t temp = 0; //time to deal with this temp sensor switch(temp_sensors[i].temp_type) { @@ -404,7 +401,7 @@ void temp_sensor_tick() { else if (temp_sensors_runtime[i].target_temp < temp) temp--; - temp_sensors_runtime[i].next_read_time = 0; + temp_sensors_runtime[i].next_read_time = 1; break; #endif /* TEMP_DUMMY */ @@ -421,6 +418,13 @@ void temp_sensor_tick() { (EWMA_SCALE-EWMA_ALPHA) * temp_sensors_runtime[i].last_read_temp ) / EWMA_SCALE); } + + /** + Decrement the counter here so that we avoid a off-by-one error. It's + assumed that sensor update code in the switch statement above has set a + non-zero next_read_time (!). + */ + temp_sensors_runtime[i].next_read_time--; } #ifdef NEEDS_START_ADC