# Limited Count - Incremental Encoder

Hi Guys,

I saw this code to read an encoder but I wanted the count ended at 10,000 (for exemple). It's going up to 65,535

How can I do to limit the count?

->Code:

enum PinAssignments { encoderPinA = 2, encoderPinB = 3, clearButton = 8 };

volatile unsigned int encoderPos = 0; unsigned int lastReportedPos = 1;

boolean A_set = false; boolean B_set = false;

void setup() {

pinMode(encoderPinA, INPUT); pinMode(encoderPinB, INPUT); pinMode(clearButton, INPUT); digitalWrite(encoderPinA, HIGH); // turn on pullup resistor digitalWrite(encoderPinB, HIGH); // turn on pullup resistor digitalWrite(clearButton, HIGH);

// encoder pin on interrupt 0 (pin 2) attachInterrupt(0, doEncoderA, CHANGE); // encoder pin on interrupt 1 (pin 3) attachInterrupt(1, doEncoderB, CHANGE);

Serial.begin(9600); }

void loop(){ if (lastReportedPos != encoderPos) { Serial.print("Index:"); Serial.print(encoderPos, DEC); Serial.println(); lastReportedPos = encoderPos; } if (digitalRead(clearButton) == LOW) { encoderPos = 0; } }

// Interrupt on A changing state void doEncoderA(){ // Test transition A_set = digitalRead(encoderPinA) == HIGH; // and adjust counter + if A leads B encoderPos += (A_set != B_set) ? +1 : -1; }

// Interrupt on B changing state void doEncoderB(){ // Test transition B_set = digitalRead(encoderPinB) == HIGH; // and adjust counter + if B follows A encoderPos += (A_set == B_set) ? +1 : -1; }

thanks!!

Always use code tags.

``````if (x>10000) x=10000;
``````