#include "DHT.h" //a library used to read the DHT22 sensor.
#include <math.h>
#define sensor 4
DHT dht(sensor, DHT22);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(sensor, INPUT);
}
String temp_calculate(){
float temperature = 0;
float total_temp = 0;
float avg_temp;
float arr[10];
float sum_x;
float deviation;
float max_temp = -40;
float min_temp = 80;
for (int i=0; i<10; i++){
delay(1000);
temperature = dht.readTemperature();
if(!isnan(temperature)){
Serial.println("Temperature: " + String(temperature));
total_temp+= temperature;
if (temperature > max_temp){
max_temp = temperature;
}
if (temperature < min_temp){
min_temp = temperature;
}
arr[i] = temperature;
}
else{
i-=1;
continue;
}
}
avg_temp = total_temp / 10;
for(int i=0; i<10; i++){
float x;
x = (arr[i] - avg_temp) * (arr[i] - avg_temp);
sum_x += x;
}
deviation = sqrt(sum_x/10);
String out = "Minimum temperature: " + String(min_temp) + "\nMaximum temperature: " + String(max_temp) + "\nAverage temperature: " + String(avg_temp) + "\nStandard deviation: " + String(deviation);
return out;
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(temp_calculate());
}