Smoothing example error?

Hi

Can someone check my findings please,

I have recently being trying to add smoothing code to a sketch I am writing for a sensor circuit. After several issues I decided to go back to basics and test each part of my code separately.
I started by building a basic voltage divider with a 10k resistor referenced to the ground rail from the "A0" pin and a 100 ohm resistor from the +5v pin to the "A0" pin.
I then wrote the following basic sketch to get an anologRead value from the voltage divider.

int inputpin = A0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
analogRead (inputpin);
Serial.println (inputpin);

delay (2000);
}

This returned a value of 14 see the screen dump below.
!(http://C:\Program Files (x86)\Arduino\libraries\14.jpg)

Next I tried the Example smoothing code with the same voltage divider, which returned a totally different result of 1013 see below.
!(http://C:\Program Files (x86)\Arduino\libraries\1013.jpg)
I decided to alter the code and added in Serial.println's for each of the indexes and the average. The maths side of the code works fine taking the individual readings of 1013 and placing them into the indexs' 0-9, totalling them then dividing by ten to get the average.
What is still a mystery is why this sketch returns an anolgread value on "A0" of 1013 and a different sketch returns a value of 14.
Any ideas?

You're doing an analogRead on inputpin, then throwing the result away, and printing the pin number.

Regards,
Ray L.

Thanks,
How is the result being thrown away without me resetting A0 to zero (A0 = 0);?
Do I need to move the result to another int.?

You are not SAVING the result of the analog Read. You need:

int inputpin = A0;
int analogVal = 0;

...

analogVal = analogRead(intputpin);
Serial.println(analogVal);

Regards,
Ray L.

Thanks

That has explained where I was going wrong in my other code. :slight_smile: