igorantolic / ai-esp32-rotary-encoder

Easy implement rotary encoder to your application using microcontroler like ESP32
GNU General Public License v2.0
284 stars 70 forks source link

setCount() after setup() #72

Closed siz- closed 11 months ago

siz- commented 11 months ago

Hi,

I am trying to set higher level boundaries, but I am having issues as I cannot set a max boundary that works like intended. SetCount() can be set in setup(), but not again in code. It keeps on counting beyond the boundary in hardware, so it will take many additional rotations to return. Clear() sets it to 0, I can't setCount() after clearing. Pause() will always keep the same position so resume() will never be called. If I resume and pause each time, it will keep adding to the count if going clockwise...

Any suggestions?

Thanks :)

#include <ESP32Encoder.h>

ESP32Encoder encoder;
long oldPosition = 0;

void setup () { 
  encoder.attachHalfQuad ( 13, 12 );
  encoder.setCount ( 0 );
  Serial.begin ( 115200 );
}

long encFunc (ESP32Encoder d, long min_boundary, long max_boundary, long stepsize) {
  long position = (d.getCount() / 2) * stepsize;
  if (position < min_boundary) {
    d.clearCount();  
    position = min_boundary;
  } else if (position > max_boundary) {
    d.setCount(max_boundary * 2);
    position = max_boundary;
  } 
  return position;
}

void loop () {    
 long newPosition = encFunc(encoder, 0, 100, 5);
  if (oldPosition != newPosition) {
    Serial.println("Encoder 1: ");
    Serial.println(newPosition);
    oldPosition = newPosition;
  }
}
siz- commented 11 months ago

wrong repo, sorry...