Adding up analogue inputs to average them

Use the </> button on the menu, it opens a box

that you then paste your code into.

hanica:
, I know that line is incorrect , might be helpful if you Identified the problem ?

. int ampreq = A3;

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

This is a simple 'for' loop. Study it, make an attempt and someone will be glad to help.

how about this:

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)


for (int j = 0; j < 10; i++)
{
  Sum = Sum + ar;   //Sum for averaging
 
   
}

actamp = (Sum/10);
if (actamp>=ampreq)        //then compare actamp with ampreq
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);
  }

}
int ar = A1; //amps read analog read(acs 712 sensor)

ar is a pin number

 analogRead(ar); //acs715  (A1)

Reads the analogue pin and throws away the answer

for (int j = 0; j < 10; i++)
{
  Sum = Sum + ar;   //Sum for averaging
 
   
}

Sum will be 10 times the pin number, or it would be if Sum were declared and defined as zero. Is that what you want ?

Declare Sum and set its value to zero. Read the input 10 times and add each reading to Sum then divide Sum by 10 to get the average

cheers Bob,
how do I add each reading to sum?
is it:

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
int sum;  // declared place for added readings
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)



[color=#000000][color=#222222]for (int j = 0; j < 10; i++)[/color][/color]
[color=#000000]{[/color]
[color=#000000]sum=[color=#222222]analogRead(ar)+sum[/color][/color]
[color=#000000][color=#222222]actamp = (Sum/10);[/color][/color]
 
   
}

analogRead(ampreq);
if (actamp>=ampreq)        //then compare actamp with ampreq
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) Again, the same problem

actamp is used in an earlier part of the code :
actamp = map( analogRead(ar), 513, 1024, 0, 1024); // mapped amps value(51 points per amp - 8= 408)
//because the + amps start at 2.5v out of the acs712 which is actually an ac device

...and ampreq has the value ..?

These two lines read analog values and throw them away. They are a waste of time:

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

Here you are comparing your average to a pin value:

if (actamp>=ampreq)

how do I add each reading to sum?

sum = sum + reading;
  analogRead(ampreq); //required amps pot(A3)

"Here you are comparing your average to a pin value:"
yes the analogread of A3

No. You have thrown the reading away - there is no assignment of the return value of analogRead to a variable.

All you did was waste 100 us or so

hanica:

  analogRead(ampreq); //required amps pot(A3)

"Here you are comparing your average to a pin value:"
yes the analogread of A3

Nope. You are actually comparing to the pin number not the analog value.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.