Smoothing Data

Hi smart people

My head hurts lol… I have been at this for hours and I am over it (coding is not my forte)

Would someone be kind enough to type in the code to smooth out these 4 analogue inputs, as I am using this as a tool to balance throttle body vacuum over 4 ports, it would be nice to have a smoother data.

Thanks, mega appreciate your help

int minVoltage = 950;
int maxVoltage = 1050;

void setup() {
// put your setup code here, to run once:
// initialize serial communication at 9600 bits per second:

void loop() {
// read the input on analog pin 1:
int sensorValue1 = analogRead(A1);
int sensorValue2 = analogRead(A2);
int sensorValue3 = analogRead(A3);
int sensorValue4 = analogRead(A4);

// Convert the analog reading (which goes from 1.3 - 4.7) to a preasure (800 - 1160):
float voltage1 = sensorValue1 * (4.59 / 3.4); // print out the value you read:
float voltage2 = sensorValue2 * (4.617 / 3.4); // print out the value you read:
float voltage3 = sensorValue3 * (4.60 / 3.4); // print out the value you read:
float voltage4 = sensorValue4 * (4.62 / 3.4); // print out the value you read:

Serial.print(" “);
Serial.print(” “);
Serial.print(” “);
Serial.print(” “);
Serial.print(” ");


Use an averaging method:

int readAverage(byte pin, byte num_readings = 10)
  int result = 0;
  for (byte i = 0; i < num_readings; i++)
    result += analogRead(pin);
  return result / num_readings;

Thanks Dan

Yeah.. I found these methods upon google search but shamefully I have no idea how to add it to the script for the 4 signals (I tried)

Everything I know I learnt today, and is in the above script and tbh I have no idea what any of it means lol

What do you want to do? Smooth each of the 4 sensors data, in time? Or compute the average of the 4 readings at the same instant?

I guess you want the first option. In this case, you need some kind of memory. Suppose you want to average over the last 4 readings (for each sensor independently), you need to create an array of 4 values for each sensor and store 4 readings (at 4 consecutive instants) then compute the average values.

What I would do is:

  • Declare the arrays, dimension N (to store N values, your choice ffor N), one for each sensor
  • In the setup: read N-1 values for each sensor (use a for loop) and store them in the arrays
  • In the loop: for each sensor: read 1 value, compute the average over the N values, print it, left shift the array

hi lesept

Yes I would like to smooth each of the 4 sensors data, in time

you have given me some nice new keywords to search in the morning