Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup range up/down for each encoder #118

Open
gavron04 opened this issue Nov 12, 2024 · 7 comments
Open

How to setup range up/down for each encoder #118

gavron04 opened this issue Nov 12, 2024 · 7 comments

Comments

@gavron04
Copy link

Hello. How to add up and down range for each encoder? Like moving between 0 to 10 or different value? ESP32,

For example. Range between 0 and 3 (4 positions 0;1;2;3)

sel = encoder2.getCount();
if(sel>3)
{
  encoder2.pauseCount();
}

encoder go to 4 and then stop. Not 3 and when moving up for 4 - is paused.

I cant find easy solution in .cpp or .h for this.

Thanks for help

@gavron04
Copy link
Author

gavron04 commented Nov 13, 2024

or how to write function if direction of rotate was for left or right side? (up/down)

like:

if (result == DIR_CCW) {
make A
}

if (result == DIR_CW) {
make B
}

DIR - direction
CCW - counterclockwise
CW - clockwise

@madhephaestus
Copy link
Owner

There is not API for this, but you can try to modify the values here: https://github.com/madhephaestus/ESP32Encoder/blob/master/src/ESP32Encoder.cpp#L185

If modifying those values is working for you, you could add class variables for those values with the _INT16_MAX and _INT16_MIN as the defaults, and an API to set them before attach() is called. I would be happy to accept a PR addin that functionality once you have tested it working for your use case.

@gavron04
Copy link
Author

But it is for all connected encoders, not for each encoder, right? I need separate range for 2 encoders, for example:

one for 0-120, second for 0-10

@madhephaestus
Copy link
Owner

that's why i said "class variable" not "static variable" ;) If you made it a static variable it would be for all encoders, class variables are templated into each instance of an object.

@gavron04
Copy link
Author

gavron04 commented Nov 13, 2024

OK. How about https://github.com/madhephaestus/ESP32Encoder/blob/master/src/ESP32Encoder.cpp#L182

where:
r_enc_config.lctrl_mode = PCNT_MODE_KEEP; // Rising A on HIGH B = CW Step
r_enc_config.hctrl_mode = PCNT_MODE_REVERSE; // Rising A on LOW B = CCW Step

there is no possibly to use it as detect if encoder has CW or CCW step?

@madhephaestus
Copy link
Owner

checking for up or down count should be done by seeing if the count increased or decreased in user space and not something the encoder object should be doing since up or down is determined best by the difference between the current count, and a previous count in the arbitrary past. this should be done in user code imho.

@gavron04
Copy link
Author

gavron04 commented Nov 13, 2024

Maybe it will help :)

  newvalue = encoder.getCount();
  if (newvalue > oldvalue) {
    oldvalue = newvalue;
    Serial.println("increase");
// put here your function //
  }

  if (newvalue < oldvalue) {
    oldvalue = newvalue;
    Serial.println("decrease");
// put here your function //
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants