PaulStoffregen master encoder library

Hi there; using PaulStoffregen master encoder library for multiple encoders
Would like to know how to get the absolute values from the encoder rather than the totals, i.e. –1,0 +1 rather that the total 0,1,2,3,4, etc. Is this possible ??

HINT - do you think that it might help if you posted the code here ?

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

I simply don't understand your question.

What's the difference between absolute and totals? this is the same for me.

-1,0 and +1,0 are single incements something completely different from absolute values.

Give an example with different but still easy to understand numbers
best regards Stefan

I have

int dir = encoder.read();
if (abs(dir) >= ENCODER_PULSES_PER_STEP) {
  if (dir > 0) {
    ...
  } else {
    ...
  }
  encoder.write(0);
}

Juraj:
I have

int dir = encoder.read();

if (abs(dir) >= ENCODER_PULSES_PER_STEP) {
  if (dir > 0) {
    ...
  } else {
    ...
  }
  encoder.write(0);
}

The problem with this approach is that it is not atomic. While you're executing the code between encoder.read() and encoder.write(0), the encoder tick count could have been updated in the interrupt handler.

You could try something like this:

#include <Encoder.h>

Encoder enc(2, 3);

void setup() {
  Serial.begin(115200);
  while (!Serial);
}

constexpr uint8_t pulsesPerStep = 4;

void loop() {
  static int8_t remainder = 0;

  int delta     = enc.readAndReset() + remainder;
  int stepDelta = delta / pulsesPerStep;
  remainder     = delta % pulsesPerStep;

  if (stepDelta)
    Serial.println(stepDelta);
}

Pieter

Hi there - Was just using the example code of the library, which is

#include <Encoder.h>

// Change these pin 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 knobLeft(22, 24);
Encoder knobRight(32, 34);
//   avoid using pins with LEDs attached

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

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  //if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(knobLeft.read());
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    //positionLeft = newLeft;
    //positionRight = newRight;
  //}
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
  }
}

Pieter, - will try that later, the idea of using write to reset the numbers, look good, would that give a negative number.
StefanL38 - Like the real results from the encoder, rather than the totals, so encoder reads 0, [turn encoder] encoders returns 1, [turn encoder] returns 2, [turn encoder] returns 3, [turn other way encoder] returns 2. I would like encoders reads 0, [turn encoder] reads 1, returns 1 then 0, and so on, if that makes it clearer than mud.
Many thanks

@Pieter, I am aware of small possibility of skipped pulses. the code is from a kitchen timer, so slow human input. It's not worth a more complicated code.

@sthomas229, my encoders have steps. they are not rotating free. and they send more then one pulse per step. one model sends two, other one sends four pulses