I'm using I2C and the Arduino Library (Wire) to communicate between two Arduinos, and my code isn't working. It's supposed to read in an int from the slave, write a value to the slave, then read back that value. I'm expecting "0 4", but i keep getting "0 0". Does this mean that the slave isn't registering the write?
Master code:
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
ffsis();
}
void ffsis()
{
//request i
Wire.requestFrom(2, 1);
int i = Wire.read();
Serial.print(i);
//send n seed
Wire.beginTransmission(2);
Wire.write(12);
Wire.endTransmission();
Serial.print(" ");
//request n
Wire.requestFrom(2, 1);
int n = Wire.read();
Serial.print(n);
}
void loop()
{
delay(100);
}
Slave code:
#include <Wire.h>
void setup()
{
Wire.begin(2);
Wire.onRequest(requestEvents);
Wire.onReceive(receiveEvents);
}
void loop(){}
int n = 0;
void requestEvents()
{
Wire.write(n);
}
void receiveEvents(int howMany)
{
int n = wire.read();
}