user29420: This is the code I am currently using, but I want to add an arithmetic
mean to it. My goal is: when the pin A0 reads 5 values, I get the average
on which the fan and led will depend to be turned on and off, instead of
5 separate measurements.
int tempPin = A0;
int fan = 3;
int led = 8;
int temp;
int tempreal;
int tempMin = 20;
int tempMax = 30;

void setup() 
{
  Serial.begin(9600);
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
}

void loop() 
{
  temp = analogRead(tempPin);
  tempreal = temp * 0.48828125;
  delay(1);
  if(tempreal > tempMax)
  {
    digitalWrite(fan,HIGH);
    digitalWrite(led,LOW);
    delay(1);
  } 
  else if (tempreal < tempMin)
  {
    digitalWrite(fan,LOW);
    digitalWrite(led,HIGH);
    delay(1);
  } 
  else if (tempreal >  tempMin && tempreal <  tempMax)   
  {
    digitalWrite(fan,LOW);
    digitalWrite(led,LOW);
    delay(1);
  }
}

Edit by Edgar Bonet – The OP added in a comment to my answer:
“I tried [averaging consecutive readings]. It worked as you would expect
but it still does not solves my problem. My sensor is only getting right
values in the beginning of the scanning and then it varies. That is why
I wanted to make average value but it still does not work. Sadly I think
I wont be able to change sensor, but is there any other way to
camouflage this problem?”
Below is a screenshot showing both correct (✓) and incorrect (✗)
readings:


user30005: A fixed sampling window can be hard to manage, especially if you want the calculation to be recursive, for space and speed considerations. 
You can achieve similar results via exponential smoothing. 
Yn = (1−α) Yn−1 + α Xn
Where Yn is the smoothed outcome, Xn is the current measurement, and α is the weight.
With some thoughts, it can be done with integers and without loss of precision.
edit: i ran a comparison of exponential smoothing vs. (fixed window) moving average. The moving average algorithm was written to be recursive for maximal speed.
the sampled data is a simulated sine wave:
1000 * sin(2pi*n/100) + 100 * noise. n = 0..19999, and noise = -0.5...+0.5.
So snr = 1000 / 50 = 20:1, or about 5 digits. far worse than what you would encounter in a 10-bit adc.

the window I picked is 4.
As you can see, both approaches closely resemble each other. exponential smoothing uses much less ram and is also much faster. older data has lower weight on the output, something generally desirable in most applications.

user33228: See Arduino Tutorials Smoothing 
https://www.arduino.cc/en/tutorial/smoothing
Create an array to hold a series of readings. And a variable to hold the sum.
Incrementally cycle through the array, subtracting the oldest value from the sum and replacing it in the array with the current reading. Divide by the number of readings for the average. 
Remember to allow for a delay in each reading as is appropriate for the sensor. 

user32188: You could use a moving average. It will update at each cycle whenever you take a new reading. Suppose you want to average over N readings:
double moving_average(double old_average, double new_value) {
    double new_average = old_average;
    new_average -= old_average/N;
    new_average += new_value/N;
    return new_average;
}

I edited the function above. I had typed one of arguments incorrectly.

user46506: 
My sensor is only getting right values in the beginning of the scanning and then it varies. That is why I wanted to make average value but it still does not work.

I had a similar problem. It turned out that I was reading writing and transmitting too fast. It means that since it seems that reading and writing can be interrupted during "their job", when you read a variable and in the same time someone is writing on that you're reading garbage. In my opinion this can be fixed by the application of "semaphores" or mutex or reducing the call of the reading. Randomly it would happen again but less frequently.
An idea can be found here
https://forum.arduino.cc/index.php?topic=132813.0 
Best 
Gian Maria

29420: <p>This is the code I am currently using, but I want to add an arithmetic
mean to it. My goal is: when the pin A0 reads 5 values, I get the average
on which the fan and led will depend to be turned on and off, instead of
5 separate measurements.</p>

<pre class="lang-c++ prettyprint-override"><code>int tempPin = A0;
int fan = 3;
int led = 8;
int temp;
int tempreal;
int tempMin = 20;
int tempMax = 30;

void setup() 
{
  Serial.begin(9600);
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
}

