Multi Serial in Arduino Mega

HELLO
I Working with Arduino Mega and using him Serial port. I want to use Serial2 and Serial3 for control two servo motor but whrn I use this Program just ones serial port works.
Iwant control the first servo motor with Serial2 and control the second Servo motor by Serial3. but in this program just one Serial works. please guide me. thanks

String servo;
#include <Servo.h>
Servo myservo1;
Servo myservo2;
void setup() {
Serial2.begin(9600);
Serial3.begin(9600);

myservo1.attach(3);
myservo2.attach(5);
Serial3.setTimeout(2);
Serial2.setTimeout(2);
}
void loop() {

while ( !Serial2.available()& !Serial3.available() ); // Wait for characters
double incomingValue1 = Serial2.parseFloat();
double incomingValue2 = Serial3.parseFloat();

if (incomingValue1>850) {
myservo1.write(90);
myservo2.write(90);

}

if (incomingValue1<700){
myservo1.write(-90);
myservo2.write(-90);

}
if (incomingValue2>850) {
myservo1.write(90);
myservo2.write(90);

}

if (incomingValue2<700){
myservo1.write(-90);
myservo2.write(-90);

}

}

asli.ino (882 Bytes)

  while ( !Serial2.available()& !Serial3.available() ); // Wait for characters

Why are you using the bitwise and operator? It seems more likely that you want to use the logical and operator, &&.

Of course, it is not at all obvious what is connected to these other two ports, so it is not clear what you are waiting for. It is also not at all clear why you are waiting for data to arrive on both ports before you do anything.

What is sending the data to Serial2 and to Serial3?

The data from the PC (via the USB cable) only goes to Serial.

...R
Serial Input Basics - simple reliable ways to receive data.

This:

while ( !Serial2.available()& !Serial3.available() ); // Wait for characters
  double incomingValue1 = Serial2.parseFloat();
  double incomingValue2 = Serial3.parseFloat();

makes no sense.... You wait for EITHER Serial2.available() OR Serial3.available(), then read BOTH? And, of course, since you're using bitwise AND (&) rather than logical AND (&&), if Serial2.available() is, for example, 1 and Serial3.available is 2, you'll remain in the while....

Regards,
Ray L.

I would explain that another way...

The bitwise and (single &) looks at bits. 1&1 is 1 but 1&2 is zero since the binary representation of 2 is 10 and all the ones miss all the ones in 01.

You guys missed the not (!) operator in front of each available() call. The not operator converts 0 to 1 and any other value to 0. So the bitwise and (&) operator isn't a problem for this reason (it's a problem for other reasons, though).

christop:
You guys missed the not (!) operator in front of each available() call. The not operator converts 0 to 1 and any other value to 0. So the bitwise and (&) operator isn't a problem for this reason (it's a problem for other reasons, though).

IMO, its rather you miss the whole logik of what is written (no hard feelings, just my opinion may be wrong).

Is your "reading" (ie Serial2.parseFloat(), Serial3.parseFloat()) returning anything valid?

demkat1:
IMO, its rather you miss the whole logik of what is written (no hard feelings, just my opinion may be wrong).

I was responding to the discussion in posts #3 and #4 on using a bitwise operator. Using either a bitwise & or a logical && doesn't change the logic of the code because the ! operator ensures both operands of the & operator are either 0 or 1. There's no possible way that you'll end up with something like 1&2. It can only be 0&0, 0&1, 1&0, or 1&1.

Whatever the case, as a quick fix I'd change it to use a logical or (||) operator to ensure that data is available on both serial ports. (If it were my project I'd rewrite it to be non-blocking.)