Rotary encoder experiencing step loss at high speeds

Hello everyone, I am facing some issues while using an Arduino Leonardo to simulate mouse movements. I am using an ABSOLUTE MAGNETIC rotary encoder input as the y-axis value for the mouse. However, I noticed that the mouse movement is very slow when I run the rotary encoder at high speeds.

Thanks!
This is my code:

#include <Mouse.h>
#include "avdweb_AnalogReadFast.h"


#define encoderPin  A0
#define maxEncoderValue  255  

volatile unsigned int currentEncoder0Pos = 0;
volatile unsigned int previousEncoder0Pos = 0;

long wheelValue = 0;

bool isWheelValuePositive = true;

void setup() {
  pinMode(encoderPin, INPUT);
  Mouse.begin();
  Serial.begin(115200);
}

void loop() {
  updateMouseWheel();
  delayMicroseconds(50);
}

// output mousewheel positive value when going in one direction
// and output a negative value when going in the opposite direction
void updateMouseWheel()
{
  currentEncoder0Pos = analogRead(encoderPin);
  currentEncoder0Pos = map(currentEncoder0Pos, 0, maxEncoderValue, 0, 128);
  
 
  if(previouscurrentEncoder0Pos != currentEncoder0Pos)
  {
   
    if(currentEncoder0Pos > previouscurrentEncoder0Pos)
    {
     
      if(!isWheelValuePositive)
      {
        wheelValue = 0;
        isWheelValuePositive = true;
      }
     
      wheelValue++; 
    }
    
    else if(currentEncoder0Pos < previouscurrentEncoder0Pos)
    {
     
      if(isWheelValuePositive)
      {
        wheelValue = 0;
        isWheelValuePositive = false;
      }
     
      wheelValue--;
    }

    previouscurrentEncoder0Pos = currentEncoder0Pos;

    
    Mouse.move(0,wheelValue,0);
  }
}

Whatever the delta between the current and previous read you increment or decrement de value by just 1

May be use a formula that uses more than 1 if the delta is larger

Thank you, I understand what you mean! Perhaps I should update 'wheelValue++' to 'wheelValue = deltaY'.

That’s a simple way to do it. See if it works

(Curious to know exactly what you get out of your encoder though - the analog value cannot just keep growing if you keep turning)

Hi, @ming_psych
Welcome to the forum.

Can you please post a link to data/specs of the encoder?
Can you please post a circuit diagram?

Thanks... Tom... :smiley: :+1: :coffee: :australia:

1 Like

Thank you, Tom.

My encoder is an absolute rotary encoder from US Digital (https://www.usdigital.com/products/encoders/absolute/kit/mae3/). It has only one analog output. And what type of circuit diagram would you like to see?

I am using a mouse with Raspberry Pi 4B. Everything works fine when I turn at a normal speed, but I encounter issues when I turn the mouse at very high speeds.

that's the output signal when you perform one turn

it is possible that if you run fast enough you could do more than one turn in between two sampling, or just that you actually appear to go backwards when you are actually going forward...

you'd be better off with a quadrature encoder (and the encoder library)

1 Like

Thanks your advice! I will buy a quadrature encoder to test.

i found that interrupts are needed to track an encoder even when turned by hand

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.