mega2650 multiple serial programm ?

hi,

i m having mega2650 and 3 servo motor , but program not working
below mentioned program is possible ?
please help me

int a = Serial.read();
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);

}

void loop()
{
if (a > 1)
{
Serial1.print('X');
}

else
{
Serial2.print("G1800");
}

}

Forum how to use sticky read.

Code post using tags.

but program not working

program doing exactly what you told it to do.
Use the playground Luke

Your program is possible, yes.

int a = Serial.read(); it is on wrong place so a it will be empty, this must be on loop routine and better use it after

if (Serial.available() > 0){
     int a = Serial.read();

    // You're code goes here
}

Your Serial.read is being executed before you have set the serial interface up.
This clearly cannot work.

thanks all, for ur support.

Grumpy_Mike - i,m learner of programming and forum. Now code tags learned

from ur feedback, i changed code but still not working . code below

void setup() 
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);
   
}

void loop() 
{
  if (Serial.available() > 0)
  {
  int a = Serial.read();
  if (a > 1) 
     {
      Serial1.print('X');
      } 
  
  else
  {
      Serial2.print("G1800");
      } 
  }
}

What you try to do and what you mean is not working can you explain more please.

int a = Serial.read();
  if (a > 1)

It almost certainly will be.

hi tasosstr,

i have arduino mega 2650 and 3 servo motor ( closed loop , inbuilt drivers & encoder )

i have to control motors from mega board via 3 uart ( serial 1,2 and 3 )

input from arduino serial monitor

Hi friend,

Let me see if i have understand well.

You have an arduino monitor --------> Arduino mega -----Serial 1 -----> Other Arduino for Server motor 1
| -----Serial 2 -----> Other Arduino for Server motor 2
| -----Serial 3 -----> Other Arduino for Server motor 3

Or on arduino monitor you have a software in PC ?

Bellow i give you an example i had make for me (for learn) The data was coming from pc

#define led 13

volatile boolean flag = false;
bool state = false;

String dataIn = "";


void setup()
{
  Serial.begin(9600);
  Serial.println("Start up ...");
  dataIn.reserve(200);
  pinMode(led,OUTPUT);

}

void serialEvent()
{
	//dataIn = "";
    while (Serial.available() > 0) {
        char inByte = (char)Serial.read();
        if (inByte == '\n' || inByte == '\r') {
        	flag = true;
        } else {
        	dataIn.concat(inByte);
        	//Serial.println(dataIn);
        }
    }
}

void loop() {

	if (flag){
		flag = false;
		Serial.println("I got data");
		Serial.println(dataIn);

		if (dataIn == "100"){
			flag = false;
			digitalWrite(led,LOW);
		}

		if (dataIn == "101"){
			flag = true;
			digitalWrite(led,HIGH);
		}

/*
		if (dataIn  == "101"){
			digitalWrite(led,HIGH);
			Serial.println("Requested High");
		}

		if (dataIn == "100"){
			digitalWrite(led,LOW);
			Serial.println("Requested Low");
		}
*/

		dataIn = "";
	}
}

Just for you're information the void serialEvent() it is a routine for hardware serial port only and there you can check the other 3 serial ports as well.

If you need more help please give me more info please.

Good luck

hi tasosstr,

its working thanks lot.