# Smoothing readings of individual sensor circuits

Hello,

Please can some one lend a hand, I am very new to this and learning by jumping in feet first.
I am building a automatic irrigation system for my green house and have managed to get things working by cutting and pasting borrowed code. That being said I am having diffculty with smoothing the individual sensors.
I have tried adapting the code in the smooting example but I keep getting a cumulative running total of all the sensors added together. What I want to do is have the sensor on (A0) take 5 readings then average that reading then use this average as the final total to control a valve (for the purpose of my code test I am just getting the reading “serial printed” to my PC) .
Then I want that total to reset to zero and the Arduino take 5 readings from (A1), average it and use the reading to control a different valve. I have a total of 3 sensors controlling 3 individual valves and when this process has been done for all three sensor controlled valve circuits I want the process to start all over again.

My smoothing experiment code is below, thanks in advance for any help.

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

int inputPin = A0;
int inputPin1 = A1;
int inputPin2 = A2;
int inputPin3 = A3;

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…

// calculate the average:
// send it to the computer as ASCII digits
Serial.write (“AO Average”);
Serial.println(average);
// …wrap around to the beginning:
(total) = 0; // the running total
(average) = 0; // the average
delay(10000); // delay in between reads for stability

// advance to the next position in the array:

// if we’re at the end of the array…

// calculate the average:
// send it to the computer as ASCII digits
Serial.write (“A1 Average”);
Serial.println(average);
// …wrap around to the beginning:
(total) = 0; // the running total
(average) = 0; // the averag
delay(10000); // delay in between reads for stability

// advance to the next position in the array:

// if we’re at the end of the array…

// calculate the average:
// send it to the computer as ASCII digits
Serial.write (“A2 Average”);
Serial.println(average);
// …wrap around to the beginning:
(total) = 0; // the running total
(average) = 0; // the averag
delay(10000); // delay in between reads for stability

// advance to the next position in the array:

// if we’re at the end of the array…

// calculate the average:
// send it to the computer as ASCII digits
Serial.write (“A3 Average”);
Serial.println(average);
// …wrap around to the beginning:
(total) = 0; // the running total
(average) = 0; // the averag
delay(10000); // delay in between reads for stability

smoothing_test2.ino (3.5 KB)

I have tried adapting the code in the smooting example but I keep getting a cumulative running total of all the sensors added together.

You need an array of readings for each sensor, if you need an array at all.

Thanks, I want the smoothing to stop chatter on the actuators or random fluctuations tyrning them on/off. Can you help with an example sketch I can only find the one for the blinking LED's or ones that are far too complicated for a layman.

Can you help with an example sketch

You already have that. All you need to do is create a different array for each sensor. From that, you can probably see that reading, reading1, reading2, etc. are useless names. If nothing else the array name should define the sensor pin that the readings were taken from.

I’ve used this on a similar project.

``````    //sketch avg_16.ino

byte sensorPin[] = {A0, A1, A2, A3};
int sensorVal[4];
void setup()
{
Serial.begin(9600);
}

void loop ()
{
for(byte i = 0;i < 4;i++)
{
sensorVal[i] = 0;
for(byte j = 0;j < 16;j++)
{
// get accumulated sum of 16 samples
}
sensorVal[i] /= 16; // divide sum by 16 for average
}
// print each sensor value
Serial.println(sensorVal[0]);
Serial.println(sensorVal[1]);
Serial.println(sensorVal[2]);
Serial.println(sensorVal[3]);
Serial.println();
delay(2000);
}
``````

Thank you for the help.
I think I get it., Ill have a try at adapting my code to suit putting the readings into individual arrays.

Welcome to the Forum. Please read Nick Gammon's two articles at the top of this Forum for guidelines when posting, especially the use of code tags when posting source code. Just before you post your code, use Ctrl-T in the IDE to automatically reformat your code into a standard format. This will help us help you.