madhephaestus / ESP32Encoder

A Quadrature and half quadrature PCNT peripheral driven encoder library supporting 8 encoders
Other
285 stars 60 forks source link

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

Open gavron04 opened 1 week ago

gavron04 commented 1 week ago

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 commented 6 days ago

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 commented 6 days ago

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 commented 6 days ago

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 commented 6 days ago

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 commented 6 days ago

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 commented 6 days ago

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 commented 6 days ago

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 //
  }