0

I've already sent you the code but you told me to format it, so I tried this way ..

the problem I got is it doesn't read good the 2 slave (sender) to the master

(reader) ...you told me LAST TIME (DON'T DO SERIAL PRINTLN IN THE CALLBACK

ROUTINE) so I tried this code (I've got some data from both but not in order)

here's the slave 1 and 2 code (I only change the slavedeviceId address)

#include "Wire.h"


//
// i2c_test_01

//
// This demonstrates a master transmitting to a slave.
// 

// The master transmits a value read from an analogue port.

// The slave sends the value to the serial device.
//


byte x = 0;
//
const byte AnalogueInputPin = 0;

const byte SlaveDeviceId = 2;



void setup() {  
  // Start I2C bus as master
  Wire.begin();     
}

void loop() {
  int input0 = analogRead(x);
  // Send two bytes to slave.
  Wire.beginTransmission(SlaveDeviceId);
  Wire.write(input0 >> 8);
  Wire.write(input0 & 255);
  Wire.endTransmission();

  delay(10);

  x++;
  if (x == 4) {
    x = 0;
  }

  delay(40);


}

here's the master (reader)

thanks for your time it is realy intructive for me !!!

#include "Wire.h"

const byte SlaveDeviceId = 1;
const byte SlaveDeviceId2 = 2;
int S1;
int S2;
int c;
int cc;

void setup() { 
  // Start I2C bus as a slave
  Wire.begin(); 
  Wire.onReceive(receiveCallback);
  Serial.begin(9600);
}

void SLAVEone() {
  Wire.begin(SlaveDeviceId);
  delay(5);
  Wire.onReceive(receiveCallback);
  delay(5);
}

void SLAVEtwo() {
  Wire.begin(SlaveDeviceId2);
  delay(5);
  Wire.onReceive(receiveCallback2);
  delay(5);
}

void receiveCallback(int aCount) {
  if(aCount == 2) {
    int receivedValue  = Wire.read() << 8; 
    receivedValue |= Wire.read(); 
    //Serial.print(c);  
    //Serial.print(" S1 = ");
    //Serial.println(receivedValue);  
    S1 = receivedValue;
  } else {
    Serial.print("Unexpected number of bytes received: ");
    Serial.println(aCount);
  }
  c++;
  if (c == 4) {
    c = 0;
  }
}

void receiveCallback2(int aCount2) {
  if (aCount2 == 2) {
    int receivedValue2  = Wire.read() << 8; 
    receivedValue2 |= Wire.read();
    // Serial.print(cc);
    //Serial.print(" S2 = ");
    //Serial.println(receivedValue2);
    S2 = receivedValue2;
  } else {
    Serial.print("Unexpected number of bytes received: ");
    Serial.println(aCount2);
  }
  cc++;
  if (cc == 4) {
    cc = 0;
  }
}

void loop() {
  for(int y = 0; y <= 3; y++) {
    SLAVEone();
    Serial.print(c);
    Serial.print(" = S1 = ");
    Serial.println(S1);
    delay(10);
  }
  for(int y = 0; y <= 3; y++) {
    SLAVEtwo();
    Serial.print(cc);
    Serial.print(" = S2 = ");
    Serial.println(S2);
    delay(10);
  }
}
  • I have formatted your code, without changing anything else. Please take a moment to check how it is done. Just hit edit on your question. – Igor Stoppa Nov 5 '15 at 21:15
  • Don't do serial prints in an ISR (callback). – Nick Gammon Nov 8 '15 at 8:20
2

There are so many mistakes in your code. I'll do my best to correct them using comments. The code for the slaves should be as follows:

#include "Wire.h"


//
// i2c_test_01

//
// This demonstrates a master transmitting to a slave.
// 

// The master transmits a value read from an analogue port.

// The slave sends the value to the serial device.
//


int inp = 0;  // input pin

const byte SlaveDeviceId = 2;

void setup() {  
  Wire.begin(SlaveDeviceId);  // here the slave joins the bus, with its address as argument to begin()
  Wire.onRequest(callback);  // here you indicate what the slave should do if a bus master queries it
}

void callback() {
  int input0 = analogRead(inp);  
  // Send two bytes to master
  Wire.write(input0 >> 8);
  Wire.write(input0 & 255);

  inp++;
  if (inp == 4) {
    inp = 0;
  }

}

void loop(){}

This is the master's code:

#include "Wire.h"

const byte SlaveDeviceId = 1;
const byte SlaveDeviceId2 = 2;
char inputDev = 4;  // number of devices connected to slave analog inputs

void setup() {
  Wire.begin();   // no argument here since its a master
  Serial.begin(9600);
}

//the onReceive() method is for slaves receiving from a master, so we dont use it here

void loop() {
  Serial.println("\r\nSlave 1: ");
  for (char i = 0; i < inputDev; i++){
    Wire.requestFrom(SlaveDeviceId, 2);
    delay(1);  // more than enough time for all the data to come in
    int val = Wire.read() << 8;
    val |= Wire.read() & 0xFF;
    Serial.print(val);
    Serial.print(",");
  }
  Serial.println("\r\nSlave 2: ");
  for (char i = 0; i < inputDev; i++){
    Wire.requestFrom(SlaveDeviceId2, 2);
    delay(1);  // more than enough time for all the data to come in
    int val = Wire.read() << 8;
    val |= Wire.read() & 0xFF;
    Serial.print(val);
    Serial.print(",");
  }        

}
1

I don't quite see how your code is supposed to work. It seems like your "slave" devices are trying to write to your "master" as if they were master devices not slaves, and your "master" is rapidly switching between thinking it is one of two slaves. The whole methodology is backwards. The "master" should connect to a "slave" and request the data not wait for a "slave" to randomly throw data at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.