#define WINDOW_SIZE 16
float samples[WINDOW_SIZE]; //The first element of the array is 0
int i=0;
int numErrors = 0;
float upperValue = 25.5;
float lowerValue = 24.5;
float tempAvg = 0;
int Pin=17;
int counter=0;
float sum=0;
float Average=0;
float m=0; //Variable to store the value that dissapier from the array as it is displaced to the right
float convertReading(int x)
{
float result=0;
result=2*x+24; //As the input value of the sensor is digital (it means the values can be 1 or 0) the result will be 24 for 0 and 26 for 1.
return result;
}
int getSample()
{
int r;
float v;
r = digitalRead(Pin); //A sensor is supposed to be connected to the Pin giving high or low values 0 when it is closer to 24 and 1 when it is closer to 26
Serial.println(r);
v = convertReading(r);
//The displacement of the elements is done in the same array for avoiding unnecessary computation resources of having two different arrays
if (counter==0) //In the first iteration it has no sense to displace the elements of the array at they are all ceros
;
else
{
m=samples[WINDOW_SIZE-1]; //The number in the last position of the array is storaged in "m" variable
for(i=WINDOW_SIZE-1;i>0;i--) //Numeration of arrays start in 0 --> size of 16 includes from 0 to 15
samples[i]=samples[i-1]; //The elements of the array are moved one place to the right to let the next sample in the first position of the array
}
counter++;
if(samples[0]==samples[1])//When the displacement of the elements of the array is done, the fist and second position of the array must be the same, if not, an error have occurred
{
samples[0]=v;
return 0;
}
//In practice, the displacement of the elements is allways done correct
else
{
samples[0]=v;
return 1;
}
}
float computeAverage() //there is no sense in doing the average operation considering all the elements of the array when only a single number is added to the array in each iteration
//The previous average value is stored to calculate the new average value of the elements of the array for reducing computation costs
{
if(counter<=WINDOW_SIZE)
{
sum=sum+samples[0];
Average=sum/counter; //While the complete array is not full, the average value is done only with the introduced values
}
else
{
sum=sum-m+samples[0]; //When a number of the array disappears due to the displacement to the right of the array, the number thar disapears is substracted and the new number is added in order to calculate the average
Average=sum/WINDOW_SIZE;
}
return Average;
}
void setup()
{
Serial.begin(115200);
pinMode(Pin, INPUT);
}
void loop()
{
if (getSample() == 1)
{
numErrors++;
if(numErrors > 8)
{
Serial.println("Review the data adquisition function getSample() !!!!");
numErrors = 0; //Initialization of the variable when the error is detected (the error should be fixed when message is shown)
}
}
else
{
tempAvg = computeAverage();
if(tempAvg > upperValue)
{
Serial.println( "Reduce the air conditioning temperature :(" );
}
else
{
if(tempAvg < lowerValue)
{
Serial.println( "Increase the air conditioning temperature :(" );
}
else
{
Serial.println( "All right :)" );
}
}
}
}