I2C bus - sending 2 numbers higher than 255

I would like to send 2 number higher than 255 between Master and Slave using the I2C bus.
Send 1 number is working perfect.

I hope for some help to change the code. I think the problem is the slave code.

I am using Arduino Uno (2 of course)

// Slave com 7
#include <Wire.h>
int VinL;
int VinR;
byte dataSend[5];
 
void setup(){
Wire.begin(10);
Wire.onRequest(requestEvent);
}
void loop(){
   VinL = 1020;
   VinR = 980;
  
delay(100);
}
 
void requestEvent(){
dataSend[0] = (VinL >> 8) & 0xFF;
dataSend[1] = VinL & 0xFF;
dataSend[2] = (VinR >> 8) & (0xFF * 2);
dataSend[3] = VinR & (0xFF * 2);
dataSend[4] = 2;

Wire.write(dataSend,3);
}

This is the master code

// Master - com 6
#include <Wire.h>
volatile byte dataGet[5];
int VinL;
int VinR;
 
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
 delay(2000);

 hentData();
 Serial.println(VinL);
 Serial.println(VinR);
 Serial.println(dataGet[4]);
 Serial.print("\n");
}

void hentData(){  
   Wire.requestFrom(10, 5);                             // Request data
   for (byte i=0; i<5; i++){dataGet[i] = Wire.read();}  // Reading data

   VinL = dataGet[0];VinL = VinL << 8 | dataGet[1];
   VinR = dataGet[2];VinR = VinR << 8 | dataGet[3];
}

These lines are wrong:

dataSend[2] = (VinR >> 8) & (0xFF * 2);
dataSend[3] = VinR & (0xFF * 2);

You just need to do exactly like the first number

dataSend[2] = (VinR >> 8) & 0xFF;
dataSend[3] = VinR & 0xFF;

It may be easier to use a structure

Screen shot:
smData.png

Master Sketch (NANO):

// Master - com 6
#include <Wire.h>
byte dataGet[5];
int VinL;
int VinR;

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

  hentData();
  Serial.println(VinL);
  Serial.println(VinR);
  Serial.println(dataGet[4]);  //should show 2
  Serial.println();//("\n");
}

void hentData()
{
  Wire.requestFrom(10, 5);                             // Request data
  for (byte i = 0; i < 5; i++)
  {
    dataGet[i] = Wire.read(); // Reading data higher byte first
  }

  VinL = dataGet[0] << 8 | dataGet[1];
  //VinL = VinL << 8 | dataGet[1];
  VinR = dataGet[2] << 8 | dataGet[3]; // VinR = VinR << 8 | dataGet[3];

}

Slave Sketch (MEGA):

// Slave com 7
#include <Wire.h>
int VinL;
int VinR;
byte dataSend[5];

void setup() 
{
  Wire.begin(10);
  Wire.onRequest(requestEvent);
}
void loop() 
{
  VinL = 1020;  //03FC
  VinR = 980; //03D4

  delay(100);
}

void requestEvent() 
{
  dataSend[0] = highByte(VinL); //sending higher byte first (VinL >> 8) & 0xFF;
  dataSend[1] = lowByte(VinL);//VinL & 0xFF;
  dataSend[2] = highByte(VinR);//sending high byte first(VinR >> 8) & (0xFF * 2);
  dataSend[3] = lowByte(VinR);//VinR & (0xFF * 2);
  dataSend[4] = 0x02;

  Wire.write(dataSend, sizeof(dataSend));//3);
}

smData.png

I am very happy to have a reply fast and a fully working sketch.

The suggestion are working perfect.

Thank you very much GolamMostafa and blh64

Perhaps a small note regarding the application for the solution.

I am working on a soundcard interface for measuring specifications on audio amplifiers.

The Arduino part.

Arduino ATtiny84 (master and slave)
Measuring 4 analog inputs (2 channels input and 2 channels output)
and sending these information to Arduino Nano.
ATtiny 84 receives 6 bytes of switch positions from the Nano.

Arduino Nano (Master and slave)
Monitoring 6 switches and send the information to ATtiny 84 which then control switching relays.
Receiving the 4 analog measured inputs and outputs
The mesured data are displayed on 2 OLED displays (one for input and one for output)
The display also show the settings of the switches.

The I2C databus handle all the communication and saves a lot of cables :slight_smile:
...... and the project is a nice challange.