averaging analog input readings??

I am going to use a homemade soil moisture sensor to control my lawn sprinklers. currently I am experimenting an i made sensors from plaster of paris. I realize the downfalls of such sensors and may modify my plans but for right now I have a question.

My sensor readings are a bit erratic at times. I have my arduino at my desk and about 25 feet away and outside my window is my sensor plugged into the soil of my yard.

my readings are fluctuating a bit.

I was wondering. Is there a line of code that would take the average of the last say 10 sensor readings and return that value instead so to smooth the readings?

thanks for your help.

2 ways:

partial code, need to be completed.

void loop()
{
  int readings = 10;
  int raw = 0;
  for int i = 0; i< readings; i++) raw += analogRead(A0);
  raw /= readings;

  serial.println(raw);
}

=========================
long raw = 0;

void loop()
{
  raw =  (90* raw + 10 * analogRead(A0)) / 100;  // new reading can change 10% of the value
  serial.println(raw);
}

Note that the second needs some time to stabilize ==> you can do a first reading in setup() of course…

Is there a line of code that would take the average of the last say 10 sensor readings and return that value instead so to smooth the readings?

Not a line of code, but it isn’t hard. Use a for loop to take 10 readings, with a suitable delay in between. Sum those readings, and divide by the number of readings.

const int numReads = 10;

void loop()
{
   int averageReading = 0;
   for(int i=0; i<numReads; i++)
   {
      averageReading += analogRead(pin);
      delay(10000);
   }
   averageReading /= numReads;
}

thanks sooo much for your help. this is my code I am using... do I just add yours after mine? sorry, newbie.

// SENSOR READING


int sensePin =0;
int ledPin = 9;

void setup() {
 analogReference (DEFAULT); //isnt nec 


 Serial.begin (9600);

}

void loop(){
  
  Serial.println (analogRead(sensePin));
  
  delay (3000);

Add mine before your print statement. Then change the print statement to print averageReading, instead.

thanks allot!!! :)

did it but its not working… what did I mess up?

its giving an error of:
expected constructor,destructor, or type conversion before ‘.’ token

for line
Serial.println (averageReading (sensePin));

// SENSOR READING


int sensePin =0;


void setup() {
 analogReference (DEFAULT); 
 
 
 Serial.begin (9600);
 
}


 const int numReads = 10;

void loop()
{
   int averageReading = 0;
   for(int i=0; i<numReads; i++)
   {
      averageReading += analogRead(sensePin);
      delay(10000);
   }
   averageReading /= numReads;
} 
  Serial.println (averageReading (sensePin));
  
  delay (3000);

Look at the extent of the loop function. You have code outside of the function.

I think you meant... move the } thing below the line.

now it says 'averageReading' was not declared in this scope

  Serial.println (averageReading (sensePin));

The variable averageReading is not a function. Don't try to use it like one.

Its possible to do what usually termed a running average (but is more technically a simple "infinite-impulse-response digital filter") like this:

void loop()
{
  float averageReading = 0.0 ;
  averageReading += 0.1 * (analogRead (sensePin) - averageReading) ;
  delay (3000);
  Serial.println (averageReading) ;
}

The magic value 0.1 determines how slowly the filter responds to input changes - smaller means more slowly. So 0.3 will be quite quick, 0.01 will average over hundreds of readings. Set it to 1.0 and there is no averaging at all.

thanks guys