Hi, I also have some trouble with the I2C: I am very new to this communication protocol, So I was trying to see how it works, and I tried making my Due and my Uno talk to each other. It worked, but when I send charters from the Due to the Uno, half is lost.
Here are my codes:
for the Uno (slave):
#include <Wire.h>
char buffer[64];
byte serialpos=0, wirepos=0;
void setup() {
// put your setup code here, to run once:
Wire.begin(0x02);
Serial.begin(115200);
Serial.println("I2C test Starting...");
Wire.onRequest(handler);
Wire.onReceive(receiver);
}
void loop() {
if(Serial.available())
{
buffer[serialpos]=Serial.read();
serialpos++;if(serialpos>64)serialpos=0;
}
// put your main code here, to run repeatedly:
}
void handler()
{
if(wirepos<serialpos)
{
Wire.write(buffer[wirepos]);
wirepos++;if(wirepos>64)wirepos=0;
}
}
void receiver(int num)
{
while(Wire.available())
{
char c=Wire.read();
Serial.println(c);
}
}
and for the Due (master):
#include <Wire.h>
int old, now;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(115200);
Serial.println("I2C test starting...");
old=millis();now=millis();
}
void loop() {
if(Serial.available())
{
Wire.beginTransmission(0x02);
Wire.write(Serial.read());
Wire.endTransmission();
}
now=millis();
if(now-old>1001)
{
old=now;
Wire.requestFrom(0x02,1);
delay(10);
while(Wire.available())
{
char c=Wire.read();
if(c>31)Serial.println(c);
}
}
// put your main code here, to run repeatedly:
}
Any help will be appreciated. Thanks.