Skip to content

Instantly share code, notes, and snippets.

@NT7S
Last active September 8, 2020 23:43
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 NT7S/4be7ee62a953f5783f0ef86521dfb0fd to your computer and use it in GitHub Desktop.
Save NT7S/4be7ee62a953f5783f0ef86521dfb0fd to your computer and use it in GitHub Desktop.
Very basic rotary encoder support for Empyrean
constexpr uint8_t encoder_pin_a = 5;
constexpr uint8_t encoder_pin_b = 6;
int16_t count = 0;
bool prev_b = false;
bool change = false;
void ISRENCA()
{
static bool prev_read = false;
if(prev_b)
{
count--;
change = true;
}
else
{
count++;
change = true;
}
}
void ISRENCB()
{
prev_b = !prev_b;
}
void setup()
{
// Set pin modes
pinMode(encoder_pin_a, INPUT_PULLUP);
pinMode(encoder_pin_b, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoder_pin_a), ISRENCA, RISING);
attachInterrupt(digitalPinToInterrupt(encoder_pin_b), ISRENCB, CHANGE);
prev_b = digitalRead(encoder_pin_b);
// Serial
SerialUSB.begin(115200);
while(!SerialUSB);
SerialUSB.println("Encoder demo");
}
void loop()
{
if(change)
{
SerialUSB.println(count);
change = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment