How to change scroll speed of mouse.move()

I am able to simulate moving the scroll wheel on a mouse using an Arduino Pro Micro by using mouse.move(0,0,1)
or mouse.move(0,0,-1) for scroll down.

But how can I make it scroll with finer resolution? 1 or -1 acts as if I am scrolling several times with my actual mouse.

I tried mouse.move(0,0,0.1), but this did not respond, as I am assuming it can't handle floats.

1 Like

the doc states it's a char (-128... 127) and it's the amount to move scroll wheel.

are you sure you are not in the loop and sending it multiple times? post your code

1 Like
#include "Mouse.h"

const int L1Pin = 6; //button with pullup resistor to VCC (pressed is GND)
int commandSent = 0;

void setup() {
    pinMode(L1Pin, INPUT);
    Mouse.begin();
}

void loop() {
  if (digitalRead(L1Pin) == LOW && commandSent == 0) {
    commandSent = 1;
    Mouse.move(0, 0, 1);
  }
  else{
    commandSent = 0;
  }
delay(2);
}

Here is the code, I am fairly certain it is only scrolling once.

1 Like

Ahh nevermind I found the issue - Sorry!
Should be another if statement, not else:

#include "Mouse.h"

const int L1Pin = 6; //button with pullup resistor to VCC (pressed is GND)
int commandSent = 0;

void setup() {
    pinMode(L1Pin, INPUT);
    Mouse.begin();
}

void loop() {
  if (digitalRead(L1Pin) == LOW && commandSent == 0) {
    commandSent = 1;
    Mouse.move(0, 0, 1);
  }
  if (digitalRead(L1Pin) == HIGH){
    commandSent = 0;
  }
delay(2);
}
1 Like

I am fairly certain it is only scrolling once.

have a closer look
(remember the loop spins really fast - so when you press your button even for a short while, you'll probably do a few tens or hundreds loops() - and would be more if you did not have the 2ms delay)

I was typing as you modified your answer.

you could use the INPUT_PULLUP mode to avoid having the external resistor and it's easier to separate conditions sometimes to know exactly what an else means. if you are unlucky, your two digitalRead() won't be the same thing if you catch a bounce for example.

Code could look like this

#include "Mouse.h"

const byte buttonPin = 6;
bool commandSent = false;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Mouse.begin();
}

void loop() {
  if (digitalRead(buttonPin) == LOW) { // button pressed
    if (!commandSent) { // is it a new press? 
      Mouse.move(0, 0, 1);
      delay(20); // poor's man anti-boucing
      commandSent = true;
    }
  } else { // button not pressed
    commandSent = false;
  }
}
1 Like

Great, thank you!

1 Like

note the delay(20); // poor's man anti-boucingit's usually enough to catch most buttons' bouncing. 2ms might be a bit short

(and ideally you would want also to catch the bouncing when you release the button)

1 Like

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