Smoothing 4 Temperature Probes

Hello! I’m new to the forums so if I posted this under the wrong thread, please bear with me. I am also fairly new to Arduino and writing code (again, please bear with me).

I am currently in a high school senior level course called Engineering Design and Development. This is NOT a programming course, however the project that I am doing in this class requires me to write code. With a very limited understanding of Arduino this is proving to be very difficult.

So, here is what we are trying to do:
My group is developing a program that averages the input from four 100k potentiometers using a smoothing code.

The Problem? The reading is displayed as 1023.00 as if it is maxed out and adjusting the potentiometers doesn’t help. Do we have to use resistors to reduce the output of each potentiometer, or does the problem lie in our code?

Here is my code:

#define NUMSENSORS 4
#define NUMREADINGS 10

int readings[NUMSENSORS][NUMREADINGS];      // the readings from the analog input
int index = 0;                            // the index of the current reading
float total = 0;                            // the running total
float average[NUMSENSORS];                      // the average
float totalAverage;
int i,j;;

int inputPin[NUMSENSORS] = {0,1,2,3};

void setup()
{
  Serial.begin(9600);          // initialize serial communication with computer
  for(i=0;i<NUMSENSORS;i++)
  {
    for(j=0;j<NUMREADINGS; j++)
    {
     readings[i][j]= 0;     // Clear the data
    }
  }
}

void loop()
{
  for(i=0; i<NUMSENSORS; i++)        // This handles the data for each sensor
  {
    for(j=0; j<NUMREADINGS - 1; j++)
    {
      readings[i][j+1] = readings[i][j]; // Shift to make room for the new reading
    }
    readings[i][0] = analogRead(inputPin[i]); // get the latest value
  }
  
   for(i=0; i<NUMSENSORS; i++)    // Average each sensor
   {
     total = 0;                    // Reset the total for each sensor
     for(j=0; j<NUMREADINGS; j++)
       total += readings[i][j];
     average[i] = total / NUMREADINGS;
   
   }
 
  total = 0;                    // Reset total again for use with totalAverage
 
  for(i=0; i<NUMSENSORS; i++)  // Average all sensors together
  {
    total += average[i];
  }
  
  totalAverage = total / NUMSENSORS;
  Serial.println(totalAverage);       // send it to the computer (as ASCII digits)
}

Any information that you can provide would be greatly appreciated, Thanks!

The easiest thing to do is make sure you are getting data correctly. Then you can go from there.

void loop(){
  for(i=0; i<NUMSENSORS; i++)
  {
    Serial.println(analogRead(inputPin[i]));
  }
  delay(100);
}

WHE, Not to do your homework for you, but it sounds like you might have your pots hooked up slightly wrong. Check out this example from Arduino.cc learning pages http://arduino.cc/en/Tutorial/AnalogReadSerial. TomJ

Hey WHE, I think what you are looking for is much simpler than you think it is. I believe you are not trying to smooth but rather average the 4 values. It's a very easy process and took me about 5 minutes to come up with it. Enjoy!

void setup() 
{
  // initialize serial communication with computer
  Serial.begin(9600);         
}

void loop() 
{
  //define potentiometer analog inputs
  int a = analogRead(A0);     
  int b = analogRead(A1);    
  int c = analogRead(A2);     
  int d = analogRead(A3); 
    
  //define "e" as the average formula
  int e = (a+b+c+d)/4;        

  //Print e
  Serial.println(e);          
  delay(500);        
}

Reminds me of this very fast moving average method, old technique from assembly days.

Basic average formula : avg’ = ( 7 x oldvalue + newvalue )/ 8 → avg’ = ( (8-1) oldvalue + newvalue )/8

Avg’ = ((depth-1)Avg + new) / depth
Avg’ = (depthAvg - avg + new) / depth
depth * Avg’ = (depth
Avg - avg + new)
if you store depth*Avg in a register ch
ch = (ch - avg/depth + new)
Divide by 8 == logic shift right >>3. 16 equals >>4 etc…
ch = (ch - ch>>3 + new)

Routine uses only integers, only one register needed, fast excecution

#define NUMSENSORS 4
#define AVGDEPTH 4
unsigned int ch[NUMSENSORS], result;;
unsigned long startus, end__us;
//--------------------------------------------------------------------------
void setup()
{ Serial.begin(9600);                               // initialize serial communication with computer
}
//--------------------------------------------------------------------------
void Method_2_loop() // fast and accurate averaging , does not throw away bits in avg routine
{ for(byte i=0; i<NUMSENSORS; i++)                 
  { unsigned int Old = ch[i];                                        // fetch old value
    ch[i]= ( Old - (Old>>AVGDEPTH) + analogRead(i)) ; 
  } 
}
int analogAvgRead(int i)
{ return (int)( ch[i]>>AVGDEPTH );
}
//--------------------------------------------------------------------------
void loop()
{
}
/*
if depth is average depth, eg. 8
Avg' = the new averaged value
Avg  = the stored value
Basic average formula : Avg'  = ((depth-1)Avg + new)  / depth
basicly               : value =   7 x oldvalue + newvalue / 8
Can be written as     : Avg'  = (depth*Avg - avg + new)  / depth
Can be written as     : depth * Avg' = (depth*Avg - avg + new)
if you store depth*Avg in your register
Can be written as     : ch[ ] = (ch[ ] - avg/depth + new)
By choosing depth 2,4,8,16,etc we can use logicShiftright for dividing
Can be written as     : ch[ ] = (ch[ ] - ch[ ]>>3 + new)
*/

excecution time is 2250us for 200 times 4 channels