I changed the unsigned char for a char and it now work for negative number. The only downside is that we loose a bit for the value itself. Here are the final codes if anyone needs it :
SIGNED 6 bit Receiver
#include <Wire.h>
// MAX INT -16382 to 16382
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
int output = 0;
void loop()
{
Wire.requestFrom(2, 1); // the first byte
while(Wire.available())
{
char received = Wire.receive();
output = received;
}
for (int i = 0 ; i < 3 ; i++) // next 3 bytes
{
Wire.requestFrom(2, 1);
while(Wire.available())
{
char received = Wire.receive();
output |= (received << 8);
}
}
Serial.print(output);
Serial.println(".");
delay(500);
}
SIGNED 6 bit Sender :
#include <Wire.h>
// MAX INT -16382 to 16382
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestInt); // register event
}
int byteSending = 1;
int toTransfer = 32766;
int Shift = toTransfer;
int mask = 0xFF;
char toSend = 0;
void loop()
{
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestInt()
{
if (byteSending == 1) //send packet 1
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 2;
}
else if (byteSending == 2) //send packet 2
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 3;
}
else if (byteSending == 3) //send packet 3
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 4;
}
else if (byteSending == 4) //send packet 4
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 1;
//initialization for next turn
Shift = toTransfer;
mask = 0xFF;
toSend = 0;
}
}
UNSIGNED 7 bit Sender :
#include <Wire.h>
// MAX INT 32767
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestInt); // register event
}
int byteSending = 1;
int toTransfer = 32767;
int Shift = toTransfer;
int mask = 0xFF;
unsigned char toSend = 0;
void loop()
{
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestInt()
{
if (byteSending == 1) //send packet 1
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 2;
}
else if (byteSending == 2) //send packet 2
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 3;
}
else if (byteSending == 3) //send packet 3
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 4;
}
else if (byteSending == 4) //send packet 4
{
toSend = Shift & mask;
Shift = Shift >> 8;
Wire.send(toSend);
byteSending = 1;
//initialization for next turn
Shift = toTransfer;
mask = 0xFF;
toSend = 0;
}
}
UNSIGNED 7 bit Receiver :
#include <Wire.h>
// MAX INT 32767
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
int output = 0;
void loop()
{
Wire.requestFrom(2, 1); // the first byte
while(Wire.available())
{
unsigned char received = Wire.receive();
output = received;
}
for (int i = 0 ; i < 3 ; i++) // next 3 bytes
{
Wire.requestFrom(2, 1);
while(Wire.available())
{
unsigned char received = Wire.receive();
output |= (received << 8);
}
}
Serial.print(output);
Serial.println(".");
delay(500);
}