Last active
September 8, 2020 23:43
-
-
Save NT7S/4be7ee62a953f5783f0ef86521dfb0fd to your computer and use it in GitHub Desktop.
Very basic rotary encoder support for Empyrean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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