void loop() 
{
  temp = analogRead(tempPin);
  tempreal = temp * 0.48828125;
  delay(1);
  if(tempreal &gt; tempMax)
  {
    digitalWrite(fan,HIGH);
    digitalWrite(led,LOW);
    delay(1);
  } 
  else if (tempreal &lt; tempMin)
  {
    digitalWrite(fan,LOW);
    digitalWrite(led,HIGH);
    delay(1);
  } 
  else if (tempreal &gt;  tempMin &amp;&amp; tempreal &lt;  tempMax)   
  {
    digitalWrite(fan,LOW);
    digitalWrite(led,LOW);
    delay(1);
  }
}
</code></pre>

<p><strong>Edit</strong> by Edgar Bonet – The OP added in a comment to my answer:</p>

<p>“I tried [averaging consecutive readings]. It worked as you would expect
but it still does not solves my problem. My sensor is only getting right
values in the beginning of the scanning and then it varies. That is why
I wanted to make average value but it still does not work. Sadly I think
I wont be able to change sensor, but is there any other way to
camouflage this problem?”</p>

<p>Below is a screenshot showing both correct (✓) and incorrect (✗)
readings:</p>

<p><img src="https://i.stack.imgur.com/UmEHM.png" alt="Example readings"></p>
 30005: <p>A fixed sampling window can be hard to manage, especially if you want the calculation to be recursive, for space and speed considerations. </p>

<p>You can achieve similar results via exponential smoothing. </p>

<p>Y<sub>n</sub> = (1−α) Y<sub>n−1</sub> + α X<sub>n</sub></p>

<p>Where Y<sub>n</sub> is the smoothed outcome, X<sub>n</sub> is the current measurement, and α is the weight.</p>

<p>With some thoughts, it can be done with integers and without loss of precision.</p>

<p>edit: i ran a comparison of exponential smoothing vs. (fixed window) moving average. The moving average algorithm was written to be recursive for maximal speed.</p>

<p>the sampled data is a simulated sine wave:</p>

<p>1000 * sin(2pi*n/100) + 100 * noise. n = 0..19999, and noise = -0.5...+0.5.</p>

<p>So snr = 1000 / 50 = 20:1, or about 5 digits. far worse than what you would encounter in a 10-bit adc.
<a href="https://i.stack.imgur.com/6lRO7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6lRO7.png" alt="enter image description here"></a></p>

<p>the window I picked is 4.</p>

<p>As you can see, both approaches closely resemble each other. exponential smoothing uses much less ram and is also much faster. older data has lower weight on the output, something generally desirable in most applications.</p>
 33228: <p>See Arduino Tutorials Smoothing 
<a href="https://www.arduino.cc/en/tutorial/smoothing" rel="nofollow noreferrer">https://www.arduino.cc/en/tutorial/smoothing</a></p>

<p>Create an array to hold a series of readings. And a variable to hold the sum.</p>

<p>Incrementally cycle through the array, subtracting the oldest value from the sum and replacing it in the array with the current reading. Divide by the number of readings for the average. </p>

<p>Remember to allow for a delay in each reading as is appropriate for the sensor. </p>
 32188: <p>You could use a moving average. It will update at each cycle whenever you take a new reading. Suppose you want to average over N readings:</p>

<pre class="lang-c++ prettyprint-override"><code>double moving_average(double old_average, double new_value) {
    double new_average = old_average;
    new_average -= old_average/N;
    new_average += new_value/N;
    return new_average;
}
</code></pre>

<p>I edited the function above. I had typed one of arguments incorrectly.</p>
 46506: <blockquote>
  <p>My sensor is only getting right values in the beginning of the scanning and then it varies. That is why I wanted to make average value but it still does not work.</p>
</blockquote>

<p>I had a similar problem. It turned out that I was reading writing and transmitting too fast. It means that since it seems that reading and writing can be interrupted during "their job", when you read a variable and in the same time someone is writing on that you're reading garbage. In my opinion this can be fixed by the application of "semaphores" or mutex or reducing the call of the reading. Randomly it would happen again but less frequently.</p>

<p>An idea can be found here
<a href="https://forum.arduino.cc/index.php?topic=132813.0" rel="nofollow noreferrer">https://forum.arduino.cc/index.php?topic=132813.0</a> </p>

<p>Best </p>

<p>Gian Maria</p>
