Sending an INT value using xBee Series 1

The TX circuit should send 100 if the distance sensor detects an object within 3cm and 70cm. The RX should receive the 100 and make a pin HIGH for 10 milliseconds.

I’ll add more distance sensors later. The number 100 was chosen arbitrarily.

The TX is sending 100, but the RX is receiving values like this:
49
48
0
10
13
19
48
49

What am I doing wrong?

TX code:

void loop() {
SonarSensor(trigPin1, echoPin1);   //calculate distance using distance sensor 

FrontSensor = distance; //store the distance

 if (FrontSensor>3 && FrontSensor<70) //if it is within 3 and 70 cm, print 100
 {
  Serial.println(100);
  delay(500);
 }
}

RX code:

void loop() {

if (Serial.available()>0){
  
value = Serial.read();
int realvalue=int(value);
Serial.println(realvalue);


if (realvalue == 100){
digitalWrite (3, HIGH);
delay(10);
digitalWrite(3,LOW);
}

delay(500);
  }

}

Easy, you're using the hardware serial port for both XBee and PC, which is BAD. Never do that - ever.

Instead, use softwareserial.h

I'm not sure what you mean, could you elaborate please?

Assuming you are using an UNO, you're using the hardware serial port for both interfacing with your PC AND your XBee. This will give incoherent results since the XBee and PC are both fighting for access to the serial port.

You need to use softwareserial.h to use different pins as a TX and RX for the XBee. Softwareserial.h uses a "bitbang" method to emulate a hardware UART module in software. Just google it.

This will sound trivial but, how can I open the serial port for the new serial I made?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11); // RX, TX

void setup() {
mySerial.begin(9600);
pinMode (3, OUTPUT);
}

void loop() {
mySerial.println(“Hello, SoftwareSerial ready”);
delay(1000);
}

I’m testing this at the moment.

For anyone who comes to this post with the same problem, here’s what I added to make the original code work (Note that I’m only showing the changes I made):

TX:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);

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

}

Replace “Serial.print” with “mySerial.print”

RX:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11); // RX, TX

byte value[3];

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

}

void loop() {
if (mySerial.available()>2) {

for (int i=0; i<3; i++)
value*=mySerial.read();*

  • Serial.println(value[2]);*
    IMPORTANT NOTE: For some reason, when TX sends 0, RX receives 48. When TX sends 1, RX receives 49, and so on. In my case, I’m sending 1,2, and 3. The RX code uses 49,50, and 51 in it’s switch statement.

CGL2017: IMPORTANT NOTE: For some reason, when TX sends 0, RX receives 48. When TX sends 1, RX receives 49, and so on. In my case, I'm sending 1,2, and 3. The RX code uses 49,50, and 51 in it's switch statement.

This is because you are sending the ASCII coded chars for 1,2, and 3. Look at the this ASCII table.

See how "1" corresponds to 49?

In order to actually send a "1", use write() instead of print().

|500x341