I have connected 2 arduinos to send I2C data to one another for a clock project, and I have one of them driving the 4 digit 7 segment display. I want to make a function for the master one (the one without a display) to send a single integer to the other one. Here is the sending function that I have so far:
void display(int data) {
char dataOut[4];
dataOut[3] = (data >> 24) & 0xFF;
dataOut[2] = (data >> 16) & 0xFF;
dataOut[1] = (data >> 8) & 0xFF;
dataOut[0] = data & 0xFF;
Wire.beginTransmission(8);
Wire.write(dataOut, 4);
Wire.endTransmission();
}
I have tried converting the integer into a 4 byte array and sending that object using Wire.write(), and on the receiver end, I keep ending up with zeros. Here is the receiver-end function:
void receiveEvent(int bytes) {
int data;
while(Wire.available() > 0) {
data += Wire.read();
data = (data << 8);
}
Serial.println(data); //send to serial monitor for testing purposes
}
It should also help to notice that I am a noob with bit shifting, and these are just some things I found in some forums and I tried to combine my knowledge. Sorry if it's just a simple mistake.