The following code works correctly in reading single int values. It also works correctly for 3-4 seconds if I 'stream' the values in constantly (Mouse move coordinates) before I get an IO error and the i2C bus connection is dropped. I assume this is some sort of buffer overrun...or? Can anyone get me on the right track here? What changes do I need to make so that I can constantly stream in serial data?
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
int zeroByte;
int state = 0;
int receivedValue = 0;
int numBytes = 0;
bool newData = false;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // start serial for output
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
Serial.println("Ready!");
}
void loop() {
delay(100);
if(newData){
Serial.println(receivedValue);
newData=false;
}
}
// callback for received data
void receiveData(int byteCount){
while(Wire.available()) {
newData = true;
zeroByte = Wire.read();
if(Wire.available() == 2)
{
receivedValue = Wire.read() | Wire.read() << 8;
}
}
}
// callback for sending data
void sendData(){
Wire.write(receivedValue);
}