hi,
i want to monitor my application with two different temperature sensors. therefore i have written a sketch. my approach is that i sum up 5 values of each sensor with a gap of 5 seconds. after that i calculate the average of these 5 measurements. if one of the two avrg. values is over a given limit, the code should stop.
what do you think about my code? do you think, there is a simpler solution? btw: i want to avoid the delay() function:-)
static unsigned long lastMicros = 0;
static unsigned long lastMillis = 0;
static long timer_13 = 0;
int temp_counter;
int rawvoltage_temp1;
float temp1;
float temp1_sum;
float temp1_before;
float temp1_avrg;
int rawvoltage_temp2;
float temp2;
float temp2_sum;
float temp2_before;
float temp2_avrg;
#define temp1_pin A0//Temperature Air Refill
#define temp2_pin A1//Temperature M2
#define TIMER_INTERVAL_13 2000//Temp measurement every 2 sec.
void setup() {
timer_13 = TIMER_INTERVAL_13;
Serial.begin(9600);
lastMillis = millis(); // do this last in setup
lastMicros = micros(); // do this last in setup
}
void loop() {
// set millisTick at the top of each loop if and only if millis() has changed
unsigned long deltaMicros = 0; // clear last result
unsigned long thisMicros = micros();
unsigned long deltaMillis = 0; // clear last result
unsigned long thisMillis = millis();
// do this just once to prevent getting different answers from multiple calls to millis()
if (thisMicros != lastMicros) {
// we have ticked over
// calculate how many millis have gone past
deltaMicros = thisMicros-lastMicros; // note this works even if millis() has rolled over back to 0
lastMicros = thisMicros;
}
// do this just once to prevent getting different answers from multiple calls to millis()
if (thisMillis != lastMillis) {
// we have ticked over
// calculate how many millis have gone past
deltaMillis = thisMillis-lastMillis; // note this works even if millis() has rolled over back to 0
lastMillis = thisMillis;
}
if ((temp1_avrg > 23) || (temp2_avrg > 28)){
Serial.println("************Overtemp - Shutdown. Please restart the device! *****************");
}
else{
timer_13 -= deltaMillis;
if (timer_13 <= 0) {
if(temp_counter < 5){
// reset timer since this is a repeating timer
timer_13 += TIMER_INTERVAL_13; // note this prevents the delay accumulating if we miss a mS or two
rawvoltage_temp1= analogRead(temp1_pin);
temp1= ((rawvoltage_temp1/1024.0) * 500) - 273.15;//converts raw voltage to deg. celsius
temp1_sum = temp1 + temp1_before;
temp1_before = temp1_sum;//the sum temperature becomes the new "before"-temperature
Serial.println(temp1_sum);
rawvoltage_temp2= analogRead(temp2_pin);
temp2= ((rawvoltage_temp2/1024.0) * 500) - 273.15;//converts raw voltage to deg. celsius
temp2_sum = temp2 + temp2_before;
temp2_before = temp2_sum;//the sum temperature becomes the new "before"-temperature
Serial.println(temp2_sum);
temp_counter++;
}
if(temp_counter == 5){
temp1_avrg = temp1_sum/5;
temp1_before=0;
Serial.print("*****Average Sensor No. 1: ");Serial.println(temp1_avrg);
temp2_avrg = temp2_sum/5;
temp2_before=0;
Serial.print("*****Average Sensor No. 2: ");Serial.println(temp2_avrg);
temp_counter=0;
}
}
}
}