Reading Rotary Encoders -fluctuation-incrementing by itself

I've been studying for quite a while... the code that they gave here:

I've put the two 10k resistors at the terminals A and B and the 5V at C, but my arduino keeps incrementing forever, with or without the encoder connection! And if i rotate it one step, it stops for a milisecond or so, and then continues to increment.

Need HELP :confused:

/* Read Quadrature Encoder * Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V. * * Sketch by max wolf / * v. 0.1 - very basic functions - mw 20061220 * */

int val; int encoder0PinA = 3; int encoder0PinB = 4; int encoder0Pos = 0; int encoder0PinALast = LOW; int n = LOW;

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; }

If your code keeps incrementing the count, its because these 2 conditions are true :

true -> if ((encoder0PinALast == LOW) && (n == HIGH)) { if (digitalRead(encoder0PinB) == LOW) { encoder0Pos--; true -> } else { counting > encoder0Pos++

If an Arduino input pin is NOT defined, i.e. NOT connected to anything, it will constantly toggle between low and high by random noise, because the inputs are very sensistive.

To avoid this, you can use the input Mode : pinMode(inputpin, PULL_UP) This will connect an internal resistor from 5V to the input, giving a constant no change default high condition on the input. A low must then be provided by a GND connection, like a switch or a transistor output shorting the input to GND. My guess is that your encoder at rest, does not have its output defined. Only at the instant you turn it, it will provide a short time GND connection, to indicate a pulse is counted.

Dear Sir,
I am also having the same issue with my arduino.
After using, internal pullups, still having the same incremental issue.