I have an Arduino Mega2560 and a Raspberry Pi hooked up through I2C. The Pi is acting as the master, while the Arduino is the slave. While sending data over I2C, the int message gets messed up and will either send the address or the data. Then I can't even receive any useful data from the Arduino.
So if I turn the receiving function off it sends the data just fine, but when I ask the Pi to start receiving data again it slows down the entire bus and data starts conflicting between the Pi and Arduino. Also it only happens with the Arduino. Every other device works flawlessly.
Here's what my code looks like on the Pi side. I'm using SMBus to send and receive bytes. I didn't bother to put up code for it because I don't have the source code for SMBus. I just trust that they work because my other I2C devices work without a hitch.
while True:
#if alternating == true then get the imu if not then get the motor speed
if alternating == True:
try: #need the try because an error will stop the program completely
ypr = IMU.getYPR()
yaw = int(ypr['yaw'] * 180 / math.pi)
savedYaw = yaw
except:
yaw = savedYaw
#print "exception"
pass
#write yaw to arduino
i2c.writeByte(yaw)
alternating = False
else:
#start reading data from arduino
#readArduino()
alternating = True
pass
Then on the Arduino side these are my onReceive
and onRequest
functions:
void readPI(int byteC) {
if(Wire.available()) {
reference = Wire.read();
}
pid1.getReference(reference);
pid2.getReference(reference);
}
void sendPI() {
int values[] = {(int)out, (int)outReversed };
Wire.write(values[count]);
count++;
if(count > 1) {
count = 0;
}
}
Is there something about the Arduino that I don't know? Maybe it's a timing issue? Is there a way to set up registers? I can either receive or send, but I can't do both, and Googling the problem only leads to an example of someone sending 1 - 9 over the Pi. I can't find examples of anyone actually doing something complicated with an Arduino and a Rasp Pi over I2C. Any and all help would be amazing!