For starters, try taking out the Serial.print statements inbetween Wire.begintransmission and Wire.endtransmission. You are forcing the Arduino to spend a lot of time doing other things in the middle of the I2C transmission.
And maybe start with a simpler slave program: just have see if Wire.available is ever true in your main loop. If it is, add back the interrupt, prints, etc.
Finally, the usual value for the pullup resistors is 4.7k ohms.
while (Wire.available()) Serial.print(Wire.receive(), BIN);
How are you monitoring the Serial prints of the slave? If you are watching in a serial monitor, then you probably won't see anything. You are sending the number "1" which if you look at an ASCII chart, isn't a character. Instead of BIN, try DEC.
I wouldn't call delay() in your loop. Instead, just leave the loop blank.
Professor Chaos, I have follow your indication; no time lose between begin and endtransmission + I put some check before the while. But I have to wait for Monday to by a 4.7k resistor. The sentance: "Ho! Lord! The master arduino taking to me!" never apear! Well, The function receiveEvent are never call!
James C4S, I have remove the delay in the master loop function and i now send the number 666 print whit the DEC attribute. Always no communication...
Thx for the help! I will buy the proper resistance this Monday! If anyone have a other think. This is my new test code:
Master:
#include <Wire.h>
void setup()
{
Serial.begin(115200);
Wire.begin();
}
void loop()
{
Serial.print("beginTransmission");
Serial.print('\t');
Wire.beginTransmission(0x11);
Wire.send(666); //Send the devil number whitout losing time whit some other manipulation.
Wire.endTransmission();
Serial.print("endTransmission");
Serial.println();
delay(1000);
}
Slave:
#include <Wire.h>
void setup()
{
Serial.begin(115200);
Wire.begin(0x11);
Wire.onReceive(receiveEvent);
Serial.println("I'm initialize!");
}
void loop()
{
//Empty
}
void receiveEvent(int howMany)
{
Serial.print("Ho! Lord! The master arduino taking to me!");
//Print the state of Wire
if(Wire.available())
{
Serial.print("Available");
}
else
{
Serial.print("No Available");
}
//Receive data
int myInt = Wire.receive();
//Print data
Serial.print(myInt, DEC);
Serial.println();
}
By the way, This is what I receive when I plug the arduino in a console:
The basic forms of Wire.send and Wire.receive are byte functions, not int, so use a number between 0 and 255.
But I realize we are overlooking the most basic questions:
Have you connected each Arduino's ground to each other?
Since you are using a breadboard Arduino, are you using pins 27 and 28 on the chip, which are analog inputs 4 and 5 (the I2C pins), or are you using pins 4 and 5 on the chip (not the I2C pins!)?
Some of the "connect two devices" issues are also covered in my essays on connecting two devices by a serial link. (It is a "dumb" connection, no hardware handshake, and no software checksums implemented in the demo stuff.)
Bulleye Professor Chaos,
I make a very stupid mistake in my connection pin.... And yes my 2 ground are connected. Well, whit wire at good place and a 10k pullup resitor (thx daveg360) the communication begun!!! Youpi! And effectively my Devil number didnt make the way. That a good news, only one arduino have to repent and confess its sins.