ROTARY ENCODER MAX COUNT

I have now have a code which works perfectly with my rotary encoder and the function I was after. The function I needed the rotary to do is to give me a count from 0 - 1000 & 1000 - 0. this code attached below works in so far as when the rotary count gets to 255 it goes back to 0. how do I increase to maximum rotary count to a 1000 or more if ever I need to?

enum PinAssignments {
  encoderPinA = 3,   // right (labeled DT on our decoder, yellow wire)
  encoderPinB = 2,   // left (labeled CLK on our decoder, green wire)
  clearButton = A0    // switch (labeled SW on our decoder, orange wire)
                
};

volatile byte encoderPos = 0;  // a counter for the dial
byte lastReportedPos = 1;   // change management
static boolean rotating = false;    // debounce management

// interrupt service routine vars
boolean A_set = false;
boolean B_set = false;

int pressCount = 0;

void setup() {

  pinMode(encoderPinA, INPUT_PULLUP); // new method of enabling pullups
  pinMode(encoderPinB, INPUT_PULLUP);
  pinMode(clearButton, INPUT_PULLUP);


 // 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);  // output
}

// main loop, work is done by interrupt service routines, this one only prints stuff
void loop() {
  rotating = true;  // reset the debouncer

  if (lastReportedPos != encoderPos) {
    Serial.print("Index:");
    Serial.println(encoderPos, DEC);
    lastReportedPos = encoderPos;
  }
  if (digitalRead(clearButton) == HIGH )  {
    /*delay(300);
     pressCount ++;
     //Reset count if over max mode number
     if(pressCount > 2)
     {
     pressCount = 0;
     }
     switch (pressCount) {

     case 0:
     digitalWrite(13, LOW);
     break;
     case 1:
     analogWrite(9,25);
     break;
     case 2:
     analogWrite(9,0);
     digitalWrite(13, HIGH);
     break;

     }*/
    encoderPos = 0;
  }
  switch (encoderPos) {

    case 0:
         digitalWrite(4, HIGH);
      digitalWrite(A2, LOW);
      digitalWrite(A3, LOW);
      break;

    case 1:
      
       digitalWrite(4, HIGH);
      digitalWrite(A2, LOW);
      digitalWrite(A3, LOW);
      break;

    case 2:
      digitalWrite(4, LOW);
      digitalWrite(A2, HIGH);
      digitalWrite(A3, LOW);
      break;
    case 3:
      digitalWrite(4, LOW);
      digitalWrite(A2, LOW);
      digitalWrite(A3, HIGH);
      break;
  }

}



// Interrupt on A changing state
void doEncoderA() {
  // debounce
  if ( rotating ) delay (1);  // wait a little until the bouncing is done
  // Test transition, did things really change?
  if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
    A_set = !A_set;
    // adjust counter + if A leads B
    if ( A_set && !B_set && encoderPos != 1000)  //CHANGE HERE FOR MAX LIMIT
    {
      encoderPos += 1;                         //INCREMENT BY 1
    }
    if ( A_set && !B_set && encoderPos == 1000)   //CHANGE HERE MAX LIMIT
    {
      encoderPos = 1000;                          //DECLARE MAX LIMIT
    }

    rotating = false;  // no more debouncing until loop() hits again
  }
}
void doEncoderB() {
  // debounce
  if ( rotating ) delay (1);  // wait a little until the bouncing is done
  // Test transition, did things really change?
  if ( digitalRead(encoderPinB) != B_set ) { // debounce once more
    B_set = !B_set;
    // adjust counter + if A leads B
    if ( !A_set && B_set && encoderPos != 0)    //CHANGE HERE FOR MIN LIMIT
    {
      encoderPos -= 1;                          //DECREMENT BY 1
    }
    if (!A_set && B_set && encoderPos == 0)      //CHANGE HERE FOR MIN LIMIT
    {
      encoderPos = 0;                            //DECLARE MIN LIMIT
    }

    rotating = false;
  }

}

Use something bigger than "byte" for your counter.

Thanks for that. I should have seen that.

Proietti:
I should have seen that.

:sunglasses:

If you use subtraction to get the amount of movement as in

distance = pulseCount - prevPulseCount;

it won't matter if the variable overflows as long as it cannot overflow twice between checks.

...R

Proietti:
I have now have a code which works perfectly with my rotary encoder and the function I was after. The function I needed the rotary to do is to give me a count from 0 - 1000 & 1000 - 0. this code attached below works in so far as when the rotary count gets to 255 it goes back to 0. how do I increase to maximum rotary count to a 1000 or more if ever I need to?
]

t