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);
}
}
