Your sender never reads from the serial port, so, in the receiver, what is this for?
Serial.print(int(incomingByte));
The receiver is getting something, but totally different values at first
How do you know what the receiver is getting?
You should isolate the problem. Put the LED (with resistor) on the sender, WITHOUT the XBee shield. Get it reacting to the IR sensor output properly. Then, put the XBee shield back on, with the switch in the DLINE position (on both shields).
Use NewSoftSerial to create a software serial port involving pins 2 and 3. Use the software serial port to talk to the XBee and the hardware serial port to talk to the PC.
This way, you can send data to the XBee and the serial monitor, or receive data from the XBee and send it to the serial monitor.
Echo what you send, and what you receive (on a different computer or different instance of the IDE/Serial Monitor on the same computer), to make sure what is sent IS what is received.
Do I have to assign destination address for both xbees in the code?
No. You assign MY and DL for each series 1 XBee using X-CTU (from digi.com).
Thanks a lot for the prompt reply!!
I am a newbie about wireless communication with Arduino boards, and am really lost by looking at other people's projects.
When you say
Your sender never reads from the serial port, so, in the receiver, what is this for?
Serial.print(int(incomingByte));
you mean I should have "Serial.read()" in the sender too? I thought Serial.print(val2); sends the value to serial port, and in the receiver, by using "Serial.read()", it automatically gets incomingByte from serial port?
How do you know what the receiver is getting?
I connected the receiver to my laptop and opened the terminal window. So I can see it's printing something.
You should isolate the problem. Put the LED (with resistor) on the sender, WITHOUT the XBee shield. Get it reacting to the IR sensor output properly. Then, put the XBee shield back on, with the switch in the DLINE position (on both shields).
This is a good idea. I'm going to try it out. Thanks! But when you put the switches in the DLINE position, the xbees are not using serial port to communicate, right? Instead they should use the software serial port to communicate as you said below. So I can monitor both xbees from 2 separate PCs, right?
No. You assign MY and DL for each series 1 XBee using X-CTU (from digi.com).
I was not using X-CTU, I guess this causes another problem. Thanks for your thorough explanation!!
//RECEIVER
#include <NewSoftSerial.h>
NewSoftSerial mySerial(2,3);
byte incomingByte, sensor1;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("Ready!");
//led pins
pinMode(5, OUTPUT);
delay(1000);
}
void loop()
{
if(mySerial.available()) { //are there any bytes available on the serial port?
//assign bytes to the var 'incomingByte'
incomingByte = mySerial.read();
Serial.print(int(incomingByte));
if((int(incomingByte) == 255)){
sensor1 = mySerial.read();
Serial.print("Sensor 1 = ");
Serial.print(int(sensor1));
}
}
analogWrite(5, sensor1);
}
I plugged the xbee shields back on to the Arduino boards and switched to DLINE, uploaded the two sketches. The two objects(the sender and the receiver) are plugged to two different PCs.
This time I've pre-configured the two Xbees with X-CTU, made each one the destination of the other.
But after uploading the sketches, I still did not get anything other than the "Ready!" word printed by the hardware serial port on the receiver's terminal window. I tried switching both to UART, and both to DLINE too... Neither worked. The sender's terminal window is printing each analogvalue gotten from Pin5 though.
I am thinking maybe I'm still not using NewSoftSerial correctly. Could you advice how to use it correctly? Thanks a lot!!
It turned out that one xbee shield was broken...
I replaced it with a new one and now the two are communicating. I can see the DIN DOUT LEDs blinking all the time.
The receiver terminal is also printing "sensor1=some value". But the values are really big, much bigger than the values printed on the sender's terminal.
The receiver's terminal stopped printing after several dozens of value prints when both are in UART mode.
But if I switch the receiver to DLINE and keep the sender in UART mode, the receiver keeps printing, with wrong values though...
Do you see any reason for this? Thanks!!
Here are parts of the print outs on both terminals.
I guess 'ÿ' is the character of ASCII 255. So for example, when sending 464, the receiver gets 525548. When I turned the IR sensor to send small values, like 2 or 3, the receiver gets some values around 50...
If you printed spaces between the values, you would see
Sensor 1 = 52 55 48 255
52 is the ASCII code for 4
55 is the ASCII code for 7
48 is the ASCII code for 0
Printing the values as characters, instead of integers, you would have seen
Sensor 1 = 470ÿ
Not printing data that wasn't there, you would have seen
Sensor 1 = 470
Thanks, Paul!
Both are printing the same characters now.
So it would always be better to print characters instead of integers, right? Because serial port sends 8-bit characters, right?
I tried this simple project without software serial.
I used 2 9v batteries to power up the boards. Both sender and receiver are switched to UART. But it seems this time it's not working. The LED is not lightening. The sender's DIN LED is blinking while the receiver's DOUT LED is blinking. This is so weird. Could you suggest what is wrong here? Thanks!
if(Serial.available()) { //are there any bytes available on the serial port?
//assign bytes to the var 'incomingByte'
incomingByte = Serial.read();
//Serial.print(incomingByte);
if((int(incomingByte) == 255)){
sensor1 = Serial.read();
If there is a byte to read, read both of them. How's that working for you?
Never mind. It's clear that the answer is "not very well". Wait for there to be two bytes before reading anything.
PaulS:
Wait for there to be two bytes before reading anything.
But the sender sends two bytes at a time. So if the first byte the receiver got is the sync byte, then the following byte must be the sensor value. It worked when I was using software serial...
No, it doesn't It sends two bytes, one after the other. But, it sends them like I type - slowly.
The Arduino can read the data far faster (many orders of magnitude) than it can be sent. Nearly as soon as the first byte appears, the Arduino discovers that it is available, and reads that one and the one that is still being sent (i.e. nothing).
Should I write
instead?
You could. It wouldn't work. All that it would do is ensure that sensor1 was never valued.
You need to wait for there to be two bytes.
if(Serial.available() > 1) { //are there any bytes available on the serial port?
The Serial.available() function does not return true/false. It returns the number of characters available to read. Wait for it to report that both have arrived before you read anything.
Thanks for helping!
I finally found the real problem. It was the power supply.
When I switch to USB power supplies for both, it worked. But still didn't work with battery supplies.
This is so weird. Do you have any idea why the battery supplies don't work? I can see the sender's DIN LED and the receiver's DOUT LED blinking all the time as they do while using USB power supplies. But the LED which connects to the output pin of the receiver is not lightening up...