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".
is i2c capable of doing what i want?
Yes.
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
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
}