OK here is the encoder code I am using currently:
which is basically the buxtronics code which uses a loop table to not Miss encoder output as it works on a state change table:
#define ENABLE_PULLUPS
#define NUMROTARIES 4
//Define Rotary Physical Addressing
struct rotariesdef {
byte pin1;
byte pin2;
int ccwchar;
int cwchar;
volatile unsigned char state;
};
// Define Rotary Pin and Number Map
// { PinCCW, PinCW, ButtonCCW, ButtonCW, Initial State}
rotariesdef rotaries[NUMROTARIES] {
{ 9, 10, 17, 18, 0},
{ 11, 12, 19, 20, 0},
{ 13, 14, 21, 22, 0},
{ 15, 16, 23, 24, 0}
};
//Rotary Decoding
// #define HALF_STEP //Uncomment #define HALF_STEP this for HALFSTEP encoder operation
#define DIR_CCW 0x10
#define DIR_CW 0x20
#define DIR_NONE 0x0
#ifdef HALF_STEP // Use the half-step state table (emits a code at 00 and 11)
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char ttable[6][4] = {
// R_START (00)
{R_START_M, R_CW_BEGIN, R_CCW_BEGIN, DIR_NONE},
// R_CCW_BEGIN
{R_START_M | DIR_CCW, DIR_NONE, R_CCW_BEGIN, DIR_NONE},
// R_CW_BEGIN
{R_START_M | DIR_CW, R_CW_BEGIN, DIR_NONE, DIR_NONE},
// R_START_M (11)
{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, DIR_NONE},
// R_CW_BEGIN_M
{R_START_M, R_START_M, R_CW_BEGIN_M, DIR_NONE | DIR_CW},
// R_CCW_BEGIN_M
{R_START_M, R_CCW_BEGIN_M, R_START_M, DIR_NONE | DIR_CCW},
};
#else // Use the full-step state table (emits a code at 00 only)
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
const unsigned char ttable[7][4] = {
// R_START
{DIR_NONE, R_CW_BEGIN, R_CCW_BEGIN, DIR_NONE},
// R_CW_FINAL
{R_CW_NEXT, DIR_NONE, R_CW_FINAL, DIR_NONE | DIR_CW},
// R_CW_BEGIN
{R_CW_NEXT, R_CW_BEGIN, DIR_NONE, DIR_NONE},
// R_CW_NEXT
{R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, DIR_NONE},
// R_CCW_BEGIN
{R_CCW_NEXT, DIR_NONE, R_CCW_BEGIN, DIR_NONE},
// R_CCW_FINAL
{R_CCW_NEXT, R_CCW_FINAL, DIR_NONE, DIR_NONE | DIR_CCW},
// R_CCW_NEXT
{R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, DIR_NONE},
};
#endif
// Rotary Initialization
/* Call this once in setup(). */
void rotary_init() {
for (int i=0;i<NUMROTARIES;i++) {
pinMode(rotaries[i].pin1, INPUT);
pinMode(rotaries[i].pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(rotaries[i].pin1, HIGH);
digitalWrite(rotaries[i].pin2, HIGH);
#endif
}
}
void setup() {
Serial.begin(38400);
rotary_init();
}
void loop() {
CheckAllEncoders();
}
void CheckAllEncoders(void) {
for (int i=0; i<NUMROTARIES; i++) // Scan the rotary list.
{
unsigned char result = rotary_process(i);
if (result == DIR_CCW) {
Joystick.button(rotaries[i].ccwchar, 1); delay(0); Joystick.button(rotaries[i].ccwchar, 0);
Serial.println("CounterCW");
};
if (result == DIR_CW) {
Joystick.button(rotaries[i].cwchar, 1); delay(0); Joystick.button(rotaries[i].cwchar, 0);
Serial.println("CW");
};
}
}
// Check State Of Rotatry Encoder - Returns 0 on no event, otherwise 0x80 or 0x40 depending on the direction
unsigned char rotary_process(int _i) {
unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1);
rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate];
return (rotaries[_i].state & 0x30);
}
This all works fine as is and is very quick to respond and doesn't have any delays, the very last part of the code (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1) is where the encoder pin state is detected.. So basically I want to read the matrix for the Encoder state A / B (1 / 0) state and request and watch a pair of matrix positions. as of course if it watches the entire Matrix and only detects state changes I think the encode mapping would get confused.
Ok and right now the Keypad.h matrix is what I plan on using but it could be something different.