i2c send and response 2 arduinos

hi

I am trying to use 2 way communication between 2 arduinos (dont actually have them yet, currently using the tinkercad online simulator)

i want one arduino (master) to send a message to the other one (slave).
I then want the slave to send a response to the master.

i attempted to combine the 2 examples given (https://www.arduino.cc/en/Tutorial/MasterWriter and https://www.arduino.cc/en/Tutorial/MasterReader)

the master sucessfully sends the "x is 0", but the slave responds with "ÿÿÿÿ"

here is my code: i2c communication and response · GitHub

i have a few questions:

  1. is i2c capable of doing what i want?
  2. if the answer to 1 is yes, what am i doing wrong?
  3. if the answer to 1 is no, what would be a better protocol for doing this?

thanks

i attempted to combine the 2 examples given (https://www.arduino.cc/en/Tutorial/MasterWriter and https://www.arduino.cc/en/Tutorial/MasterReader)

Combine them how? You are supposed to run one of them on the master and the other one on the slave. There is nothing to "combine".

here is my code

Strange definition of "here".

  1. is i2c capable of doing what i want?

Yes.

  1. if the answer to 1 is yes, what am i doing wrong?

Trying to use a simulator instead of real hardware, for one thing. Attempting to "combine" things that don't need to be combined may be another. Posting questions in one place and code in another is a another thing you are doing wrong.

PaulS:
Combine them how? You are supposed to run one of them on the master and the other one on the slave. There is nothing to "combine".
Strange definition of "here".
Yes.
Trying to use a simulator instead of real hardware, for one thing. Attempting to "combine" things that don't need to be combined may be another. Posting questions in one place and code in another is a another thing you are doing wrong.

i dont want to combine the master and slave code,
there are 2 sets of master code, and 2 sets of slave code

https://www.arduino.cc/en/Tutorial/MasterWriter
this page has example code for the master to send data to a slave. (there are 2 separate pieces of code for the master and the slave)

https://www.arduino.cc/en/Tutorial/MasterReader
this has example code for the slave to send data to the master. (there are 2 separate pieces of code for the master and the slave)

this page has my attempt to make the master send data to a slave, and then that slave sends data to the master. (there are STILL 2 separate pieces of code for the master and the slave)
i dont want to combine the master and the slave code, just the 2 separate pieces of master code and the 2 separate pieces of slave code

most other sites that i have been on prefer you to use pastebin or github gist to post code so that it isnt clogging up the page.

When i comment out the part of the master code for sending data to the slave, the master receives the message from the slave properly.
when it isnt commented out, all the master outputs is "ÿÿÿÿ"

My Master code:

//MASTER
#include <Wire.h>

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
    Serial.begin(9600);           // start serial for output

}

byte x = 0;

void loop() {
// /* if this part is commented out, the master recieves the slaves message
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting
  // end commented out part */
  
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8
  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }
      Serial.println();         // print the character

  x++;
  delay(500);
}

My Slave code:

//SLAVE
#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  Serial.print("Master SENT DATA:");
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}
void requestEvent() {
  Serial.println("Master ASKED for a response");
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}
void loop() {
  delay(100);
}

Why do you care how many times loop() iterates? Get rid of that stupid delay().

  Serial.print("Master SENT DATA:");

Serial.print() does NOT belong in an ISR, which is what receiveEvent() is.

  int x = Wire.read();    // receive byte as an integer

Why in hell would you want to do that?

Serial.print() doesn't belong in requestEvent(), either.

A schematic or a decent photo showing how the Arduinos are connected is going to be essential.