Use Potentiometer as mouse wheel

I am trying to emulate a mouse scroll using a potentiometer, however I have found no tutorials online to help me out, the closest I have gotten is to using the mouse.h library, and using the mouse.move function however, the description for it does not give any info for moving the wheel. Here is a link to the description of the function. I have a pro micro so I assume it would work with the current hardware.

The scroll wheel is relative, and you may have noticed you can turn one endlessly.

A potentiometer is absolute, and you may have noticed you can only turn one so far in either direction.

So first, describe how operating the potentiometer is to be interpreted as a scrool wheel movement.

a7

Ah ok, so I should use a rotary encoder?

You can use a rotary encoder for unlimited coordinates or a pot or analog joystick for the speed of movement. Analog joysticks have two pots with a spring-defined idle position of constant voltage in their mid. The more you press the stick the faster mouse ticks should be created.

1 Like

You could use the map function to relate the potentiometer values to the horizontal position of the screen

MouseCursorPosition=Map(PotReading,PotMin,PotMax,MaxLeftScreenPosition, MaxRightScreenPosition);

A rotary encoder would be an excellent, and probably preferred option as well

1 Like

Yes, that wouid be a better simulator.

1 Like

So, I looked at a tutorial online that uses the mouse.move function and I swapped the vertical value to be in the wheel placement instead of ypos. I can now scroll to my hearts content (although at very high speeds) using a potentiometer, but when I stop turning the pot, the scrolling continues, I am wanting the scrolling to stop (and then continue when I move the pot). I assume I need to use the map function that you talked about to do this, how would I implement it?

That's why the analog joysticks revert to the middle (neutral) position if you release the stick.

Switch to a rotary encoder as suggested

OR

if you want to use a potentiometer, post the code you are having some fun and success with now and we can help see how to fix it up.

a7

1 Like

Sorry, should probably give the code, lol
For the thing I'm using it for, it will only need to go to a limited point so I'll just stick with the pot
Code:

#include <Mouse.h>

int scrollPin = A0;
int scrollValue;
int scrollZero;
const int sensitivity = 100;

void setup() {
Serial.begin(9600);

pinMode(scrollPin,INPUT);
delay(1000);
scrollZero = 511;
}

void loop() {
Serial.println(scrollValue);
scrollValue = analogRead(scrollPin) - scrollZero;
if(scrollValue != 0);
Mouse.move(0,0, scrollValue/sensitivity);

}

Well, that is precisely what a mouse wheel does use.

Mind you, most mouse wheels are mechanical contacts, not optical encoders, so it is impressive that they work so reliably. :grin:

1 Like

I’ve never seen a mouse wheel that wasn’t optical! Rotary encoders I have looked at are mechanical and there i would agree, like so many things that shouldn’t work well at least not for very long they seem to do.

a7

Well that is very curious, as I have never seen a mouse wheel that did have an optical encoder.

I just cracked open four in a row - a Dell. a Lenovo (almost identical internal assembly), a "Microsoft" and an innominate wireless mouse which was the most recent that I have purchased - all the others are quite old and second hand - but clearly good quality and sold specifically with business PCs.

All these have the same mechanical encoder with just three pins.

The mechanical encoder is tiny - it would be difficult to fit an optical encoder in the wheel assembly. :face_with_raised_eyebrow:

OK, a bit of research on Duck2Go indicates that some of the necessarily larger mice do use a rather cumbersome optical encoder on the wheel. :grin:

However you look at it, the optical encoder parts are not a commonly advertised item, while the mechanical mouse wheel encoders are legion. :astonished:

This amazes me. I, too, have never found a mouse with a mechanical encoder for the wheel! All of the ones I've opened use optical encoders. I tend to hammer my mice and open them up for two purposes: to remove fluff from the wheel encoder, and to replace the microswitches (which, on Microsoft mice but not Logitech mice, go noisy and start doing double clicks).

Finding out that some mice use mechanical encoders is an eye-opener for me! It's probably because all the mice I buy are large ones.

This sounded familiar, I found

which is a start.

A deadband at the mid position will be helpful essential.

Googling variations of

 joystick deadband equation map accelerator brake 

turns up a crap-ton of good stuff. It’s a nice problem. I am sure ppl here will fall all over themselves proposing nifty and personal approaches.

One could also make this rate orientated - if the value is integrated and becomes the speed at which the pointer moves on the screen, you could “fly” the cursor around like the rocket in Asteroids. :wink:

a7

1 Like

Which says it all! :sunglasses:

Were you able to check out my code? If you need it again her it is:

#include <Mouse.h>

int scrollPin = A0;
int scrollValue;
int scrollZero;
const int sensitivity = 100;

void setup() {
Serial.begin(9600);

pinMode(scrollPin,INPUT);
delay(1000);
scrollZero = 511;
}

void loop() {
Serial.println(scrollValue);
scrollValue = analogRead(scrollPin) - scrollZero;
if(scrollValue != 0);
Mouse.move(0,0, scrollValue/sensitivity);

}

If you wait long enough, someone will be unable to resist the urge to write this code for you.

In the meantime, did you try googling with variations the search items I mentioned? There are many many examples and so forth… you could learn something!

a7

I'm just now learning what deadband means so I could be wrong on this, but... I have a "midpoint" setup at 511 (of 1023), which allows me to scroll down AND up, if I make the midpoint set to 0 then I would only be able to scroll up. For whatever reason, there seems to be this space from -510 to 510 where nothing happens, but as soon as I go to -511 or 511, things start scrolling. I'm assuming that the -510 to 510 is the deadband space. That explanation seemed kind of confusing so if you need me to, I can take a video with the serial monitor up. I'm not really sure how I can incorporate a deadband into what I'm wanting (when I stop moving the pot, scrolling stops and then continues when I move the pot)