#define WINDOW_SIZE 16
float samples[WINDOW_SIZE];
int i=0;
int numErrors = 0;
float upperValue = 26;
float lowerValue = 24;
float tempAvg = 0;
int Pin=17;
double convertReading(int x)
{
float result=0;
result=12*x+18; //As the input value of the sensor is digital (it means the values can be 1 or 0) the result will be 18 for 0 and 30 for 1.
return result;
}
int getSample()
{
int r;
float v;
r = digitalRead(Pin); //A sensor is supposed to be connected to the Pin
v = convertReading(r);
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
if(samples[0]==samples[1])
{
samples[0]=v;
return 0;
}
else
{
samples[0]=v;
return 1;
}
}
float computeAverage()
{
float Average=0; //Inicialization of the variable
for (i=0;i<WINDOW_SIZE;i++)
Average = Average+samples[i];
Average = Average/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 :)" );
}
}
}
}