Xbee receiver with Mega2560

First of all I’m french, sorry for my english :slight_smile:

Hi, I’m using 2 Xbee PRO S2C with 2 Arduino Mega 2560.

I want to send informations between 2 arduinos.

I’ve made many researches and I can’t find a solution.

I have the great code to send informations on the first Mega 2560 with the first Xbee, when I connect the second Xbee on XCTU I can see that he receives informations.

My problem is I can’t receive informations when I connect the second Xbee on the second Mega 2560. I try a lot of codes but no results.

My second xbee was connect with a shield on pin 2 and 3 and I saw that all the pins of Arduino Mega can not support high speed transmissions. So now My xbee is connect with wires to the Mega

"
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
"

Here is my code for the receiver :

#include <SoftwareSerial.h>
#define rxPin 13
#define txPin 19
int var = 0 ;
SoftwareSerial xbee = SoftwareSerial(rxPin, txPin);

void setup(){
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
xbee.begin(9600);
Serial.begin(9600);
Serial.println(“Starting XBee Comunication”);
}

void loop(){
Serial.println(“Waiting for XBe”);
var = xbee.read() ;
Serial.println(var);
}

I suppose that there is a problem with the serials.

Stand.ino (379 Bytes)

#include <SoftwareSerial.h>
#define rxPin 13
#define txPin 19

Why are you doing software serial on hardware serial pins? Get over the (stupid) idea that you must use software serial.

You should not read() unless you know that there is data available() to read.

Ok, what sould I do ?

valchpt:
Ok, what sould I do ?

Use a HardwareSerial instance, such as Serial1, Serial2, or Serial3, depending on which matching pair of TX/RX pins you connect the XBee to.

Can you show me an example?

valchpt:
Can you show me an example?

Of what? Reading from the Serial2 instance's pins?

   char crap = Serial2.read();

Ok thanks, it works. I connect the Rx of my Xbee on pin 19 (RX) and Tx of the xbee on pin 18 (Tx) and do :

void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}

}

    int inByte = Serial1.read();
It makes NO sense to use a type (byte) in the name (inByte) of a variable, when the type of the variable (int) is NOT the type used in the name.

THINK about the type needed, and use a meaningful name.

If you KNOW that there is data to read, then it is NOT necessary to store the value in an oversized variable.

If you KNOW that the data is binary data, use a byte variable and the write() method. If you KNOW that the data is ASCII data, use a char variable and the print() method.

If you do NOT know the type, you should.