hi there im using arduino uno + usb host shield and i have taken the readings from the usb mouse in this line of code : x = mi->dX; and the x output is the speed of the mouse x coordinates and i wanna make it thru an analog reading from 0 to 1023 i should do a constrain and a map then i send the data thru i2c comm and recieve them with a slave code and display them but the mouse on the screen is moving always when i give the mouse a left move the mouse moves always to left and when i gave the mouse roght it moves to the right always without stopping the cursor as if im giving it a speed direction + or - .

this is the master code :

#include <hidboot.h>
#include <usbhub.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
int x; // mouse x
int y; // mouse y
int y1;
int x1;
int l,r;
int x11,y11;
class MouseRptParser : public MouseReportParser
void OnMouseMove (MOUSEINFO *mi);
void OnLeftButtonUp (MOUSEINFO *mi);
void OnLeftButtonDown (MOUSEINFO *mi);
void OnRightButtonUp (MOUSEINFO *mi);
void OnRightButtonDown (MOUSEINFO *mi);
void OnMiddleButtonUp (MOUSEINFO *mi);
void OnMiddleButtonDown (MOUSEINFO *mi);
void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
x =  mi->dX;
//x = constrain(x, 0, 1023);


y =  mi->dY;
//y = constrain(y, 0, 1023);


void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
Serial.println("L Butt Up");
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
Serial.println("L Butt Dn");
void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
Serial.println("R Butt Up");
void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
Serial.println("R Butt Dn");
void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
Serial.println("M Butt Up");
void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
Serial.println("M Butt Dn");
x = 90;
y = 90;

USB Usb;
USBHub Hub(&Usb);

uint32_t next_time;

MouseRptParser Prs;

void setup()

Wire.begin();       //Begins I2C communication at pin (A4,A5)

#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection

if (Usb.Init() == -1)
Serial.println("OSC did not start.");

delay( 200 );

next_time = millis() + 5000;

HidMouse.SetReportParser(0, &Prs);

void loop()


and the output is speed direction of x and y :

0 0 0 1 1 2 3 4 5 66 66 78 99 102 113 22 3 0 and this is the slave code :

int x,y,l,r;
#include <Mouse.h>                                                                               // Analog output of vertical joystick pin

void setup() {
Serial.begin( 115200 ); // start serial for output
Mouse.begin(); //Init mouse emulation 

void receiveEvent( int bytes )

x = Wire.read();
y = Wire.read();
//l = Wire.read();
//r = Wire.read();

void loop() {

Mouse.move(x, y, 0); // move mouse on y axis

anyone could help please i think the problem is i should ask the current position and the next postion and if it was not zero move the mouse otherwise if it is 0 do not move

  • Well, you are constantly sending the X and Y coordinates over and over and over again in loop()...
    – Majenko
    Jun 27, 2020 at 17:53
  • so what to do ? Jun 27, 2020 at 20:35
  • Don't keep sending them? Only send them when you receive them from the mouse?
    – Majenko
    Jun 27, 2020 at 20:38
  • you really need to learn how to format your code properly ... it is not a problem when the code is short, but when the code is more complicated, correct indentation is very important when trying to find missed braces
    – jsotola
    Jun 27, 2020 at 20:56
  • 1
    could you please show me how ? Jun 27, 2020 at 21:02


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.