Pages: [1]   Go Down
Author Topic: Simple I2C Config and Code  (Read 433 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
Code:
#include <Wire.h>

int counter = 0;

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

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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().
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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).
« Last Edit: February 11, 2013, 05:40:41 pm by 012anonymousxyz » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: February 11, 2013, 06:32:00 pm by 012anonymousxyz » Logged

Pages: [1]   Go Up
Jump to: