Go Down

Topic: Xbee - stop sending to when a byte is received (Read 1 time) previous topic - next topic

Aleks

I have a robot with a XBee module (ROUTER) and a bunch of sensors. Another Xbee module (COORDINATOR) is connected via USB to the PC. They work in AT mode, broadcasting.

I would like to send the readings of the sensors from ROUTER to COORDINATOR in a loop, but still being able to interrupt it by sending (manually) some byte from the COORDINATOR to the ROUTER, and then again continue on receiving the sensors' readings. I get the sensors' readings sent and received, but I cannot get the ROUTER to receive the byte sent from the COORDINATOR.

I would appreciate your help.

Here is my code:


Code: [Select]
int sensorValue = 0;

void setup() {              
 Serial.begin(9600);        
 int readSensorValue();
}

void loop() {
 if ( Serial.available() ) {
    if ( Serial.read() == 'R' ) { // "R" is the byte addressing the robot
      Serial.println("Byte received");
    }
 }

 sensorValue = readSensorValue();
 Serial.println(sensorValue );

}

int readSensorValue(){
  // this function returns the sensor reading
}


Aleks

Did I ask something too trivial, too difficult or just inappropriate?  :-?

tasasaki

#2
Sep 24, 2009, 02:27 pm Last Edit: Sep 24, 2009, 02:28 pm by tasasaki Reason: 1
For a test, why not comment out the following two lines?

Code: [Select]
 sensorValue = readSensorValue();
 Serial.println(sensorValue );

With the modification, do you get the "Byte received" message?

Aleks

When I comment those lines, I get the "Byte received" message. I believe I need to create an interrupt to read a received byte, but I am not sure how to do that. I would appreciate any help.

tasasaki

Quote
When I comment those lines, I get the "Byte received" message.
Ok. There is no problem with basic wireless communication configurations.
Next, try to insert the following red line.
Quote
void loop() {
 if ( Serial.available() ) {
    if ( Serial.read() == 'R' ) { // "R" is the byte addressing the robot
      Serial.println("Byte received");
    }
 }

 sensorValue = readSensorValue();
 Serial.println(sensorValue );
 delay(500);

}

Do you get sensor data and "Byte received" response?
I suspect sending the data without any delay in the loop to be the main source of the problem.

Aleks

I tried with delay(100); already, didn't work. But I will try longer delay as well. I can't do it before Monday as I am away, but I will get back to you on this.
Thank you for your help, I really appreciate it.

tasasaki

If longer delay() doesn't solve the problem,
how about trying to control the sending interval using a timer like the following?
Quote
int sensorValue = 0;
long previousMillis = 0;
long interval = 500;


void setup() {              
 Serial.begin(9600);        
 int readSensorValue();
}

void loop() {
 if ( Serial.available() ) {
    if ( Serial.read() == 'R' ) { // "R" is the byte addressing the robot
      Serial.println("Byte received");
    }
 }
 if (millis() - previousMillis > interval) {
   previousMillis = millis();

   sensorValue = readSensorValue();
   Serial.println(sensorValue );
 }
}

int readSensorValue(){
  // this function returns the sensor reading
}

Aleks

I am sorry for the late reply. I resolved the issue by creating an external interrupt. I guess serial port could not handle the amount of data it was receiving.

thanks for your help.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy