Scroll and Cursor with Arduino Leonardo - Help

Hello!
I am a newbie trying to make a project for my university.
I am trying to use a joystick module to Scroll and use as a cursor for my Laptop.
Essentially I want the joystick to function in a way that the joystick to have two modes.
One mode to use as a cursor like a mouse (i don't want it to have the mouse_left click, I plan to use a seperate button for that).
The other mode to scroll pages up or down by moving joystick.

I want to implement it in a way that the joystick behaves as a cursor by default. But if the joystick it pressed (and released) it then starts to scroll the page. If it is pressed again, it begins to behave as a cursor once again.

#include <Mouse.h>
int horzPin = A0;  // Analog output of horizontal joystick pin
int vertPin = A1;  // Analog output of vertical joystick pin
int selPin = 9;  // select button pin of joystick

int vertZero, horzZero;  // Stores the initial value of each axis, usually around 512
int vertValue, horzValue;  // Stores current analog output of each axis
const int sensitivity = 400;  // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag = 0;
int scroll_switch = 0;
bool commandSent = false;

//int invertMouse = 1;        //Invert joystick based on orientation
int invertMouse = -1;         //Noninverted joystick based on orientation

void setup()
{
  pinMode(horzPin, INPUT);  // Set both analog pins as inputs
  pinMode(vertPin, INPUT);
  pinMode(selPin, INPUT);  // set button select pin as input
  digitalWrite(selPin, HIGH);  // Pull button select pin high
  delay(1000);  // short delay to let outputs settle
  vertZero = analogRead(vertPin);  // get the initial values
  horzZero = analogRead(horzPin);  // Joystick should be in neutral position when reading these

  Mouse.begin(); //Init mouse emulation
}

void loop()
{
  vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
  horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset

  if ((digitalRead(selPin) == 0) && (!mouseClickFlag))  // if the joystick button is pressed
  {
    mouseClickFlag = 1;
    scroll_switch += 1;
    // click the left button down
  }

  if (scroll_switch % 2 == 0){
    if (vertValue != 0){
      Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0);
      } // move mouse on y axis
    if (horzValue != 0){
      Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis
      }
  }

  else if (scroll_switch % 2 != 0){
    if (!commandSent){
      if (vertValue != 0){
        Mouse.move(0, 0, 1); // move mouse on y axis
        delay(200);
        commandSent = true;
      }
    }
    else if (!commandSent){
      if (horzValue != 0){
        Mouse.move(0, 0, -1);
        delay(200);
        commandSent = true;
        }
    } // move mouse on x axis
  }
  
}

Here is my code.
Please help.
Thank you

I guess your code isn't doing what you expect. But you didn't tell us what the difference was.

You might also have to explain what "scroll pages up and down" means. The mouse itself doesn't have such a feature. If you mean a feature of your OS you might have to find out which mouse input triggers that feature.

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