// Used for generating interrupts using CLK signal
const unsigned int pinA = 2;
// Used for reading DT signal
const unsigned int pinB = 3;
// Updated by ISR for each pin
volatile int pinACount;
volatile int pinBCount;
// Delay to counteract switch bounce (milliseconds)
char bounce = 15;
// ------------------------------------------------------------------
// INTERRUPT INTERRUPT INTERRUPT INTERRUPT INTERRUPT
// ------------------------------------------------------------------
void isrA () {
static unsigned long lastInterruptTimeA = 0;
unsigned long interruptTimeA = millis();
// If interrupts come faster than Xms, assume it's a bounce and ignore
if (interruptTimeA - lastInterruptTimeA > bounce) {
if(digitalRead(pinB) == HIGH)
pinACount++;
else
pinACount--;
// Keep track of when we were here last
lastInterruptTimeA = interruptTimeA;
}
}
void isrB () {
static unsigned long lastInterruptTimeB = 0;
unsigned long interruptTimeB = millis();
// If interrupts come faster than Xms, assume it's a bounce and ignore
if (interruptTimeB - lastInterruptTimeB > bounce) {
pinBCount++;
// Keep track of when we were here last
lastInterruptTimeB = interruptTimeB;
}
}
// ------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// ------------------------------------------------------------------
void setup() {
// Just whilst we debug, view output on serial monitor
Serial1.begin(115200);
// Rotary pulses are INPUTs
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
// Attach the routine to service the interrupts
attachInterrupt(pinA, isrA, CHANGE);
//attachInterrupt(pinB, isrB, CHANGE);
// Ready to go!
Serial1.println("Start");
}
// ------------------------------------------------------------------
// MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP
// ------------------------------------------------------------------
void loop() {
// Write out to serial monitor the value and direction
Serial1.print("A="); Serial1.print(pinACount); Serial1.print(" B="); Serial1.println(pinBCount);
delay(1000);
}