Pages: [1]   Go Down
Author Topic: Xbee - stop sending to when a byte is received  (Read 895 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Tokyo, Japan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Trackballer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For a test, why not comment out the following two lines?

Code:
 sensorValue = readSensorValue();
  Serial.println(sensorValue );
With the modification, do you get the "Byte received" message?
« Last Edit: September 24, 2009, 07:28:45 am by tasasaki » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Tokyo, Japan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Trackballer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Tokyo, Japan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Trackballer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: