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.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
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:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  readings[readIndex] = 5.0 * 100.0 * readings[readIndex]/ 1024.0 ;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // 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

Is that a moving average ? https://www.arduino.cc/en/Tutorial/Smoothing.

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.
The “int readings[numReadings];” will become a two-dimensional array.

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++)
{
  for (int thisReading = 0; thisReading < numReadings; thisReading++) 
  {
    readings[i][thisReading] = 0;
  }
}

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;

// new addition
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()
{
// new addition
for (i=0;i<10;i++)
{
 tempC1 = analogRead(tempPin);           //read the value from the sensor
 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;

// new addition
delay(500);

for (j=0;j<10;j++)
{
 tempC2 = analogRead(tempPin);           //read the value from the sensor
 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

I have made a mistake.
The variable declarations as arrays should all be like this: readings[2][numReadings], readIndex[2], total[2], average[2], inputPins[2].

The average is something extra. That is something for later.
At this moment you should make a small test sketch with arrays to get the analog value of both channels.

It is possible without arrays. You could move some code into functions. For example a function: getAverage(int pin).
But I don’t know if you are familiar with functions and arrays.

All that float and smoothing code seems useless for an LM35 read with default Aref. Leo..