Rotary Encoder - constrain command doesn´t limit the values

Hi,
I tried to work with with this sketch, but its not doing exactly what I need:

#include <Encoder.h>
    Encoder Zaehler (5, 6);
    void setup()  
      {
        Serial.begin(9600);
       }
    void loop()
    {
      int newZaehler=Zaehler.read();
      newZaehler = constrain(newZaehler,30,50);  
      Serial.println(newZaehler);
    }

The problem is when I turn the encoder CW further than the 50 ...for example 10 steps - I need to turn CCW again 11 steps to reach the value 49. But I want to turn only one step to reach the first value below the upper maximum. How to set the min and max values of the counter and not only the min and max value of the display?
Hope you got what I mean.
Rgds from Germany
Scoovy

The constrain() function DOES limit the value. What it does NOT do is tell the encoder that you have constrained the value. So, of course, for the encoder to return a value less than 50, you have to crank it back however many steps you want over 50.

It seems that with the constrain () function only the value which will be displayed is constrained but not the counted encoder value itself. Otherwise there would be no "overcounting" of the set limits.
I hope you got what I tried to explain.

I tried other ways without the desired result:

"if(newZaehler<30){newZaehler=30;} "
or
"write.newZaehler();"
or
void setMinMax(float _min, float _max);

Anymore hints?

Anymore hints?

You need to understand what

      int newZaehler=Zaehler.read();

is doing. Since the encoder doesn't know that you want the value to be with certain limits, it will continue to tell you where the encoder is.

It is possible that the library you are using has a way to set/reset the value that read() returns, which can be used to set the 0 position. It is possible that it has a method to set some other position. If it does, any value above 50 should be used to trigger a set to 50. Any value below 30 should be trigger a set to 30.

Without knowing which library you are using, you are on your own.

Thanks Paul for your reply.
Concerning the used lib: in the first line of my sketcht following lib is included:
#include <Encoder.h>

Using Mr. Go..le did not bring the result I was searching for:

  • methods of the lib
  • value setting
  • resetting method of the encoder counter
    etc.

Hope I don`t stay on my own with this problem :confused:

It appears that the constrain() method returns newZaehler if the value falls between 30 and 50. Any value greater than 50 returns 50; any value less than 30 returns 30. If I understand your question, you might need something like:

    #define UPPERLIMIT    50
    #define LOWERLIMIT    30

    // more code...

    void loop()
    {
      int newZaehler = 0;

      newZaehler =Zaehler.read()
      newZaehler = constrain(newZaehler,LOWERLIMIT, UPPERLIMIT);  

      if (newZaehler == UPPERLIMIT) {       // If encoder >= 50...
         newZaehler = UPPERLIMIT - 1;        // ...set it to 49
      } else {
           if (newZaehler == LOWERLIMIT) { // If encoder <= 30...
              newZaehler = LOWERLIMIT + 1; // ...set it to 31
           }
       }
      Serial.println(newZaehler);
    }

Like I said, I'm not sure if this is your design goal nor am I sure how the constrain() method works.

Like I said, I'm not sure if this is your design goal nor am I sure how the constrain() method works.

OP's problem is that the encoder doesn't know that he has ignored some values that it returns, by using constrain. So, when he/she twists the encoder to 60, and constrains the value to 50, he/she wants, when turning the encoder in the opposite direction, to get 49, 48, etc. That is not a reasonable expectation, without telling the encoder that he/she has ignored some values that the encoder returns.

OP: I can read code. I know that you are using Encoder.h. What I don't know is which aisle at WalMart you got the library.

Unless you post a link, we can't help you.

@PaulS: Since we do not have any Walmart here in Germany, I had to use the unsexy way of shopping via Internet. I found the lib here: Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals

Are these the information you need?
Tks for yr support.

@econjack --> no change with your sketch change - thanks for try

Are these the information you need?

Yes.

Basic Usage
Encoder myEnc(pin1, pin2);

Create an Encoder object, using 2 pins. You may create mulitple Encoder objects, where each uses its own 2 pins. The first pin should be capable of interrupts. If both pins have interrupt capability, both will be used for best performance. Encoder will also work in low performance polling mode if neither pin has interrupts.
myEnc.read();

Returns the accumulated position. This number can be positive or negative.
myEnc.write(newPosition);

Set the accumulated position to a new number.

You are using two out of the three functions. Use the third one, instead of, or in addition to, constrain(), and the encoder will never report a position above 50 or below 30.

Thanks PaulS and now it works with this changed sketch:
#include <Encoder.h>
Encoder Zaehler (5, 6);
void setup()
{
Serial.begin(9600);
}
void loop()
{

int newZaehler=Zaehler.read();
if(newZaehler<30){Zaehler.write(30);}
if(newZaehler>50){Zaehler.write(50);}
newZaehler = constrain(newZaehler,30,50);

Serial.println(newZaehler);
}

In future I will take more care of the lib documentations :-*