WIRING of KY-040 Rotary Encoder plus Demo Code

JonnyMac:
I have a low-bandwidth application (digital lock for an escape game). In my case I only need digits 0..9, so coded the interrupt like this:

void isr() 

{
  if (digitalRead(ENC_A) ^ digitalRead(ENC_B)) {
    if (encValue < 9)
      ++encValue;
    else
      encValue = 0;
  }
  else {
    if (encValue > 0)
      --encValue;
    else
      encValue = 9;
  } 
}




Note that the interrupt is triggered on a state change of the ENC_A pin. There are some who recommend against polling inside the interrupt, but in this case I think it's a good idea as I can determine direction using the current state of the encoder pins and update the encoder value so that it is available to any part of the application.

Can you please give me full example sketch of yours?

It will work on ESP8266 platform too?

Thanks in advance!