Multi serial communication via xbee s2b

Hello guys
I'm facing a problem in my project , I need to control two servo motors by two encoders so I have to send two serial data via xbee s2b which is in arduino mega2560 and receive also those two data by the other xbee s2b which in the other arduino mega 2560
The problem that the xbee has only one Rx and one Tx and I need two Rx and Tx .
For example to send data serial.write , serial1.write and recieve by seial.read and serial1.read .
Can anybody help me in this :frowning: I spent more than two weeks trying solving this .
Thanks

The problem that the xbee has only one Rx and one Tx and I need two Rx and Tx .

Why? There is only one communication channel needed between the XBee and the Arduino. You can send tons of data over that one channel.

I spent more than two weeks trying solving this .

Why would you spend more than 5 minutes on the impossible?

Where IS the code you are using?

Thank you very much
So I can now send serial and serial1 from the arduino into the only one tx in the xbee
I'm I right ? Well I will post my tonight hope you will have a look at it

So I can now send serial and serial1 from the arduino into the only one tx in the xbee

How are you storing the data from Serial and Serial1? What is connected to those ports? If you are collecting the data from two serial ports into two separate arrays, then sending the data to the XBee using something like:

xbee.print("<1,");
xbee.print(array1);
xbee.print(">");


xbee.print("<2,");
xbee.print(array2);
xbee.print(">");

xbee is the instance of HardwareSerial (or SoftwareSerial) that the XBee is connected to. Then, the receiver, using one port, can tell which port to send the data to.

Actually I'm using serial.write(the desired value that I want to transmit) in the transmit code
And in the receiver I'm using if serial.available>0
Int x= Serial.read()
And x is tha value that I need in the receiver for moving the servo
I did get the idea of array but I dont know how to apply it in the code , is the array the only solution for this ?

is the array the only solution for this ?

Nop. But somehow you have to define whether the data is to go out one port vs. the other.

What is the data being collected from? Sent to?

here is my code for TRANSMITING.....................

#define encoder0PinA 2
#define encoder0PinB 3
#define encoder0PinC 18
#define encoder0PinD 19
volatile unsigned int encoder0Pos = 0;
volatile unsigned int encoder0Pos1 = 0;

void setup() {
Serial.begin (9600);
Serial1.begin (9600);
//encoder
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
pinMode(encoder0PinC, INPUT);
pinMode(encoder0PinD, INPUT);
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(1, doEncoderB, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(4, doEncoderC, CHANGE);

attachInterrupt(5, doEncoderD, CHANGE);

}
void loop(){
}

void doEncoderA(){
if (digitalRead(encoder0PinA) == HIGH) {
// found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == HIGH) {
// check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.write(encoder0Pos);

}

void doEncoderB(){
if (digitalRead(encoder0PinB) == HIGH) {
// found a low-to-high on channel B
if (digitalRead(encoder0PinA) == HIGH) {
// check channel A to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinA) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}

Serial.write(encoder0Pos);

}

void doEncoderC(){
if (digitalRead(encoder0PinC) == HIGH) {
// found a low-to-high on channel A
if (digitalRead(encoder0PinD) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1 - 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinD) == HIGH) {
// check channel B to see which way encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1 - 1; // CCW
}
}
Serial1.write(encoder0Pos1);

}

void doEncoderD(){
if (digitalRead(encoder0PinD) == HIGH) {
// found a low-to-high on channel B
if (digitalRead(encoder0PinC) == HIGH) {
// check channel A to see which way
// encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1- 1; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinC) == LOW) {
// check channel B to see which way encoder is turning
encoder0Pos1 = encoder0Pos1 + 1; // CW
}
else {
encoder0Pos1 = encoder0Pos1 - 1; // CCW
}
}

Serial1.write(encoder0Pos1);

}

and for RECIEVING.......................

#include <Servo.h>
Servo servo1;
Servo servo2;

int servo1Max = 180;
int servo1Min = 0;
int servo2Max = 180;
int servo2Min = 0;

void setup() {
Serial.begin (9600);
Serial1.begin (9600);
// Servo
servo1.attach(9); //Servo 1 is on Pin 9
servo2.attach(10); //Servo 2 is on Pin 10
servo1.write(0); //position servo at 0 initially
servo2.write(0); //position servo at 0 initially

}
void loop()
{

while(Serial.available() == 0);
int encoder0Pos = Serial.read();

// set upper and lower limits to servo movement
if(encoder0Pos >=servo1Max && encoder0Pos <65500) {
encoder0Pos = servo1Max;
}
if (encoder0Pos >65500 && encoder0Pos<=servo1Min){
encoder0Pos = servo1Min;
}
servo1.write(encoder0Pos*5); // move servo
//Serial.println (encoder0Pos, DEC);

Serial.flush();

while(Serial1.available() == 0);
int encoder1Pos = Serial1.read();

// set upper and lower limits to servo movement
if(encoder1Pos >=servo2Max && encoder1Pos <65500) {
encoder1Pos = servo2Max;
}
if (encoder1Pos >65500 && encoder1Pos<=servo2Min){
encoder1Pos = servo2Min;
}
servo2.write(encoder1Pos*5); // move servo
// Serial.println (encoder0Pos, DEC);

Serial1.flush();

}

that will not work ? my mega is not working i ordered one so im just waiting to try , but now i have to replace this code by the array techneique ?

that will not work ?

You should NOT be doing Serial output in an ISR.

You don't have two XBees connected to two serial ports, so writing to two serial ports is useless.

Serial.flush() blocks until all pending serial data has been sent. It is absolutely pointless in a sketch that never writes to the serial port.

Blocking until there is serial data to read, on one port, and then blocking until there is data on the other port, is NEVER going to work.

Im so sry for disturbing you but im lost now and i dont know how to use the array exactly in my case i have recieved my anothrr mega so im ready to test

could you please help me :frowning: !!!

could you please help me

I've pointed out stuff you are doing wrong. You've ignored me.

I've asked questions. You've ignored me.

Turnabout is fair play.