/*
Multi rotary encoder test
https://forum.arduino.cc/t/refering-to-an-object-instance-in-a-for-loop/693985
*/
#include <SimpleRotary.h>
const int NUM_ENCODERS = 5;
const int EN_CLK_PINS[] = {11, 8, 5, 2, A0};
const int EN_DT_PINS[] = {12, 9, 6, 3, A1};
const int EN_SW_PINS[] = {13, 10, 7, 4, A2};
SimpleRotary enc1(EN_CLK_PINS[0], EN_DT_PINS[0], EN_SW_PINS[0]);
SimpleRotary enc2(EN_CLK_PINS[1], EN_DT_PINS[1], EN_SW_PINS[1]);
SimpleRotary enc3(EN_CLK_PINS[2], EN_DT_PINS[2], EN_SW_PINS[2]);
SimpleRotary enc4(EN_CLK_PINS[3], EN_DT_PINS[3], EN_SW_PINS[3]);
SimpleRotary enc5(EN_CLK_PINS[4], EN_DT_PINS[4], EN_SW_PINS[4]);
SimpleRotary *pEncoder[] = {
&enc1,
&enc2,
&enc3,
&enc4,
&enc5
};
void checkEncoders() {
for (int i = 0; i < NUM_ENCODERS; i++) {
// 0 = not turning, 1 = CW, 2 = CCW
int value = pEncoder[i] -> rotate();
if (value != 0) {
Serial.print("Encoder ");
Serial.print(i + 1);
Serial.print(": ");
if ( value == 1 ) {
Serial.println("CW");
} else {
Serial.println("CCW");
}
}
// 0 = not pushed, 1 = pushed
int pressed = pEncoder[i] -> push();
if (pressed) {
Serial.print("Encoder ");
Serial.print(i + 1);
Serial.println(" pressed");
}
}
}
void setup() {
Serial.begin(115200);
Serial.println("Ready");
}
void loop() {
checkEncoders();
}