Stopping Rotary encoder count after certain value

Morning all

I`m looking for a advice on how to stop the rotary encoder overflowing to “0” after it reach max value and oposit way around, going to max value below “0”

int inPin = 4;
int ButtonState = 0;
int dacValue = 0;
int ledstrip = 0;
#define encoder0PinA  2
#define encoder0PinB  3
#include <Wire.h>
#define MCP4725_ADDR 0x60
volatile unsigned int encoder0Pos =32767;

void setup() {
  pinMode(inPin, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);       // turn on pull-up resistor
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);       // turn on pull-up resistor

  attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
  Serial.begin (9600);
  Serial.println("start");                // a personal quirk
  Wire.begin();
}

void loop() {

  ButtonState = digitalRead(inPin);     // read the input pin
  digitalWrite(13, ButtonState);
  // Serial.println(val);

  // do some stuff here - the joy of interrupts is that they take care of themselves
 if (ButtonState == LOW ) {
   encoder0Pos = 32767;
  }
  if (encoder0Pos >= 65535) {
    dacValue = 4050;
  }
  dacValue = map(encoder0Pos, 0 , 65535, 0, 4096);
  Wire.beginTransmission(MCP4725_ADDR);
  Wire.write(64);                     // cmd to update the DAC
  Wire.write(dacValue >> 4);        // the 8 most significant bits...
  Wire.write(dacValue << 4); // the 4 least significant bits...
  Wire.endTransmission();
}

void doEncoder() {

  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
    encoder0Pos += 100;
  } else {
    encoder0Pos -= 100;
  }

  Serial.println (dacValue);
}

void doEncoder_Expanded() {
  if (digitalRead(encoder0PinA) == HIGH) {   // found a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) {  // check channel B to see which way
      // encoder is turning
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
    else {
      encoder0Pos = encoder0Pos + 1;         // CW
    }
  }
  else                                        // found a high-to-low on channel A
  {
    if (digitalRead(encoder0PinB) == LOW) {   // check channel B to see which way
      // encoder is turning
      encoder0Pos = encoder0Pos + 1;          // CW
    }
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }

  }
}

Thanks in advance.
Kris

I`m looking for a advice on how to stop the rotary encoder overflowing to "0"

The rotary encoder just outputs pulses that you read. It is your variable that holds the number of pulses that overflows. It is trivial to prevent that from happening.

Think about a bucket that can hold 5 gallons. There is nothing stopping you, except common sense, from puring 75 gallons of water into it. You can see that it is, or is not, almost full, and add, or not, more to the bucket based on that observation. Do the same with your variable.

Got it.

Thanks Paul

void doEncoder() {

  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB) && (encoder0Pos < 65000)) {
    encoder0Pos += 100;
  }

  if (digitalRead(encoder0PinA) != digitalRead(encoder0PinB) && (encoder0Pos > 100)) {
    encoder0Pos -= 100;
  }
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB) && (encoder0Pos < 65000)) {

The upper limit is 65536, so the test should use 65500.

The upper limit is 65536, so the test should use 65500.

I actually did that on the first attempt but it overflow to 0. 65400 is the max i can get without the issue. So not to risk that i get sudden voltage drop i decided to go with 65000

I actually did that on the first attempt but it overflow to 0. 65400 is the max i can get without the issue.

I suspect that you are, in the code you didn't post, incrementing somewhere else. 65400 + 100 is less than 65536.

It`s not that.

I start the encoder with middle value, because i`m driving bipolar supply. So to have 0V on the +/- scale i write 32767 as a start. so when i increase by 100 than the last value is 65467.So if i boundary 65500 the program skips that and overflow to 0. For some reason it stops overflowing if it reach 67. Not so sure but also not a big deal.

full code

int inPin = 4;
int ButtonState = 0;
int dacValue = 0;
bool flag = false;
#define encoder0PinA  2
#define encoder0PinB  3
#include <Wire.h>
#define MCP4725_ADDR 0x60
volatile unsigned int encoder0Pos = 32767;



void setup() {
  pinMode(inPin, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);       // turn on pull-up resistor
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);       // turn on pull-up resistor

  attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
  Serial.begin (9600);
  Serial.println("start");                // a personal quirk
  Wire.begin();
}

void loop() {

  ButtonState = digitalRead(inPin);     // read the input pin
  digitalWrite(13, ButtonState);
// Serial.println(flag);

  // do some stuff here - the joy of interrupts is that they take care of themselves
  if (ButtonState == LOW )  {
    delay(20);
    flag = !flag;
    encoder0Pos = 32767;
    digitalWrite(5, HIGH);
    delay(10);
    digitalWrite(7, HIGH);
  }
/*  if ((ButtonState == LOW) && (flag == true)) {
    flag = false;
    encoder0Pos = 32767;
    digitalWrite(7, LOW);
    delay(10);
    digitalWrite(5, LOW);
  }
*/
  dacValue = map(encoder0Pos, 0 , 65535, 0, 4096);
  Wire.beginTransmission(MCP4725_ADDR);
  Wire.write(64);                     // cmd to update the DAC
  Wire.write(dacValue >> 4);        // the 8 most significant bits...
  Wire.write(dacValue << 4); // the 4 least significant bits...
  Wire.endTransmission();
}

void doEncoder() {

  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB) && (encoder0Pos < 65400)) {
    encoder0Pos += 100;
  }

  if (digitalRead(encoder0PinA) != digitalRead(encoder0PinB) && (encoder0Pos > 100)) {
    encoder0Pos -= 100;
  }


Serial.print(encoder0Pos);
  //Serial.print (dacValue);
  Serial.print (" flag = ");
  Serial.println (flag);
}

void doEncoder_Expanded() {
  if (digitalRead(encoder0PinA) == HIGH) {   // found a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) {  // check channel B to see which way
      // encoder is turning
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
    else {
      encoder0Pos = encoder0Pos + 1;         // CW
    }
  }
  else                                        // found a high-to-low on channel A
  {
    if (digitalRead(encoder0PinB) == LOW) {   // check channel B to see which way
      // encoder is turning
      encoder0Pos = encoder0Pos + 1;          // CW
    }
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }

  }
}

as a start. so when i increase by 100 than the last value is 65467.So if i boundary 65500 the program skips that and overflow to 0.

OK. That makes sense. 65467 + 100 = 65567, which causes an overflow. I was assuming that you were starting from 0 and incrementing by 100.

Still, the problem WAS in part(s) of the code that you didn’t post, which caused me to make invalid assumptions.