I am Using Arduino Mega2560 and Xbee

I am Using Arduino Mega2560 and Xbee

I want to use this code

void setup(){
  Serial.begin(9600);
  
 }

void loop(){
  if (Serial.available()>0){
    Serial.write(Serial.read());
  }
  
 
}

but it is not working

Ps. When i use arduino UNO and xbee this code is working!!!

What should i do?? Please help me!

WillKim:
What should i do??

You should place your code between [code]code tags[/code] for starters.
(It’s not too late to edit your post and fix that. :wink: )

You’re using the same port for communicating with the PC and the XBee. Try using “Serial” for the PC, and “Serial1” for the XBee.
(Maybe you were just lucky that it worked on the UNO.)

Where do you connect pins 0 and 1 and do you have the usb cable plugged in and your console opened in the IDE?

I am connecting 0,1 on xbee because i am using xbee shield!!

OldSteve: You should place your code between [code]code tags[/code] for starters. (It's not too late to edit your post and fix that. ;) )

You're using the same port for communicating with the PC and the XBee. Try using "Serial" for the PC, and "Serial1" for the XBee. (Maybe you were just lucky that it worked on the UNO.)

But now i am using shield so i cant use serial1 for the xbee.... there are no other methods???

Well are you sure you connected Rx to Tx and Tx to Rx? Is the xBee powered properly?

you have a mega, so why don’t you use Serial1 for xBee and keep Serial for communicating with your computer? Then you could debug by printing information to your computer.

Also what is the intent of the code? Just an echo? (Sending back whatever you receive?)

Could you please edit your first post and add the code tags around the block of code? Thx

int GPS_receive = 0;
char GPS_data[100];
int GPS_year, GPS_month, GPS_day, GPS_speed, GPS_hour, GPS_minute, GPS_second;
int GPS_comma[20];

float GPS_longitude, GPS_latitude, GPS_h1, GPS_m1, GPS_s1, GPS_h2, GPS_m2, GPS_s2;
long GPS_longdata[20];




char GPS_ns, GPS_ew, GPS_data_valid;
char GPS_chardata[20];

char GPS_location[7];

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

long power(int a, int b) {
  long result = 1;
  if(b == 0) {
    return 1;
  }
  else {
    for(int i = 0; i < b; i++) {
      result = result*a;
    }
    return result;
  }
}

void loop() {
  while(1) {
  
    if(Serial1.available()) {
      
      GPS_data[GPS_receive] = Serial1.read();
      
      if(GPS_data[GPS_receive] == '

this is transmit code!!

and

void setup(){
  Serial.begin(9600);
  
 }

void loop(){
  if (Serial.available()>0){
    Serial.write(Serial.read());
  }
  
 
}

this code is revice code !
and i connected 0,1 for xbee because of xbee shield i can’t change to serial1) {
        for(GPS_receive = 0; GPS_receive <= 5;) {
          if(Serial1.available()) {
            GPS_data[GPS_receive] = Serial1.read();
            GPS_receive++;
          }
        }

if(GPS_data[4] == ‘C’) {
          Serial.print("Receiving GPRMC : ");
          for(GPS_receive = 0; GPS_receive <= 100; ) {
            if(Serial1.available()) {
              GPS_data[GPS_receive] = Serial1.read();
              if(GPS_data[GPS_receive] == ’


this is transmit code!!

and 

§DISCOURSE_HOISTED_CODE_1§


this code is revice code ! 
and i connected 0,1 for xbee because of xbee shield i can't change to serial1) {
                break;
              }
              GPS_receive++;
           }
         }
         
         Serial.print(GPS_data);
         
         
         /*
         for(int i=0; i< GPS_receive; i++) {
           Serial.write(GPS_data[i]);
         }
         */
         
         int j = 1;
         GPS_comma[0] = 0;
         
         for(int i = 0; i < 60; i++) {
           if(GPS_data[i] == ',' || GPS_data[i] == '.') {
             GPS_comma[j] = i;
             j++;
           }
         }
         
         for(int i = 0; i < 15; i++) {
           GPS_longdata[i] = 0;
           for(int j = GPS_comma[i]; j < GPS_comma[i+1]; j++) {
             if('0' <= GPS_data[j] && GPS_data[j] <= '9') {
               /*
               Serial.print(GPS_data[j] - 0x30);
               Serial.print(" ");
               */
               GPS_longdata[i] = GPS_longdata[i] + (GPS_data[j] - 0x30) * power(10, GPS_comma[i+1]-j-1);
               
               /*
               Serial.print(GPS_longdata[i]);
               Serial.print("|");
               */
             }
             else if('A' <= GPS_data[j] && GPS_data[j] <= 'Z') {
               //Serial.print(GPS_data[j];
               GPS_chardata[i] = GPS_data[j];
               break;
             }
             
             //Serial.print(" ");
           }
         }
         
         //Serial.println();
         
         GPS_hour = GPS_longdata[0]/10000;
         GPS_minute = int(GPS_longdata[0] - GPS_hour*10000) / 100;
         GPS_second = (GPS_longdata[0] - GPS_hour*10000 - GPS_minute*100);
         
         GPS_data_valid = GPS_chardata[2];
         
         /*
         GPS_latitude = GPS_longdata[3] + (((GPS_longdata[4]*0.00001)*60)/100);
         GPS_ns = GPS_chardata[5];
         GPS_longitude = GPS_longdata[6] + (((GPS_longdata[7]*0.00001)*60)/100);
         GPS_ew = GPS_chardata[8];
         */
          GPS_latitude = GPS_h1 + (GPS_m1+GPS_s1/60)/60;
         GPS_ns = GPS_chardata[5];
         GPS_longitude = GPS_h2 + (GPS_m2+GPS_s2/60)/60;
         GPS_ew = GPS_chardata[8];
         
         GPS_h1 = GPS_longdata[3]/100;
         GPS_m1 = int(GPS_longdata[3]-GPS_h1*100);
         GPS_s1 = ((GPS_longdata[4]*0.001)*60)/100;
         
         GPS_h2 = GPS_longdata[6]/100;
         GPS_m2 = int(GPS_longdata[6]-GPS_h2*100);
         GPS_s2 = ((GPS_longdata[7]*0.001)*60)/100;
         
         
         GPS_speed = (GPS_longdata[9] + GPS_longdata[10]*0.001)*1.852;
     
         
         GPS_day = GPS_longdata[13]/10000;
         GPS_month = int(GPS_longdata[13] - GPS_day*10000) / 100;
         GPS_year = (GPS_longdata[13] - GPS_day*10000 - GPS_month*100);
         
         GPS_hour = GPS_hour + 9;
         
         if(GPS_hour >= 24) {
           GPS_hour = GPS_hour-24;
           GPS_day++;
           
           int last_day;
           if(GPS_month == 1 || GPS_month == 3 || GPS_month == 5 || GPS_month == 7 || GPS_month == 8 || GPS_month == 10 || GPS_month == 12) {
             last_day = 31;
           }
           else if(GPS_month == 2) {
             if(GPS_year % 4 == 0) {
               last_day = 29;
             }
             else {
               last_day = 28;
             }
           }
           else {
             last_day = 30;
           }
           
           if(GPS_day == last_day + 1) {
             GPS_day = 1;
             GPS_month++;
             if(GPS_month == 13) {
               GPS_month = 1;
               GPS_year++;
             }
           }
         }
         
         
         for(int i = 0; i <20; i++) {
           Serial.print(i);
           Serial.print(" ----> GPS_comma[i] : <");
           Serial.print(GPS_comma[i]);
           Serial.print("> | GPS_longdata[i] : <");
           Serial.print(GPS_longdata[i]);
           Serial.print("> | GPS_chardata[i] : <");
           Serial.print(GPS_chardata[i]);
           Serial.print("> | GPS_data : <");
           Serial.println(GPS_data[i]);
         }
         
         Serial.print("year : "); Serial.println(GPS_year+2000);
         Serial.print("month : "); Serial.println(GPS_month);
         Serial.print("day : "); Serial.println(GPS_day);
         Serial.print("hour : "); Serial.println(GPS_hour);
         Serial.print("minute : "); Serial.println(GPS_minute);
         Serial.print("second : "); Serial.println(GPS_second);
         
         
         Serial.print("speed : "); Serial.println(GPS_speed);
       
       
         Serial.print("valid : "); Serial.println(GPS_data_valid);
         
         Serial.print("latitude : "); Serial.println(GPS_latitude,5);
         Serial.print("ns : "); Serial.println(GPS_ns);
         
         Serial.print("longitude : "); Serial.println(GPS_longitude,5);
         Serial.print("ew : "); Serial.println(GPS_ew);
        
         Serial.print(GPS_h1);
         Serial.print(GPS_m1);Serial.print(GPS_s1);
          Serial.print(GPS_latitude,5);
          
          Serial.print(GPS_ns);
          Serial.print(',');
          Serial.print(GPS_longitude,5);
          
          Serial.print(GPS_ew);
         
          /*
          Serial2.write(GPS_data);
          */
        
        
       }
     }
    }
  }  
}

this is transmit code!!

and

§_DISCOURSE_HOISTED_CODE_1_§

this code is revice code !
and i connected 0,1 for xbee because of xbee shield i can’t change to serial1

With the minimal information you posted, I assumed you were typing characters in the serial monitor which were then sent to an XBee module.

Anyway, if you can't be bothered adding code tags as asked, (twice earlier), I can't be bothered helping.

You really should have read How to use this forum before posting. (Especially item #7.)

what didn’t you understand in the instruction from my post above

“Could you please edit your first post and add the code tags around the block of code? Thx”

And in reply #1:-

What should i do??

You should place your code between [code]code tags[/code] for starters. (It's not too late to edit your post and fix that. ;) )

(5 times now. :D )

I am sorry I did..!!! I didn't know that exactly Sorry!!!!!

Good looks much better now! thanks

So your GPS is on Serial1 of a MEGA I suppose and the Xbee on Serial?

You have noticed that you never reset GPS_receive to 0, right? so when you enter the loop and do GPS_data[GPS_receive] = Serial1.read(); you are using the GPS_receive from the last loop

Serial.print(GPS_data); --> you are lucky there is a 0 to mark the end of the string because you did not put one there yourself AND this is sending data to Serial, which is your XBee shield.. so messing things around...

how can you have this working on a UNO given that the UNO does not have two hardware serial? --> you are running a different code on the UNO, do you?

Yes I connected Mega Serial1 to GPS and Serial to Xbee shield

I mean when I use transmit code I used 1 arduino mega + GPS + Xbee and when I use recive code I used 1 arduino uno + xbee it is working !!! (I connected UNO Serial to Xbee shield)

so i can see GPS data from serial monitor which connected with arduino UNO + Xbee

but when I change Arduino UNO to Mega (I connected Mega Serial to Xbee shield this Mega is different thing that connect Gps)

it is not working!!.... anything is not show on the serial monitor...

I am sorry because my english is not good...

this is the code for the loop() on the receiver on a UNO or MEGA

void loop(){
  if (Serial.available()>0){
    Serial.write(Serial.read());
  } 
}

and you are saying pin 0 and 1 are connected to the XBee?

so you received something from your MEGA+GPS+XBEE and you send it right back to the MEGA+GPS+XBEE?

how does this make sense?

nono i use two Mega First Mega is connected with GPS + Xbee and Second Mega is connected with Xbee!

Transmit code is for First Mega and Receive code is for Second Mega

OK - guess we are getting lost in communication here :)

On the RECEIVE side. what are the pins 0 and 1 of your MEGA connected to? On the RECEIVE side, do you have the USB cable plugged into your computer IDE and console open?

On the TRANSMIT side. what are the pins 0 and 1 of your MEGA connected to? On the TRANSMIT side, do you have the USB cable plugged into your computer IDE and console open?

what I don't get is that on the transmit side you commented out

          /*
          Serial2.write(GPS_data);
          */

and are sending your GPS info on Serial (which is hardware port connected to pin 0 and 1) as well as all the debug info.. tons of stuff.

and on the receiver side, if pins 0 and 1 are connected to Xbee, then whatever you received is sent directly back because the transmit pin is also connected to Xbee...

I'm lost :)

best would be if you do a small drawing of how things are hooked up on both sides

NOno I used two Mega

First Mega is connected with Gps on Serial1 and Xbee on Serial

Second Mega is connected with Xbee on Serial

Transmit code is for First Mega

Recevie code is for Second Mega

And when I used 1 Mega and 1UNO

Mega is connected with Gps on Serial1 and Xbee on Serial

UNO is connected with Xbee on Serial

This set is Working!! so I can see GPS data on Serial Monitor from UNO

But when I use 2MEGA I can't see anything on Serial Monitor from Second MEGA

Last when I use Second MEGA and UNO it is connected USB Cable to Computer

and First MEGA is connected 9V battery!!

If you have facebook or SNS tell me I will send you Picture!!

/*
          Serial2.write(GPS_data);
          */

You dont need to think this code garbage code!!

you can just attach the picture here in the forum or upload it to a free picture hosting site and embed it into your post by using the image tag

but what happens is that you are lucky for this to work on the UNO.

The USB cable you have between the Arduino and the computer IS connected as well to pin 0 and 1. so now you have 2 things, connected to the pins 0 and 1. that creates confusion.

When you have the mega on the receiving end I suggest you use this code:

void setup() {
  Serial.begin(9600); // talking with the computer (should use 115200 really)
  Serial1.begin(9600);// talking with the Xbee
}

void loop() {
  char c;
  if (Serial1.available() > 0) { // something is available from the Xbee
    c = Serial1.read(); // read it
    Serial.write(c); // we write it to the computer
  }
}

and best I would make the steep to the computer much faster (and change it into the IDE console)

Serial.begin(115200); // talking with the computer

when you have the UNO on the receiver side, I would suggest to use Sofware Serial for connecting the Xbee