#include #include #include "PinChangeInterrupt.h" volatile int16_t counter = 0; volatile bool Update = false; volatile int currentStatePhase_B; volatile int lastStatePhase_B; volatile int pulses = 0; // Shutdown Output #define SW 11 // Rotary Encoder Inputs #define Phase_A 13 #define Phase_B 12 #define DIO 3 #define CLK 4 TM1637Display display(CLK, DIO); void setup() { // Set encoder pins as inputs pinMode(Phase_A,INPUT); pinMode(Phase_B,INPUT); pinMode(SW, OUTPUT); // Read the initial state of Phase_B lastStatePhase_B = digitalRead(Phase_B); // Call updateEncoder() when any high/low changed seen // on interrupt 0 (pin 2), or interrupt 1 (pin 3) attachPCINT(digitalPinToPCINT(Phase_A), updateEncoder, CHANGE); attachPCINT(digitalPinToPCINT(Phase_B), updateEncoder, CHANGE); display.setBrightness(0x0f); display.clear(); display.showNumberDec(0, false); } void loop() { if (Update){ // Show decimal numbers with/without leading zeros // display.showNumberDec(pulses, false); // delay(1000); display.showNumberDec(counter, false); Update = false; } } void updateEncoder(){ // Read the current state of Phase_B currentStatePhase_B = digitalRead(Phase_B); // If last and current state of Phase_B are different, then pulse occurred // React to only 1 state change to avoid double count if (currentStatePhase_B != lastStatePhase_B && currentStatePhase_B == 1){ // If the Phase_A state is different than the Phase_B state then // the encoder is rotating CCW so decrement if (digitalRead(Phase_A) != currentStatePhase_B) { counter --; } else { // Encoder is rotating CW so increment counter ++; } if (counter > 1023){ counter = 1023; } if (counter < 0){ counter = 0; } } pulses ++; // Remember last Phase_B state lastStatePhase_B = currentStatePhase_B; Update = true; }