Analog read not working as expected?

int xPin = A4;
int yPin = A5;
int xval = 0;
int yval = 0;

void setup()
{
  pinMode (xPin, INPUT);
  pinMode (yPin, INPUT);
  Serial.begin(9600); // also set up Serial port for debugging
}

void loop() {

  xval = analogRead(xPin);
  yval = analogRead(yPin);

  Serial.print(" x=");
  Serial.print(xval);
  Serial.print(" y=");
  Serial.println(yval);
  delay(1000); 
}

Expected result of analog X and Y stick is between 0 and 1023 with abouy 512, 512 as center value. The stick has 2 variable resistors with one leg on ground and other leg on 5v and center wiper to x and y.

It did not matter which analog pin I tried, the number coming back isn't right at all:

 x=467 y=392
 x=368 y=361
 x=320 y=324
 x=289 y=293
 x=271 y=274
 x=259 y=262
 x=252 y=253
 x=247 y=248
 x=244 y=245
 x=243 y=244
 x=241 y=242
 x=241 y=242
 x=240 y=241
 x=240 y=241
 x=239 y=240

The number hovers between 500 and 200 and it did not change when I changed the stick direction.

I used multimeter and I can confirm 5v and ground is working right and the analog pin is varying from 0 to 5v with respect to ground. It's as if the chip is not reading it correctly somehow?

(delay 1000 is just to slow serial print spam, it will not be in final sketch)

nm on a hunch I swapped the chip on my UNO board, it now works. The one I was using seems to have bad analog pins? It did work as digital pins though so something may have damaged the built in A-D converter?

Possibly. If you are not accustomed to working with microprocessors you may have inadvertently put too high a voltage on an input (or a negative one) or overloaded it with too high a current when it is set to output.

Or when I accidentally used the wrong LED display. The chip's sketch was set to handle common anode LED display and by mistake, I wired up 3 digit common cathode display, which behaved in funny way.

I used a Udoo neo and I had a similar problem. I was wrongly connected the pins and I thought I was having some foating signals or some pull up down issues, but the solution was simpler.

In udoo neo you should set input pin as number 2 (example), then set pin 2 as INPUT, then connect your voltage signal to pin A2, not pin 2.

I didn't know that pin 2 was only for digital signals (GPIO) and that A2 was only for analog signals. Hope this might help someone else.

antonellocherubini: I didn't know that pin 2 was only for digital signals (GPIO) and that A2 was only for analog signals. Hope this might help someone else.

The analog pins can be used for digital I/O as well; digital pins can not be used as analog pins ;)