Hello folks,
I have a problem with the wire library. I dont know if i make a mistake or the lib doesnt work. I really dont know. I tried using google etc, but it didnt answer my questions.
Maybe you guys can help me. Its pretty simple:
I have two arduinos. Arduino A (Duemilanove) and Arduino B (Nano)
Arduino A sends via i2c a command to Arduino B.
Arduino B answers to Arduino A.
This are only 2 Arduinos, but later i want to add more. Everyone is a master. So we have a multimaster setup.
To prevent other Arduinos to interfere the communication between Arduino A and B, A has to keep the connection alive after sending the command. So with the help of google and some other sources i made up some Arduino code i want to show you:
Arduino A:
#include <Wire.h>
#define THIS_ADDRESS 0x8
#define OTHER_ADDRESS 0x9
void setup() {
Serial.begin(9600);
Wire.begin(THIS_ADDRESS);
}
void loop() {
Wire.beginTransmission(OTHER_ADDRESS);
Wire.write(0x12);
Wire.endTransmission(false); //dont send stop condition, keeping connection alive.
Wire.requestFrom(OTHER_ADDRESS,1);
byte c = Wire.read();
Serial.println(c,HEX);
delay(5000);
}
Arduino B:
#include <Wire.h>
#define THIS_ADDRESS 0x9
#define OTHER_ADDRESS 0x8
byte k = 0;
void setup() {
Serial.begin(9600);
Wire.begin(THIS_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}
void loop() {
delay(1000);
}
void receiveEvent(int howMany) {
k = Wire.read();
Serial.print("in: ");
Serial.println(k,HEX);
}
void requestEvent() {
if (k == 0x12) {
Wire.write(0x13);
Serial.print("out: ");
Serial.println(0x13,HEX);
} else {
Wire.write(0x77);
}
k = 0;
}
I know about the false-parameter of the Wire.endTransmission() function. But my problem is:
Arduino B does NOT trigger the requestEvent(). Whats wrong with my code? Is it on A-side oder B-side?
When i delete the "false"-parameter of the Wire.endTransmission, i can run the code without any probs, but i know between the write operation and the read operation is a short break and another master can interfere the communcation.
So, english is not my first language, so have mercy with me