I am working with IIC in order to hopefully fix a project. I've uploaded the IIC master and slave examples and they both worked flawelessly.
Now when running my own code, everything turns into rubbish.
I am using an Atmega 328P on a breadboard however, that's the only difference between the master and slave examples.
The (partial) code for the slave is
void setup() { Wire.begin(8); Wire.onReceive(onReceive); Wire.onRequest(onRequest); } void onReceive(int byteLength) { opCode = Wire.read(); switch (opCode) { case 0x01: // Initialize the serial port { Serial.begin(9600); statusCode = SENSOR_INITIALIZED; break; } case 0x11: { Serial.end(); statusCode = SENSOR_NOT_INITIALIZED; break; } } void onRequest() { switch (opCode) { case 0x10: { Wire.write(statusCode); break; } } }
The code for the master is
void setup() { Wire.begin(); Serial.begin(115200); Serial.println("Requesting status code"); // Ask for the status code Wire.beginTransmission(IIC_ADDR); Wire.write(0x10); Wire.endTransmission(); Wire.requestFrom(IIC_ADDR, 1); Serial.print("Status code: "); Serial.println(Wire.read()); }
Edit:
I think it might have something to do with not compiling for the Atmega 328 with internal 8Mhz clock