make rotary encoder code stop at a given value

Hi!
I’m trying to get my rotary encoder counter code to stop at the value 0 or 50.
if I use this piece of code:

newPosition = myEnc.read()/2;
if (newPosition != oldPosition) {
   oldPosition = newPosition;    
   Serial.println(newPosition); 
 
   if(newposition >= 50) {
    oldPosition =50; 
    newPosition = 50; }
 }

the counter stops counting values above 50. the problem is that if I continue to turn the rotary encoder after the value 50 is reached, I’ll have to turn the rotary encoder “overloadValue - 50” times before i’ll reach 49 again.

is there any way around this?

I’m using the encoder library:

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(2, 3);
//   avoid using pins with LEDs attached

int value = 2;
long newPosition;

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  
  newPosition = myEnc.read()/2;
  if (newPosition != oldPosition) {
   oldPosition = newPosition;    
   Serial.println(newPosition); }
  
  
}

I can't see anything in your code about "overloadValue" or about doing anything after the value reaches 50 so I can't help.

You will need to give more / all information.

...R

overloadValue is just how many times I've trigged the encoder. If I trigged it 60 times clockwise, I'll have to trig it 10 times counter clockwise to make it count down from 50, 49, 48 ...

You need to be using the right kind of encoder. If it's an absolute encoder, it's the wrong one for you application. An absolute encoder gives you a code based on its position. They usually start again after 1 revolution.

You need to use an incremental encoder, which only gives pulses on two lines, and those pulses indicate a step and a direction. If this is the type of encoder you have, it's strictly up to you to count or not count any pulses, and it's up to you to decide what to do with a count.

I don't have that library, and have no idea where to get it, but from the fact that you use two pins, I will assume that the encoder you SHOULD use is an incremental one.

So in your code, after you reach 50 counts, and therefore stop counting further pulses in that direction, the first time you turn the encoder in the opposite direction, the new count should decrease by one, regardless of the number of count you didn't count after freezing the count.

If, on the other hand, you want to restart from aero after reaching 50, going in the same direction, you need only zero the count when it reaches 50.

I think I'm using the right rotary encoder. It have three pins; one goes to ground, and the other two goes to pin 2 and 3.
I'm using this encoder library: Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals

I don't wish to sound rude but you seem to major in avoiding answers to relevant questions.

Let's try to make it really simple...

(1) Post all your code (in code tags please)

(2)(a) Tell us what rotary encoder you are using,
(2)(b) Provide a link to its datasheet or specifications
(2)(c) Provide a diagram to show how it's wired.

Then you should get some useful advice

...R

hansibull:
I think I'm using the right rotary encoder. It have three pins; one goes to ground, and the other two goes to pin 2 and 3.
I'm using this encoder library: Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals

Yes, you are using the rigtht kind of encoder. Unfortunately, you are not using the library correctly for what you want to do. The solution is simple. All you need to do is to add a line to the routine that reads the encoder. Since the encoder library provides a method of setting the accumulated count, you just need to use that method...

newPosition = myEnc.read()/2;
if (newPosition != oldPosition) {
   oldPosition = newPosition;    
 
   if(newposition >= 50) {
    oldPosition =50; 
    newPosition = 50;
    myEnc.write(50);      // just add this line
    }
   Serial.println(newPosition);    // and move this line to here
 }

With this line included, no matter how much more the encoder position increases, the first encoder trigger that goes in the negative direction will read as 49. Of course, you don't want the Serial.print to print 51, so you should not print it until after a possible correction.

You should probably have a similar routine for when the encoder position goes negative, too.

aw! I didn't inspect the library very well, so I didn't realize there was a write function!
The code now works like a charm :smiley: Thanks a lot for your help! :slight_smile: