I am trying to send 2 values (x and y) from an Arduino via I2C and separate them when they are received on a 2nd Arduino. i can receive one value but not both. they are int but can be byte. Are there any examples out there. I have searched, but usually there are 2 I2C sensors, each with an address. my case is where I want to send two separate parameters from one master Arduino to a slave Arduino.
Please upload your sketch (Master and Slave) between [code] ... [/code] tags.
Do you use the 'howMany' parameter, like in the examples by Nick Gammon ?
Hello,
Here is my code. I appreciate your interest / help in this
// Master <<<<<<<
I want to send X and y // I want to send X and y axis data
// wire.write can send a sring but wire.read cannot accept a string so that approach is out
// read mouse on USB host shield on master arduino uno
#include <Wire.h>
byte x;
byte y;
class MouseRptParser : public MouseReportParser
{
protected:
virtual void OnMouseMove (MOUSEINFO *mi);
};
void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{
// mouse data (x,y) print
Serial.print("dx=");
Serial.print(mi->dX, DEC);
Serial.print(" dy=");
Serial.print(mi->dY, DEC);
Serial.print(" yy=");
int X_mouse =(mi->dX); // wan to send x and y data
int Y_mouse =(mi->dY);
Serial.print("X_mouse= ");
Serial.println(X_mouse);
Serial.print("Y_mouse= ");
Serial.println(Y_mouse);
// end of mouse data print
{ Wire.beginTransmission(5); // transmit to device #5
Wire.write(X_mouse); // sends x
delay (200);
Wire.write(Y_mouse); // sends x
Wire.endTransmission(); // stop transmitting
delay (100);
}
};
// mouse USB read functions
USB Usb;
USBHub Hub(&Usb);
HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb);
uint32_t next_time;
MouseRptParser Prs;
void setup()
{
Serial.begin( 115200 );
Serial.println("Start");
if (Usb.Init() == -1)
Serial.println("OSC did not start.");
delay( 100 );
next_time = millis() + 5000;
HidMouse.SetReportParser(0,(HIDReportParser*)&Prs);
Wire.begin(); // Start I2C Bus as Master
}
void loop()
{
Usb.Task();
x=0;
{ Wire.beginTransmission(5); // transmit to device #5
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
delay (100);
}
}
// end of Master <<<<<<<
// start of slave code <<<<<<
// I want to receive sepparte X and Y values
//wire.read will not read a string so that approach is out
#include <Wire.h>
int x;
int y;
void setup() {
Serial.begin( 115200 );
Serial.println("Start");
Wire.begin(5); // Start I2C Bus as a Slave (Device Number 9)
Wire.onReceive(receiveEvent); // register event
}
void loop() {
if (x >0)
{
Serial.println("x=") ;
Serial.println(x);
}
}
void receiveEvent(int howMany) {
x = Wire.read(); // receive byte as an integer
delay (200);
y = Wire.read();
}
//end of slave code <<<<<<<<,
I don't know the class MouseRptParser, so I can't tell if there is a problem with that.
First of all, please make good indents, good comments and so on. This is hard to read.
You use '{' in a few places without any loop. Please remove them.
When using a delay, the Arduino stops working. Only interrupts are handled. You might want to remove a few delays.
In the receiveEvent() functions, you use a delay. That is not good.
Use the parameter 'howMany' to read the amount of bytes that is available. When you only need the first 2 byte, you don't have to read them all.
There is a delay of 100ms in the Master loop. Can you replace that with a millis(). During the 100ms, the Usb.Task() can't run.
Using millis is in the example BlinkWithoutDelay.
Using that without rollover problem is here:
http://playground.arduino.cc/Code/TimingRollover#arithmetic
There are two delays in OnMouseMove(). During an i2c session, you don't need a delay. Remove them both.
In the Slave, you are not printing variable 'y'. So you will not know if it has been received.
You could write code, to see if 'x' and 'y' have changed, and print them if they have.
You need to put the data that you want to send in an array, and send the array in one shot, using Write.write() with two arguments.
Thank you for your detailed recommendations.