hello guys i have a question about arduino communication.
im using arduino uno + usb host shield + usb mouse and get inf as x and y coordinates and saved it into x and y variables as integer then send them using i2c communication to arduino leonardo but i have a problem that nothing been sent all the output is 0 0 0 0 0 0, the arduino uno is the master and leonardo iis the slave with adresse 8.
connection:
im connecting A4 in uno with d2 leonardo and A5 UNO WITH D3 LEONARDO and gnd uno to gnd leonardo
code master arduino uno:
#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;
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 = (x + mi->dX);
x = constrain(x, 0, 1023);
Serial.println(x);
y = (y + mi->dY);
y = constrain(y, 0, 1023);
Serial.println(y);
};
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
{
Serial.println("L Butt Up");
};
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
{
Serial.println("L Butt Dn");
x = 5;
y = 5;
};
void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
{
Serial.println("R Butt Up");
};
void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
{
Serial.println("R Butt Dn");
x = 175;
y = 175;
};
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.endTransmission();
delay(10);
}
and this is the output :
0
0
1
10
22
23
50
150
1000
1023
0
1023
0
left btn up
left btn dn
1023
and this is the slave leonardo code :
#include<Wire.h>
int x,y;
void setup() {
Serial.begin( 115200 ); // start serial for output
Wire.begin(8);
Wire.onReceive(receiveEvent);
}
void receiveEvent( int bytes )
{
x = Wire.read();
//y = Wire.read();
Wire.endTransmission(); // stop transmitting
}
void loop() {
Serial.println(x);
//Serial.println(y);
}
and this is the output :
0
0
0
0
0
0
0
0
0