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<Wire.h>
#include <hidboot.h>
#include <usbhub.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
int x; // mouse x
int y; // mouse y
int y1;
int x1;
int l,r;
int x11,y11;
class MouseRptParser : public MouseReportParser
{
protected:
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);
Serial.println(x);
y = mi->dY;
//y = constrain(y, 0, 1023);
Serial.println(y);
};
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
{
Serial.println("L Butt Up");
l=0;
};
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
{
Serial.println("L Butt Dn");
l=1;
};
void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
{
Serial.println("R Butt Up");
r=0;
};
void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
{
Serial.println("R Butt Dn");
r=1;
};
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);
HIDBoot<USB_HID_PROTOCOL_MOUSE> HidMouse(&Usb);
uint32_t next_time;
MouseRptParser Prs;
void setup()
{
//Serial.begin(9600);
Wire.begin(); //Begins I2C communication at pin (A4,A5)
Serial.begin(115200);
#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
#endif
Serial.println("Start");
if (Usb.Init() == -1)
Serial.println("OSC did not start.");
delay( 200 );
next_time = millis() + 5000;
HidMouse.SetReportParser(0, &Prs);
}
void loop()
{
Usb.Task();
Wire.beginTransmission(8);
Wire.write(x);
Wire.write(y);
//Wire.write(l);
//Wire.write(r);
Wire.endTransmission();
delay(10);
}
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 :
#include<Wire.h>
int x,y,l,r;
#include <Mouse.h> // Analog output of vertical joystick pin
void setup() {
Serial.begin( 115200 ); // start serial for output
Wire.begin(8);
Wire.onReceive(receiveEvent);
Mouse.begin(); //Init mouse emulation
}
void receiveEvent( int bytes )
{
x = Wire.read();
y = Wire.read();
//l = Wire.read();
//r = Wire.read();
delay(10);
}
void loop() {
Mouse.move(x, y, 0); // move mouse on y axis
delay(10);
}
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