Skip to content

Instantly share code, notes, and snippets.

@jayrhynas
Created July 28, 2022 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayrhynas/f87df2e7fcd3a669a2fa8a3b36d7d250 to your computer and use it in GitHub Desktop.
Save jayrhynas/f87df2e7fcd3a669a2fa8a3b36d7d250 to your computer and use it in GitHub Desktop.
class Encoder {
...
int32_t *_currentPosition;
...
}
Encoder::Encoder(int32_t *currentPosition, uint8_t ENCA, uint8_t ENCB) {
_ENCA = ENCA;
_ENCB = ENCB;
_currentPosition = currentPosition;
pinMode(_ENCA, INPUT_PULLUP);
pinMode(_ENCB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(_ENCA), updateEncoder, RISING);
}
void Encoder::updateEncoder() {
if (digitalRead(_ENCB) > 0) {
*_currentPosition++;
} else {
*_currentPosition--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment