I am using an ATTiny85 running the TinyWire library to communicate with an Arduino Uno running the Wire library, from an I2C connection. I can transmit one byte at a time perfectly fine for as many requests as I want, however a problem arises when I try and send two bytes at a time. Below is the code I am using (note - I am using a popular modified version of the TinyWire library which has the OnRequest method implemented). Here's my code for the slave:
#include "TinyWireS.h" // wrapper class for I2C slave routines
#define I2C_SLAVE_ADDR 0x27 // i2c slave address
void setup()
{
TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode
TinyWireS.onRequest(requestEvent);
}
void loop()
{
}
void requestEvent()
{
int16_t bigNum = analogRead(3);
byte myArray[2];
myArray[0] = (bigNum >> 8) & 0xFF;
myArray[1] = bigNum & 0xFF;
TinyWireS.send(myArray[0]);
TinyWireS.send(myArray[1]);
}
and for the master:
#include <Wire.h>
#define DEVICE_2 0x27
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
delay(2000);
int16_t bigNum;
byte a,b;
// read 2 bytes, from address 0x27
Serial.println("Request Start");
Wire.requestFrom(DEVICE_2, 2);
a = Wire.read();
b = Wire.read();
bigNum = a;
bigNum = bigNum << 8 | b;
Serial.print(bigNum);
Serial.print("\n");
}
Regardless of the delay time, I can always only get exactly 7 requests. I've tried many values from no delay to 5 second delays in between calls. If I power off the ATTiny, then supply power again while the Uno is still connected to the Serial port, I can get 7 more requests before stopping again. The Serial monitor always shows that the Uno's main loop somehow gets paused directly after calling requestFrom(), which makes it look to me like it's waiting for something, but I can't figure out what. When I unplug the ATTiny, the Uno prints to the Serial port -28412. I have also tried putting the following before reading from the buffer:
if(Wire.available() > 0) {
a = Wire.read();
b = Wire.read();
}
Thanks for your help.