#include <RotaryEncoder.h>
#define CLK_A 16
#define DT_A 17
#define CLK_B 18
#define DT_B 19
#define CLK_C 21
#define DT_C 22
#define CLK_D 23
#define DT_D 25
RotaryEncoder *RotEnc_A = nullptr;
RotaryEncoder *RotEnc_B = nullptr;
RotaryEncoder *RotEnc_C = nullptr;
RotaryEncoder *RotEnc_D = nullptr;
const char *Player1_Initial [26] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int counter_a = 0;
const char *Player2_Initial [26] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int counter_b = 0;
const char *Player3_Initial [26] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int counter_c = 0;
const char *Player4_Initial [26] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int counter_d = 0;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Is the Serial port working?");
RotEnc_A = new RotaryEncoder(CLK_A, DT_A, RotaryEncoder::LatchMode::FOUR3);
RotEnc_B = new RotaryEncoder(CLK_B, DT_B, RotaryEncoder::LatchMode::FOUR3);
RotEnc_C = new RotaryEncoder(CLK_C, DT_C, RotaryEncoder::LatchMode::FOUR3);
RotEnc_D = new RotaryEncoder(CLK_D, DT_D, RotaryEncoder::LatchMode::FOUR3);
attachInterrupt(digitalPinToInterrupt(CLK_A), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(DT_A), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(CLK_B), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(DT_B), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(CLK_C), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(DT_C), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(CLK_D), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(DT_D), checkPosition, CHANGE);}
void checkPosition() {
RotEnc_A->tick();
RotEnc_B->tick();
RotEnc_C->tick();
RotEnc_D->tick();
}
void limit_A() {
if ((int)(RotEnc_A->getDirection()) < 0) {
counter_a --;
if (counter_a < 0) {
counter_a = 25;
}
}
else {
counter_a ++;
if (counter_a > 25) {
counter_a = 0;
}
}
Serial.print("counter_a = ")&&Serial.println(counter_a);
}
void limit_B() {
if ((int)(RotEnc_B->getDirection()) < 0) {
counter_b --;
if (counter_b < 0) {
counter_b = 25;
}
}
else {
counter_b ++;
if (counter_b > 25) {
counter_b = 0;
}
}
Serial.print("counter_b = ")&&Serial.println(counter_b);
}
void limit_C() {
if ((int)(RotEnc_C->getDirection()) < 0) {
counter_c --;
if (counter_c < 0) {
counter_c = 25;
}
}
else {
counter_c ++;
if (counter_c > 25) {
counter_c = 0;
}
}
Serial.print("counter_c = ")&&Serial.println(counter_c);
}
void limit_D() {
if ((int)(RotEnc_D->getDirection()) < 0) {
counter_d --;
if (counter_d < 0) {
counter_d = 25;
}
}
else {
counter_d ++;
if (counter_d > 25) {
counter_d = 0;
}
}
Serial.print("counter_d = ")&&Serial.println(counter_d);
}
void loop() {
static int pos_A = 0;
static int pos_B = 0;
static int pos_C = 0;
static int pos_D = 0;
int newPos_A = RotEnc_A->getPosition();
int newPos_B = RotEnc_B->getPosition();
int newPos_C = RotEnc_C->getPosition();
int newPos_D = RotEnc_D->getPosition();
if (pos_A != newPos_A) {
limit_A();
}
if (pos_B != newPos_B) {
limit_B();
}
if (pos_C != newPos_C) {
limit_C();
}
if (pos_D != newPos_D) {
limit_D();
}
pos_A = newPos_A;
pos_B = newPos_B;
pos_C = newPos_C;
pos_D = newPos_D;
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
encoder1:CLK
encoder1:DT
encoder1:SW
encoder1:VCC
encoder1:GND
encoder2:CLK
encoder2:DT
encoder2:SW
encoder2:VCC
encoder2:GND
encoder3:CLK
encoder3:DT
encoder3:SW
encoder3:VCC
encoder3:GND
encoder4:CLK
encoder4:DT
encoder4:SW
encoder4:VCC
encoder4:GND