Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int enc_read(void) {
- int result = 0;
- byte newState;
- int enc_speed = 0;
- unsigned long start_at = millis();
- while (millis() - start_at < 50) { // check if the previous state was stable
- newState = enc_state(); // Get current state
- if (newState != enc_prev_state)
- delayMicroseconds(1);
- if (enc_state() != newState || newState == enc_prev_state)
- continue;
- //these transitions point to the encoder being rotated anti-clockwise
- if ((enc_prev_state == 0 && newState == 2) ||
- (enc_prev_state == 2 && newState == 3) ||
- (enc_prev_state == 3 && newState == 1) ||
- (enc_prev_state == 1 && newState == 0)){
- result--;
- }
- //these transitions point o the enccoder being rotated clockwise
- if ((enc_prev_state == 0 && newState == 1) ||
- (enc_prev_state == 1 && newState == 3) ||
- (enc_prev_state == 3 && newState == 2) ||
- (enc_prev_state == 2 && newState == 0)){
- result++;
- }
- enc_prev_state = newState; // Record state for next pulse interpretation
- enc_speed++;
- delayMicroseconds(1);
- }
- return(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement