Simple I2C Config and Code

Hello and thank you all! I'm having trouble getting the master to request a byte of data, the slave to send that data, and then the master to print that data.

Note about the config: I have the A4's and A5's of the two Arduinos connected to each other. Is there a way to specify which set of I2C buses we want to use?

Master Arduino Code:

#include <Wire.h>


int slaveIn;

void setup() {
  Wire.begin();
}

void loop() {
  Wire.requestFrom(1, 1);
  while(Wire.available()==0);
  slaveIn = Wire.read();
  Serial.println(slaveIn);
  delay(1000);
}

Slave Arduino Code:

#include <Wire.h>

int counter = 0;

void setup() {
  Wire.begin(1);
}

void loop() {
  Wire.onRequest(request);
}

void request(){
  Wire.write(counter);
  counter++;
}

I'm having trouble getting the master to request a byte of data, the slave to send that data, and then the master to print that data.

But, you want to keep the problem a secret. Well, then, we'll need to keep the solution a secret, too.

Wire.onRequest() registers an event handler to be called when an event occurs. You only need to do that once, in setup().

PaulS:

I'm having trouble getting the master to request a byte of data, the slave to send that data, and then the master to print that data.

But, you want to keep the problem a secret. Well, then, we'll need to keep the solution a secret, too.

Wire.onRequest() registers an event handler to be called when an event occurs. You only need to do that once, in setup().

Haha, sorry for the late response. I was in school and class was ending. Just got home and finished the daily load of HW.
The problem is that the slave Arduino either was not receiving the request or it was not sending. I'm not sure if the TX and RX LEDs blink when data is being sent via I2C bus. Either way, neither are blinking.

EDIT: This is what I THINK the problem is. The assumpting is based on the fact that ther is no printing going on (slaveIn should get a default value of 0, or at least something. The Master Arduino is stuck in the while loop). I'm not sure if the master Arduino just isn't making the request properly, or I haven't connected the two Arduino's correctly (once again, I just connected the A4s and A5s).

Gahhh, I forgot the pullup resistors. I got confused with SPI. Grrr.
I figured it out (I think). Thanks a lot for the onRequest method btw. I will move that to setup!