@OP
You may practice this tutorial to see how Master and Slave exchanges data using I2C Bus under interactive session and then spend time to correct your codes which we don't see.
A: Let us assume that:
(1) MEGA (Master) will send command 0x01 to Slave-UNO when K1 is pressed; in response, the Slave will send content of PINB Register to Master.
(2) MEGA (Master) will send command 0x02 to Slave-UNO when K2 is pressed; in response, the Slave will send content of PINC Register to Master.
(3) MEGA (Master) will send command 0x03 to Slave-UNO when K3 is pressed; in response, the Slave will send content of PIND Register to Master.
B: Tentative I2C Bus Connection Diagram between MEGA and UNO.

C: Master-MEGA Codes:
#include<Wire.h>
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(2) == LOW)
{
//--requesting Slave to send content of PBR-Register--
Wire.beginTransmission(0x23); //I2C Address of Slve (UNO2)
Wire.write(0x01); //command code for PIB Register
Wire.endTransmission();
//---reading data (from FIFO Buffer) that is to come from Slave--
Wire.requestFrom(0x23, 1); //1-byte data being reqiested
byte x = Wire.read(); //x contains the value of PINB sent by UNO2
Serial.print("Getting PINB value from Slave : ");
Serial.println(x, BIN); //Serial Monitor shows PINB values of Slave
}
if (digitalRead(3) == LOW)
{
//--requesting Slave to send content of PBR-Register--
Wire.beginTransmission(0x23); //I2C Address of Slve (UNO2)
Wire.write(0x02); //command code for PIB Register
Wire.endTransmission();
//---reading data (from FIFO Buffer) that is to come from Slave--
Wire.requestFrom(0x23, 1); //1-byte data being reqiested
byte x = Wire.read(); //x contains the value of PINC sent by UNO2
Serial.print("Getting PINC value from Slave : ");
Serial.println(x, BIN); //Serial Monitor shows PINB values of Slave
}
if (digitalRead(4) == LOW)
{
//--requesting Slave to send content of PBR-Register--
Wire.beginTransmission(0x23); //I2C Address of Slve (UNO2)
Wire.write(0x03); //command code for PIB Register
Wire.endTransmission();
//---reading data (from FIFO Buffer) that is to come from Slave--
Wire.requestFrom(0x23, 1); //1-byte data being reqiested
byte x = Wire.read(); //x contains the value of PIND sent by UNO2
Serial.print("Getting PIND value from Slave : ");
Serial.println(x, BIN); //Serial Monitor shows PINB values of Slave
}
}
D: Slave-UNO Codes:
#include<Wire.h>
bool flag1 = LOW;
bool flag2 = LOW;
bool flag3 = LOW;
void setup()
{
Serial.begin(9600);
Wire.begin(0x23); //Slave's I2C Address
DDRB = 0x00; //Port-B direction as input
DDRC = 0x00;
DDRD = 0x00;
Wire.onReceive(receiveEvent);
Wire.onRequest(sendEvent);
}
void loop()
{
}
void receiveEvent(int howMany)
{
byte x = Wire.read();
if (x == 0x01)
{
flag1 = HIGH; //command received to send Port-B (input = PINB) content
}
if (x == 0x02)
{
flag2 = HIGH; //command received to send Port-B (input = PINB) content
}
if (x == 0x03)
{
flag3 = HIGH; //command received to send Port-B (input = PINB) content
}
}
void sendEvent(int howMany)
{
if (flag1 == HIGH)
{
Wire.write(PINB); //sending PINB value of Port-B Register
flag1 = LOW;
}
if (flag2 == HIGH)
{
Wire.write(PINC); //sending PINB value of Port-B Register
flag2 = LOW;
}
if (flag3 == HIGH)
{
Wire.write(PIND); //sending PINB value of Port-B Register
flag3 = LOW;
}
}
E: Operating Procedures:
(1) Build the circuit as per diagram of Step-B.
(2) Upload Master codes of Step-C.
(3) Upload Slave Codes of Step-D.
(4) Using jumpers engage Logic level at DPin-8 to 13 (PB0 - PB5) of Slave.
(5) Press and hold reset buttons of both Arduinos.
(6) Bring in Serial Monitor of Master at 9600 Bd.
(7) Release reset button of Slave.
(8) Release reset button of Master.
(9) Press button K1.
(10) Check that the content of PINB Register of Slave has correctly appeared on Serial Monitor of Master.
(11) Using jumpers engage Logic level at DPin-A0 to A3 (PC0 - PC3) of Slave.
(12) Press button K2.
(13) Check that the content of PINC0-PINC3 Register of Slave has correctly appeared on Serial Monitor of Master.
(14) Using jumpers engage Logic level at DPin-2 to 7 (PD2 - PD7) of Slave.
(15) Press button K3.
(16) Check that the content of PIND2-PIND7 Register of Slave has correctly appeared on Serial Monitor of Master.


