On a Mega, you have additional serial ports; use one of those. If you do not need the serial port of the Uno, you can use that. Else you'll have to use software serial. There are limitations with software serial. There are also other libraries that you can use instead of software serial.
You can also use spi or i2c for communicayion between arduinos.
Please insert code in the post using code tags (it's small enough). I'm usinga cellphone and can't view the attachments.
Type [code]
Paste your master code after that
Type [/code] after that
Repeat for the slave.
Your com3 error does not have to do with the code. I assume that you try to develop both sketches at the same time. What I do in that situation is opening both from windows explorer by double clicking the ino file and next select the correct com ports from the comms menu (your mega and uno will have different com ports in that menu.
And here we have the slave code, that corresponds to Uno
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus with address #9
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
There is things that i don't understand such as when it's written : "slave device#2", i don't know what it corresponds to. (the commentaries come from the copied code, i didn't erase them)
Please follow the wiring and code of the example. Your I2C addressing seems wrong (2 vs 3, client vs master, etc etc). Also make sure you have the wiring right.
Your RX and TX in the opening post put me on the wrong foot You're not using serial but I2C. So you have to connect SDA to SDA and SCL to SCL (and obviously the ground).
On the Uno R3 and the Mega, you can use the pins that are marked SDA and SCL; they are on the same connector as pins 8..13. It's my understanding that on older Unos those pins are not there and you can use the pins A4 and A5.
With regards to the code, Wire.requestFrom is use by a master to request data from the slave. You however use it in the slave code. Also, as you don't specify an address in the slave code, that code is the master.
Next your code for the master simply writes without waiting for a request; this will more than likely result in collisions on the bus.
If you have a master that requests data (as you seem to have), have a look at the Master Reader/Slave Sender example.
Ah ok i'm going to change the pins, so i connect the pins SDA and SCL of the mega to pins A4 and A5 of my uno, is that right ?
And then, to answer your questions, i don't want my slave (UNO) to send informations to the master (MEGA), i just need the mega to send information (it's going to be values between 1 to 32) to the uno.
So i remove the Wire.requestFrom from the slave ?
In the Slave setup, i put Wire.begin(9), and in the master it's Wire.beginTransmission(9).