Hey guys, this is my first question, so please bare with me if I'm doing something slightly wrong. I am currently making a GPS Waypoint Robot controller and have done so with two arduino nanos, a 9 axis IMU, and a BN880 module. I am using one arduino to decode GPS signals and compute the required heading and am currently sending it over software serial to the other arduino. It works, but it is incredibly slow and is already wired to work over i2c. Currently, I am working on getting the i2c communication to work, but consistently run into the problem of good communication for a couple seconds which is followed by the entire program not working. Here are the bits of code I am using to communicate over the Wire library:
// WIRE VARIABLE INITIALIZATION
union float_byte {
float value;
byte b[4];
};
union short_byte {
short value;
byte b[4];
};
union float_byte distanceTo;
union short_byte headingReq;
union float_byte currentLat;
union float_byte currentLon;
union short_byte currentSpeed;
// WIRE TEST END
void loop() {
...
distanceTo.value = dist;
headingReq.value = int(h);
currentLat.value = gps.location.lat();
currentLon.value = gps.location.lng();
currentSpeed.value = int(gps.speed.mph());
// 4 byte, 1 byte, 4 byte, 1 byte (end)
Wire.beginTransmission(4); //address is queued for checking if the slave is present
for (int i=0; i<4; i++)
{
Wire.write(distanceTo.b[i]); //data bytes are queued in local buffer
}
for (int i=0; i<4; i++)
{
Wire.write(headingReq.b[i]); //data bytes are queued in local buffer
}
Wire.endTransmission();
...
}
------------- on receiving end (where the problems lie) ----------
void receiveEvent(int howMany) {
Serial.println(howMany);
wireCount = 0;
while(4 < Wire.available() && wireCount <= 3) // loop through all but the last
{
distanceTo.b[wireCount] = Wire.read(); // receive byte as a character
wireCount++;
}
wireCount = 0;
while(0 < Wire.available() && wireCount <= 3) // loop through all but the last
{
headingReq.b[wireCount] = Wire.read(); // receive byte as a character
wireCount++;
}
//Serial.println("Dist: " + String(distanceTo.value) + "\tHeading: " + String(headingReq.value));
}