AdaEncoder: a library for use with simple quadrature encoders

This is the example code, for explanation purposes:

#include <PinChangeInt.h> // necessary otherwise we get undefined reference errors.
#include <AdaEncoder.h>

#define a_PINA 2
#define a_PINB 3
#define b_PINA 5
#define b_PINB 6

int8_t clicks=0;
char id=0;

void setup()
{
  Serial.begin(115200);
  AdaEncoder::addEncoder('a', a_PINA, a_PINB);
  AdaEncoder::addEncoder('b', b_PINA, b_PINB);  
}

void loop()
{
  encoder *thisEncoder;
  thisEncoder=AdaEncoder::genie(&clicks, &id);
  if (thisEncoder != NULL) {
    Serial.print(id); Serial.print(':');
    if (clicks > 0) {
      Serial.println(" CW");
    }
    if (clicks < 0) {
       Serial.println(" CCW");
    }
  }
}

When you call AdaEncoder::genie(&clicks, &id) the variable "clicks" will be set with the new number of counts detected and "id" will be either "a" or "b" (in this example -- note that when the AddEncoder was called it defined one encoder as 'a' and the other as 'b'). Watch for the ampersand in front of the variable; it's sending the variable's address rather than the contents of the variable, the function adjusts the contents of that variable, and when the subroutine returns the variable is holding the new value. This is referred to as "passing by address" and I assume it's where you're getting confused. As an example, if clicks was set to 10, you call genie and two clicks have occurred, then clicks will be set to 12.

Note also that you never request changes from a specific encoder; when you call the "genie" function you might get back the counts for either 'a' or 'b', assuming one of those encoders has seen changes, or (thisEncoder != NULL) might be false which means neither encoder has changed.

So a couple notes on using this library:

  1. You probably want to set "clicks" to zero before you call genie so you only get the recent changes, or in your case you want to know how far the wheel has moved since you checked last.
  2. Call genie repeatedly until thisEncoder comes back NULL so that you know you know you've got all the recent changes to the encoders.

The ASCII diagram of the ATMega328 chip can be ignored. I assume you understand that your encoder has three wires; the common wire is connected to ground and the other two pins must be kept together in the digital pin groupings 0 to 7, or 8 to 13, or analog 0 to 5. e.g. digital 8 and 9 for one encoder are OK, but digital 7 and 8 are not.