# Measuring temperature difference from two average temperature

Hello altruists, I’m doing a simple project to sense the temperature of the environment and measure the difference of the temperature. If the temperature difference is high like 10 degree cel then the red led will glow,for 5 degree the yellow and 2 degree the green led. I’m using LM35 temperature sensor and arduino uno. I like to take 10 temperature value and then to take the average of them for better result. I have done for this for one temperature. Which code is…

``````// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.

int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
}
}

void loop() {
// advance to the next position in the array:

// if we're at the end of the array...
// ...wrap around to the beginning:
}

// calculate the average:
// send it to the computer as ASCII digits
Serial.println(average);
delay(500);        // delay in between reads for stability
}
``````

And now I want to do the same to calculate the second temperature value and then measure their difference. How can I do this without repeating this code. I am new to arduino and this is my first. Hope you guys will help me out. TIA

It is possible to make a simple smoothing calculating with a single float number.
Every new value will be added with a certain percentage to the average.
For example with 5%:

``````average = ( 0.95 * average ) + ( 0.05 * newValue );
``````

You could make arrays of the variables.

``````int readings[2][numReadings];      // the readings from the analog input
int readIndex[2] = { 0, 0};              // the index of the current reading
int total[2] = { 0, 0};                  // the running total
int average = { 0, 0};                // the average

int inputPins = { A0, A1};
``````

In the code you could use a for-loop that do both of them.

``````for( int i=0; i<2;i++)
{
{
}
}
``````

It is possible to put the code from the loop() in a function. Then you could still do the for-loop in the loop(), and pass the index as parameter to the function.

A more elegant solution is to use a class. The variables and the functions can be in the class. Then you need to make an array of two of those and you still need the for-loop.

``````//declare variables
float tempC1,tempC2;
int del,tempPin = 0;
int green = 7;
int yellow = 6;
int red = 5;

int temp1_sum = 0;
int temp2_sum = 0;
int avg_temp1;
int avg_temp2;
int i;
int j;
void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}

void loop()
{
for (i=0;i<10;i++)
{
tempC1 = (5.0 * tempC1 * 100.0)/1024.0;  //convert the analog data to temperature
tempC1=(byte)tempC1;   //send the data to the computer
temp1_sum = temp1_sum + tempC1;
}

//delay(500);                           //wait one second before sending new data

avg_temp1 = temp1_sum / 10;

delay(500);

for (j=0;j<10;j++)
{
tempC2 = (5.0 * tempC2 * 100.0)/1024.0;  //convert the analog data to temperature
tempC2=(byte)tempC2;   //send the data to the computer
temp2_sum = temp2_sum + tempC2;
}

delay(500);                           //wait one second before sending new data

avg_temp2 = temp2_sum / 10;

del=abs(avg_temp1- avg_temp2);

Serial.println("Change in temparature= ");
Serial.print(del);
Serial.print("C");
Serial.println();
delay(10);

if((del<2))
{
digitalWrite(green, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
}
else if(del>4, del <7)
{
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
}
else
{
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
}
}
``````

I have written this scratch. But I’m getting wrong values in serial monitor. It shows 3,6,9,12,15,18,21,24,27,30 and then 30 in a consistent manner