// encoder tester by jikun
long temp, counter = 0;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
attachInterrupt(0, forward, RISING);
attachInterrupt(1, backward, RISING);
}
void resetCounter() {
noInterrupts();
counter = 0;
interrupts();
}
void loop() {
if (counter != temp) {
temp = counter;
Serial.println(counter);
}
if (digitalRead(6) == LOW) {
resetCounter();
}
}
void forward() {
if (digitalRead(3) == LOW) {
counter++;
}
else {
counter--;
}
}
void backward() {
if (digitalRead(2) == LOW) {
counter--;
}
else {
counter++;
}
}