Problem connecting an ecoder

Im tryng the default and basic code from here :
https://playground.arduino.cc/Main/RotaryEncoders

My problem is that im always receiving new values, not just only when i moved the encoder.

im using an Arduino Mega, connecting the A and B to 52 and 53 pins. and the center of the encoder to the 5volts at is it indicated in the code.

I´ve tryed :

-Inverting the pins connecting A to 53 and B to 52.
-connecting the middle to GND.

I graficated the values in processing to look if I could find some kind of pattern,(the attached image).

this is my code wich is the standart code for encoder conections:

/* Read Quadrature Encoder
   Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

   Sketch by max wolf / www.meso.net
   v. 0.1 - very basic functions - mw 20061220

*/

const int encoder0PinA = 52;
const int encoder0PinB = 53;


int encoder0Pos = 50;
int encoder0PinALast = LOW;
int n = LOW;

int num = 0;
void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  Serial.begin (9600);
}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }
  encoder0PinALast = n;
}

any suggestions would be greatly aprecciated, thanks !

Arduino-Rotary-Encoder-Wiring-pinout-comparison.jpg