Attiny85 I2C 2 bytes transmission synching

Hello,
I’ve set up an attiny85 as slave communicating with Arduino Uno R3 as Master
I’m using TinyWireS library for the slave and Wire library for the Master.

I need to transfer from the slave upon Master request 10bits data, so that I setup a 2 bytes I2C connection.
What I achieved is for the most working fine. However sometime, if i insist in resetting the Arduino it apparently lose the synch in terms of what is the first and the second byte.

As an example if I transfer the value 1023 which in bits is 00000011 11111111, sometime it happens that instead of receiving on the Master 1023 i get -253 which in fact is 11111111 00000011.

Below my code:

Slave:

#include "TinyWireS.h"                  // wrapper class for I2C slave routines
#define I2C_SLAVE_ADDR  0x26            // i2c slave address (38)

int t=1023;                                 // value for testing purpose

volatile byte reg_position;

void setup()
{ 
   TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode
   TinyWireS.onRequest(requestEvent);
}

void loop()
{
  TinyWireS_stop_check();
}

void requestEvent()
{  
  byte b[2];
  integerToBytes(t, b);
  TinyWireS.send(b[reg_position]);
  
  reg_position++;
  
  if(reg_position > 1)
  {
    reg_position = 0;
  }
}

void integerToBytes(int val, byte b[2]) 
{
  b[0] = (byte )((val >> 8) & 0xff);
  b[1] = (byte )(val & 0xff);
}

Master:

#include <Wire.h>
#define I2C_SLAVE_ADDR  0x26            // i2c slave address (38)

byte b[2];
bool isTouchCh1;

void setup() 
{
  Wire.begin();
  Serial.begin(9600);
}

void loop() 
{
  // read 1 byte, from address 0x26
  Wire.requestFrom(I2C_SLAVE_ADDR, 2);

  while(Wire.available()) 
  {
    b[0] = Wire.read();
    b[1] = Wire.read();
  }
     
  isTouchCh1 = getCapacitiveStatus(b[0]);
  b[0] = stripCapacitiveStatus(b[0]);
     
  int intValue;
  intValue = ((b[0] << 8) + (b[1]));

  Serial.print("Pot Value = ");
  Serial.println(intValue);
      
  Serial.print("Touch Value Ch1 = ");
  Serial.println(isTouchCh1);
}
 
// Get the capacitive status on
// a give channel on the third bit
// basis
// Return true if is touch
bool getCapacitiveStatus(byte b)
{
  if(bitRead(b, 2) == 1) return true;
  if(bitRead(b, 2) == 0) return false;
}
 
// Strip out the third bit if value
// is 1 setting value 0
byte stripCapacitiveStatus(byte b)
{
  bool isTouch;
   
  if(bitRead(b, 2) == 1) bitClear(b, 2);
   
  return b;
}

My question: is there a way to secure the synch of the lb and hb bytes or it is just me insisting too much with resetting??

Thanks a lot in advance for any kind advise,

Filo

As with all communication, "handshaking" is key. I suggest implementing an integrity check to make sure the data was in fact received correctly. Not only will this check to make sure the data was received but also make sure the info it received is accurate. I use checksum...

if ( hexCalculatedChecksum == hexChecksum )
        {
          Serial.println("Calculated checksum matched transmitted checksum.");
        } 
        else {
          Serial.println("Calculated checksum didn't match transmitted checksum. Corrupt data!");
        }

It is also good practice when troubleshooting to use Serial.flush().