Adding up analogue inputs to average them

I would like to read a sensor (acs712)
a number of times (20), add them all up, then divide them by the number of reads.
So I can get an average.
to give me a sort of analogue "debounce"

  • am guessing the sum function?*


Did you mean the + operator?

I dont know TBH which ever code gives me what I need.

You can get great help HERE :slight_smile:

Add up n samples (into a variable of a datatype capable of holding the sum), and divide the sum by n.
If you want rounding, initialise the sum variable with the value n/2.

It needs to add all the reads on the analog pin (A1) put the result some where, then I need to divide the total by the number of times that pin has been read which till give me another figure that I can then use .

Yes, that’s correct.

The way I keep a running average is just to accumulate the desired number of samples into a sum. After that you just subtract the last sample and add the new sample then compute the new average. That way you don't have to worry array storage and manipulation. Here is a snippet of what it would look like AFTER numSamples has been accumulated in sampleSum:

  sampleSum -= lastSample;
  sampleSum += currentSample;
  lastSample = currentSample;
  sampleAvg = sampleSum/numSamples;

I dont want a running average,
basicly I want to poll the senor when comanded, then use that averaged figure in a comparitor,
I.e. > or< an other analogue read figure (from a pot)

I dont want a running average,
basicly I want to poll the senor when comanded, then use that averaged figure in a comparitor,
I.e. > or< an other analogue read figure (from a pot)

So when commanded you want to read 20 samples and average them?

Yes that would be Ideal.
I need the code written with the correct syntax so I can fill in my pins etc .

I need the code written with the correct syntax

What’s my motivation?

What's my motivation?

sorry, Dont understand?

What’s my motivation for giving you a fish, instead of teaching you to fish?

You should understand that this forum is not a code writing service, rather it aims to provide help for people who have a problem with their code. There is a forum section where people can ask for code to be written but you will usually pay for such a service

Have you got a sketch that reads the value from the sensor and prints it to the Serial monitor ?

Yes I can do that, Most of the code I write is for controlling small automation machinery, so reading sensors and relating that to stepper motor positioning/heater controllers, pneumatic s.
Im fairly well versed with the basic commands.
however, this is the first time I have had to do this.

Post the code that you have to read and print the sensor value. Averaging of 20 values should be a trivial addition. What is going to trigger the taking of 20 readings or should it happen continuously ? Is there going to be a time gap between each reading and between each set of readings ? What are you going to do with the average once you have got it ? What else is the Arduino going to be doing ?

this code is for short circuit retract on an EDM machine .
basicaly , while feeding the electrode into the work piece I want to poll the acs712 ,then if the amps go over a point (compared with the return from a pot(set point)
it moves the electrode via a stepper motor up to break the short circuit,then back down to a position slightly above and resumes the steady feed down to reinstate the arc.
it needs to be averaged because , as you can imagine, the acs712 and its mapped out put will be very unsteady.
I dont need to know what the amps are there will be an ammeter on the power supply

//code for reading acs and comparing with pot to adjust short circuit point

int ar = A1; //amps read analog read(acs 712 sensor)
int ampreq = A3; //amps required read pot(0-1024)
int j;// for amplevel calcs
int actamp; //actual amps=mapped value
int ms = 2; // motor step
int md = 3;  // motor direction

void setup() {

 pinMode(ar, INPUT); //amps read analog read(acs 712 sensor)
 pinMode(ampreq, INPUT); //amps required read pot

void loop() {

 analogRead(ar); //acs715  (A1)
 analogRead(ampreq); //required amps pot(A3)

 actamp = map( analogRead(ar), 513, 1024, 0, 1024); // mapped amps value(51 points per amp - 8= 408)

 if (actamp >= ampreq) //ACS712

   for (j = 0; j <= 200; j++)
   { digitalWrite(md, LOW); //rapid up
     digitalWrite (ms, HIGH);
     delayMicroseconds (200);
     digitalWrite (ms, LOW);
     delayMicroseconds (200);

 for (j = 175; j >= 0; j--) //return distance
 { digitalWrite(md, HIGH); //rapid up
   digitalWrite (ms, HIGH);
   delayMicroseconds (200);
   digitalWrite (ms, LOW);
   delayMicroseconds (200);


if (actamp >= ampreq)I see a problem.

Please remember to use code tags when posting code

That part of the code is where I came unstuck, where I need to place the averaging code, I know that line is incorrect , might be helpful if you Identified the problem ?

And I haven't Got a clue what code tags are.