XBEE recieves gibberish at first

Hi, I am new to arduino and xbee programming. I recently got two xbees for my school project. I am testing on some code to send alphabets from one arduino uno to another. I am using arduino Uno ,Uno Xbee sheild and Xbee Pro S1

Here's the code :-

Sender

#include <SoftwareSerial.h>

SoftwareSerial xbee(0, 1); // RX, TX
char c = 'A';
int  pingPong = 1;

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

    Serial.println( "Arduino started sending bytes via XBee" );

    //Set the data rate for the SoftwareSerial port
    xbee.begin(9600);
}

void loop() {
    // Send character via XBee to other XBee connected to Mac
    // via USB cable.
    xbee.write( c );

    //--- Display the character just sent on console. ---
    Serial.println( c );

    //--- Get the next letter in the alphabet, and reset to ---
    //--- 'A' once we have reached 'Z'.
    c = c + 1;
    if ( c>'Z' )
         c = 'A';

    //--- Switch LED on Arduino board for every character sent---
    if ( pingPong == 0 )
        digitalWrite(13, LOW);
    else
        digitalWrite(13, HIGH);
    pingPong = 1 - pingPong;
    delay( 1000 );
}

Reciever

#include <SoftwareSerial.h>

SoftwareSerial xbee(0, 1); // RX, TX

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

    Serial.println( "Arduino started receiving bytes via XBee" );

    // Set the data rate for the SoftwareSerial port.
    xbee.begin(9600);
}

void loop()  {
    
  if(xbee.available()) {
    char temp = xbee.read();

    Serial.print("Character received:");
    Serial.println(temp);
  }  
  delay(1000);

The issue is that I am recieve random characters at the beginning of transmission. After a minute or I start seeing correct values in the reciever window
I want to know why is it happening and what should I do to prevent it

I want to know why is it happening and what should I do to prevent it

Quit being an idiot.

SoftwareSerial xbee(0, 1); // RX, TX

Pins 0 and 1 are for the hardware serial port. They are NOT to be used for SoftwareSerial.

Pins 0 and 1 are for the hardware serial port. They are NOT to be used for SoftwareSerial.

Well, The code I copied had something like

Softserial xbee(2, 3);  . I don't recieve anything when I use it. Is it something to do with the sheild I am using?

Is it something to do with the sheild I am using?

Probably.

If your shield is hardwired to the hardware serial pins, quit using SoftwareSerial. Or, pitch that shield and get a decent one.

Thanks for the reply. Another thing I notice is that on my sender sheid I see the 'TX' light blinking. I assume it is probably sending the data. I don't see the light on the recieving end though

Another thing I notice is that on my sender sheid I see the 'TX' light blinking. I assume it is probably sending the data.

When the TX light is blinking, on the Arduino, that means that the hardware serial port is sending data. It does not mean that the software serial port is doing anything.

Forgive me I'm digressing a bit here but could you suggest some way to figure out where the is the problem?
I think I can send over hardware serial. I get some gibberish at the beginning of transmission though

I think I can send over hardware serial. I get some gibberish at the beginning of transmission though

The second thing that you need to do is get rid of all the stuff that you are printing that is NOT to be sent to the other XBee.
The first thing that you need to do is get rid of the SoftwareSerial include statement and all uses of the instance.

When you are using SoftwareSerial the way you are, you are interfering with the hardware serial functionality of those pins, which guarantees trouble.

Once you have removed the SoftwareSerial stuff, post the code and how you know that you are getting "gibberish".

That last part is going to be hard to do when you can't use the hardware serial pins for debugging.

you amy also want to consider eliminating the delay on the receiver side:

    Serial.println( "Arduino started receiving bytes via XBee" );

    // Set the data rate for the SoftwareSerial port.
    xbee.begin(9600);
}

void loop()  {
    
  if(xbee.available()) {
    char temp = xbee.read();

    Serial.print("Character received:");
    Serial.println(temp);
  }  
  delay(1000);

If you are sending data every second in your "Sender" then you really don't need to delay its receipt on the "Receiver"

Consider instead following PaulS's suggestion and toggling the on-board LED for a half second when a char was received:

    //Serial.println( "Arduino started receiving bytes via XBee" );

    // Set the data rate for the SoftwareSerial port.
    xbee.begin(9600);
}

void loop()  {
    
  if(xbee.available()) {
    char temp = xbee.read();
    digitalWrite(13, !digitalRead(13));  //be sure to set pin13 to OUTPUT
    //Serial.print("Character received:");
    //Serial.println(temp);
  }  
  //delay(1000);

The second thing that you need to do is get rid of all the stuff that you are printing that is NOT to be sent to the other XBee.
The first thing that you need to do is get rid of the SoftwareSerial include statement and all uses of the instance.

Worked like a charm. Thanks!
I am still get '-1' on the last variable when trying to send 3 variables across though . Not so when I use a delay .

I am still get '-1' on the last variable when trying to send 3 variables across . Results are more accurate when I use a delay though.

Try a longer delay, then. Maybe about 20 minutes.

Absolutely no delay()s are needed to properly read serial data. End of packet markers are, though.

How do you know you are getting -1, though? Serial is being used to talk to the XBee, isn't it?

Absolutely no delay()s are needed to properly read serial data. End of packet markers are, though.

Okay, I'm only using a header, nothing to indicate end of packet.

How do you know you are getting -1, though? Serial is being used to talk to the XBee, isn't it?

Can't I use 'Serial' to print after I've read the data?

Anyway here's the code :-

Here's the code at the recieving end :-

int8_t x;
int8_t y;
int8_t z;

char HEAD = 'H';

void setup()
{
    Serial.begin(9600);
    pinMode(13, OUTPUT);

}

void loop()  

{
    
  if(Serial.available()) {
    
    if(Serial.read() == HEAD) {
      x = Serial.read();
      y = Serial.read();
      z = Serial.read();
      
      // Toggle the LED
      digitalWrite(13, !digitalRead(13));
      Serial.print(x);
      Serial.print("  ");
      Serial.print(y   );
      Serial.print("  ");
      Serial.println(z   );
    }
  }
  
}