Hi,
I'm using 2 arduinos: Duemilanove and Mega2560:
The first one is connected with USB to a PC that is running Mission Planner
The second one is connected with USB to another computer and to a Pixhawk Telemetry port (UART) with softserial: I can read on the computer on Serial port (It send data received from i2c to Softserial and Serail)
both arduino are linked with i2c protocole and wired correctly.
if I simulate mission planner to send data it works, I received the data on the second Arduino. but when it is Mission planner it doen't work. any idea?
thanks
receiver:
// Include the required Wire library for I2C<br>
#include <Wire.h>
#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9); // 8=RX, 9=TX
int LED = 13;
int x = 0;
char c = ' ';
int y = 0;
char incomingByte = ' ';
void setup() {
// Define the LED pin as Output
pinMode (LED, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
Serial.begin(57600);
softSerial.begin(57600);
}
void receiveEvent(int bytes) {
c = Wire.read(); // read one character from the I2C
if(c!=0xFFFFFFFF){Serial.print(c); }
softSerial.write(&c,1);
}
void loop() {
incomingByte = softSerial.read();
if(incomingByte!=0xFFFFFFFF){//Serial.print(incomingByte); }
Wire.beginTransmission(10); // transmit to device #10
Wire.write(incomingByte);
Wire.endTransmission(); // stop transmitting
}
}
Sender:
#include <Wire.h>
int x = 0;
int LED = 13;
char incomingByte = ' ';
char c = ' ';
void setup() {
// Start the I2C Bus as Master
Wire.begin(10);
pinMode (LED, OUTPUT);
Serial.begin(57600);
Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
c = Wire.read(); // read one character from the I2C
if(c!=0xFFFFFFFF){Serial.write(&c,1); }
}
void loop() {
incomingByte = Serial.read();
if(incomingByte!=0xFFFFFFFF){
Wire.beginTransmission(9); // transmit to device #9
Wire.write(incomingByte);
Wire.endTransmission(); // stop transmitting
}
}
it makes sense it cannot work, i2c cannot do duplex communication, so I'll try with another way.
That close my question. For the same goal I try to use Serial only, but of course it doen't work for now, to I created nex questions Here (link to the forum question)
to make it more clear, I did not put Master nor Slave on th Arduinos because I don't know what is the best choice: I need duplex communication and I don't think I2C can do that (please correct if I'm wrong), so my design was wrong ,
1. It is required that we install pull-up resistors for the I2C bus; but, in your case these are not needed as the MEGA Board has built-in 2x10 pull-up resistors on the I2C Bus.
2. You can make Duemilanove as Master and MEGA as Slave for data exchange between them in half-duplex mode unlike UART which is full-duplex.
3. You can send data to Slave at any time executing the following codes;
Wire.beginTransmission(slaveAddress);
Wire.write(buffer, sizeof buffer); //buffer size is limited to 32 bytes
Wire.endTransmission();
4. You can request Slave to deliver data to Master at any time by executing the following codes:
byte m = Wire.requestFrom(slaveAddress, n); //n = number of bytes requested <= 32 (say)
byte y1 = Wire.read();
byte y2 = Wire.read();
..........
Thank you so much @GolamMostafa that is very clear, I learn a lot of things about I2C and that confirm it was a bad idea to use I2C for this project because it make it more complex or impossible (because I need full-duplex)
The actual times that full-duplex is used or even needed are almost ZERO. Please explain why you need this and expect it with microcontrollers that can only do one thing at a time.
We may remain adhered to the conventional definition of full-duplex, which says that both transmission and reception can take place simultaneously and accordingly, the UART Port of a MCU is a full-duplex port keeping in mind that the MCU is a serial device, and it has to be as its inventor/discoverer (the human being) is itself a serial entity.
Not necessarily! I recall waiting for a meeting in a lunch/break room in a bank in Brownsville Texas. Two young ladies were there talking in both English and Spanish, Both speaking at the same time and seemingly both understanding what the other was saying. Remarkable abilities!