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
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 .