Reading serial.write in ARDUINO IDE

Hello! I am having a little trouble with my code.

// We’ll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee’s DOUT (TX) is connected to pin 2 (Arduino’s Software RX)
// XBee’s DIN (RX) is connected to pin 3 (Arduino’s Software TX)
SoftwareSerial XBee(5, 4); // RX, TX

String motorfunc;

void setup()
{
// Set up both ports at 9600 baud. This value is most important
// for the XBee. Make sure the baud rate matches the config
// setting of your XBee.
XBee.begin(9600);
Serial.begin(9600);
}

void loop()
{
//if (Serial.available())
{ //If data comes in from serial monitor, send it out to XBee
// XBee.write(Serial.read());
}
if (XBee.available())
{ // If data comes in from XBee, send it out to serial monitor
Serial.write(XBee.read());
}
motorfunc = Serial.read();

if (motorfunc==“Distance: 9mm”); {
Serial.println(“stop”);
}

I want to read the data coming out of my Serial monitor and use it into another if loop. For example: I am reading ‘Distance: 9mm’ from my Serial Monitor, I want to use it to do another if loop like if ‘Distance: 9mm, a “stop” will print in the Serial Monitor.’ I cant get it to work. I tried it and the word ‘stop’ only prints but the Distance: 9mm prints in a different way

Dstop
istop
sstop
tstop
astop
nstop
cstop
estop
:stop
stop
9stop
mstop
mstop

this is what appears in the Serial Monitor. Any help is appreciated! Thanks!

read is only reading one Byte. For example the "D" in "Distance", then the "i" in "Distance" and so on.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

JaBa:
read is only reading one Byte. For example the “D” in “Distance”, then the “i” in “Distance” and so on.

oh that’s why! so how can i read the whole ‘Distance: 9mm’ from the serial monitor?

mikaeehl:
oh that's why! so how can i read the whole 'Distance: 9mm' from the serial monitor?

See reply #2

Probably because you receive a D, print it, an I and print it, a s an print it etc.

Change your quote tags around your code in the opening post to code tags, please. Makes it easier to read and copy.