I have used this code before to average out temperature readings from a thermistor - I'm now trying to average out current readings from a ACS712 sensor and having some issues.
The code does not return a viable figure for 'average1' and I'm not sure what I'm doing wrong but I'm sure its something simple! When I do a analog read on A2 I get a sensible figure, but adding them up twice and dividing by 2 does not seem to work?
Thanks in advance
//#define NUMSAMPLESA 16 // Number of analog samples to take
float avCur = 0;
//int sensorValue = 0;
void setup() {
 Serial.begin(9600);
}
void loop() {
 //int sum = 0;
 int average1 = 0;
 for (int i = 0; i < 2; i++) // take 2 samples from the analog input
 {
  average1 += analogRead(A2);
 }
 average1 /= 2;
 Serial.println(average1);
 //avCur =((514 - avCur) * 27.03 / 1023);
 //int sensorValue = analogRead(A2);
 //Serial.println((514 - sensorValue) * 27.03 / 1023);
 delay(100);    // delay in between reads for stability
}
I get a value of around 500, which is sensible seems as the sensor is bi-driectional but when I apply current the value barely changes, whereas if I simply print the analog reading I can see plenty of movement?
Not entirely sure what you mean to print the 2 inputs from A2 in the for loop - sorry I still have a lot to learn
I get a value of around 500, which is sensible seems as the sensor is bi-driectional but when I apply current the value barely changes,
So, not a problem with averaging but a problem with reading the value from the sensor despite you saying in your initial post
When I do a analog read on A2 I get a sensible figure,
Not entirely sure what you mean to print the 2 inputs from A2 in the for loop
int average1 = 0;
int reading;
for (int i = 0; i < 2; i++) // take 2 samples from the analog input
{
reading = analogRead(A2);
Serial.print(i);
Serial.print("\t");
Serial.println(reading);
average1 += reading;
}
As an aside, as average1 is an integer which you divide by 2 you may not strictly get the average anyway but that is a separate problem.
yes it is a problem with the code I have tried to implement to average the values - the analog reading direct from the sensor seems fine, but when I add it and divide it its not sensible?
OK thanks, so I guess I should use a float for the average1 reading so the sum can have decimal places?
I guess I should use a float for the average1 reading so the sum can have decimal places?
That would give you a more sensible average of the two values but if they are wrong it still won't give you the correct average. That is why I asked about the values that were being returned by the analogRead().
I have previously seen advice that to get consistent readings from analogRead() the input should be read once and the value returned discarded. The input is then read again immediately and the second reading is used in the program.
I strongly suggest that you print both values read so that we have more of a clue where the problem lies. You could also try connecting the input to a potentiometer whose outer pins go to 5V and GND to allow you to test the code under more controlled conditions.
OK thanks Bob, that is useful info - I will have another look when I get home from work today.
Is there a way to print each separate value that I am adding together in the for loop? I understand that if I used an array I could call something like 'average1[1]' but I'm not sure how I can further dig down into this issue at the moment?
 int sensorValues[] = {0.0};      // Define the array and initialize values to 0
 int sum = 0;
 int average1 = 0;
 int reading;
 for (int i = 0; i < 2; i++) // take 2 samples from the analog input
 {
  sensorValues[i] = analogRead(A2);
  sum += sensorValues[i];
  Serial.print("this reading: ");
  Serial.println(sensorValues[i]);
  Serial.print(" sum =");
  Serial.println(sum);
 }
 average1 = sum / 2;
This would allow you to see what's going on in the loop.