void IRAM_ATTR updateEncoder()
{
  uint8_t msb = digitalRead(encoderPinA);
  uint8_t lsb = digitalRead(encoderPinB);

  uint8_t encoded = (msb << 1) | lsb;
  uint8_t sum = (lastEncoded << 2) | encoded;

  if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
    encoderRaw++;
  }
  if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
    encoderRaw--;
  }

  if (encoderRaw >= 4) {
    encoderCount++;
    encoderRaw = 0;
  } else if (encoderRaw <= -4) {
    encoderCount--;
    encoderRaw = 0;
  }

  lastEncoded = encoded;
}