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);
}
}