HELP! Joystick mouse cursor speed adjusting

Good day everyone . I'm here to ask some assistance. I am trying to build a joystick mouse and keyboard using arduino that will act as a ps/2 device for both. I am using the ps2dev library. I modify a code that I've seen online then I make the mouse work but I want to know how to adjust the cursor speed. Here's my code(this code works on me), If I want to adjust the cursor speed what should I change in this code?

Thanks in advance. :slight_smile:

#include "ps2dev.h"

PS2dev mouse(3,2); // 2 data 3clock

char buttons[3] = {0,0,0};

int delta_x = 0;
int delta_y = 0;
//we start off not enabled
int enabled =0;

//ack a host command

int buttonState = 0;         // variable for reading the pushbutton status
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value


const byte leftClick = 8; //assign the pins for the buttons
const byte rightClick = 9;

int delayTime = 8 ; //so the serial output is more readable


void ack() {
  while(mouse.write(0xFA));
}

void write_packet() {
  char overflowx =0;
  char overflowy =0;
  char data[3];
  int x,y;
  
  if (delta_x > 255) {
    overflowx =1;
    x=255;
  }
  if (delta_x < -255) {
    overflowx = 1;
    x=-255;
  }  
  if (delta_y > 255) {
    overflowy =1;
    y=255;
  }
  if (delta_y < -255) {
    overflowy = 1;
    y=-255;
  }
  
  data[0] = ((overflowy & 1) << 7) |
    ( (overflowx & 1) << 6) |
    ( (((delta_y &0x100)>>8) & 1) << 5) |
    ( ( ((delta_x &0x100)>>8)& 1) << 4) |
    ( ( 1) << 3) |
    ( ( buttons[1] & 1) << 2) |
    ( ( buttons[2] & 1) << 1) |
    ( ( buttons[0] & 1) << 0) ;
    
  data[1] = delta_x & 0xff;
  data[2] = delta_y & 0xff;
  
  mouse.write(data[0]);
  mouse.write(data[1]);

  mouse.write(data[2]);

  delta_x = 0;
  delta_y = 0;
}

int mousecommand(int command) {
  unsigned char val;

  //This implements enough mouse commands to get by, most of them are
  //just acked without really doing anything

  switch (command) {
  case 0xFF: //reset
    ack();
    //the while loop lets us wait for the host to be ready
    while(mouse.write(0xAA)!=0);  
    while(mouse.write(0x00)!=0);
  
    break;
  case 0xFE: //resend
    ack();
    break;
  case 0xF6: //set defaults 
    //enter stream mode   
    ack();
    break;
  case 0xF5:  //disable data reporting
    //FM
    ack();
    break;
  case 0xF4: //enable data reporting
    //FM
    enabled = 1;
    ack();
    break;
  case 0xF3: //set sample rate
    ack();
    mouse.read(&val); // for now drop the new rate on the floor
    //      Serial.println(val,HEX);
    ack();
    break;
  case 0xF2: //get device id
    ack();
    mouse.write(00);
    break;
  case 0xF0: //set remote mode 
    ack();  
    break;
  case 0xEE: //set wrap mode
    ack();
    break;
  case 0xEC: //reset wrap mode
    ack();
    break;
  case 0xEB: //read data
    ack();
    write_packet();
    break;
  case 0xEA: //set stream mode
    ack();
    break;
  case 0xE9: //status request
    ack();
    //      send_status();
    break;
  case 0xE8: //set resolution
    ack();
    mouse.read(&val);
    //    Serial.println(val,HEX);
    ack();
    break;
  case 0xE7: //set scaling 2:1
    ack();
    break;
  case 0xE6: //set scaling 1:1
    ack();
    break;

  } 
  
}

int xcenter ;
int ycenter;

int xsum = 0;
int ysum = 0;


void setup(){
  unsigned char val;
 
  // send the mouse start up
  while(mouse.write(0xAA)!=0);  
  //while(mouse.write(0x00)!=0);
  delay(10);
  
pinMode(leftClick, INPUT); //set button pins to input
pinMode(rightClick, INPUT);


}

void loop(){
  
  
   int xReading = readAxis(A0);
  int yReading = readAxis(A1);
  unsigned char  c;
  if( (digitalRead(3)==LOW) || (digitalRead(2) == LOW)) {
    while(mouse.read(&c)) ;
    mousecommand(c);
  } 
 

  else //if (enabled) 
  {
   
    delta_x = xReading;
    delta_y = yReading;
    write_packet()  ;
  }
  
    buttonState = digitalRead(rightClick);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:  
    buttons[0] = 1;
    delta_x = xReading;
    delta_y = yReading;
    write_packet()  ;
  } 
  else {
     buttons[0] = 0; 
     write_packet()  ;
 }

delay(delayTime);
}
 
int readAxis(int thisAxis) {
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, range);

  // if the output reading is outside from the
  // rest position threshold,  use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  }

  // return the distance for this axis:
  return distance;
  
}

3 days have passed and still no reply huh.,

good news I figure out how adjust the speed of the mouse cursor. Just need to change the value of the "range" that is declared above. the higher the value the faster the cursor moves.

well, problem solved. hope this gives help to others that needs it.

1 Like

Most people are using the USB HID for mouse/keyboard/joystick interface.

GoForSmoke:
Most people are using the USB HID for mouse/keyboard/joystick interface.

yes I know. but still my question is just about the speeding the mouse cursor.