Problem with Serial.read() from Xbee S2 connected to Xbee Shield

Hi guys,

I'm currently working with a simple Point-to-Point network using Xbee Series-2.

I have 2 sets of Arduino Mega board with both connected to Xbee S2 using Xbee Shield. One will be the coordinator and another one will be the router. And the router is connect with a sound sensor module to receive input. My intention is just simple, when sound detected, router will send data [detected =>1, not detected =>0] to coordinator, when coordinator received data with value 1, it will turn on the LED [pin 13] with 1 second then turn off.

Initially, both Coordinator and Router were programmed with example Blink code to test for board functioning, [Xbee shield set to XBEE/PROG], then router was programmed with following code,

#define LED     13 // LED at D13 pin
#define Mic     9  // Digital output from Mini Mic module is connected to D9

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Mic, INPUT);
  digitalWrite(LED, LOW);
  delay(1000);
}

void loop() {
  if(digitalRead(Mic) == 0) {// When the Mini Mic Module's DO is low, means sound detected.
    digitalWrite(LED, HIGH);
    delay(100);
    //Serial.println("Status: Sound detected!");
    Serial.println(1);
    delay(1000);
  }
  else {
    digitalWrite(LED, LOW);
    //Serial.println("Status: No sound!");
    Serial.println(0);
    delay(100);
  }
}

After that, Xbee shield setting for Coordinator was set to [USB/RUN], then open X-CTU and select Coordinator to display the result in terminal. Everything work fine with value displayed correctly. Later, I programmed Coordinator with following code,

Xbee shield setting set back to [XBEE/PROG]

int inputFromMic = 0; // Detect data Input received from Mic
int led = 13;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(led, LOW);
  
  if(Serial.read() == 1) {
    digitalWrite(led, HIGH);
    delay(2000);
  }
  else {
    digitalWrite(led, LOW);
  }
}

the LED on Coordinator board never turn on. Then I change the IF condition to (Serial.read() == -1) and the LED turn on continuously.
It shows me that the Serial (TX and RX) on Coordinator board is not working.

I'm confusing with few points:
1. Please advise for my coding in Router and Coordinator (especially Serial.read() in Coordinator), am I extract the data correctly in Coordinator?
2. Am I transmit the data correctly in Router ?
3. Is the setting on Xbee shield a matter to this situation ?

The coordinator is set to be broadcast and router DH and DL are set with Coordinator' SH and SL.
I really hope for any advice. Thanks =(

I have 2 sets of Arduino Mega board with both connected to Xbee S2 using Xbee Shield.

Using which XBee shield?

If the one code read ASCII data correctly, the other one did to. That you are expecting '1' to equal 1 is the problem.

Hi Pauls,

Link below is the Xbee Shield that I'm using.
Xbee Shield

I had found out where is my problem. Just as you stated, I'm expecting '1' to equal to 1 is the problem. Thanks to you. :slight_smile:
I had changed the code for Coordinator to below,

void loop() {
  digitalWrite(led, LOW);
  delay(100);
  
  if(Serial.available()) {
    inputFromMic = Serial.read(); // Receive Mic Input from Serial
    
    if(inputFromMic == 49) { // Input receive in Byte
      // Input received delay around 5 seconds
      digitalWrite(led, HIGH);
      delay(2000);
    }
  }
  else {
    digitalWrite(led, LOW);
  }
}

The received data is in BYTE and I'm expecting it be DECIMAL, this is the problem. :wink:
And that, the data arrived with delay about few seconds (3~5 sec).

And that, the data arrived with delay about few seconds (3~5 sec).

No. The data arrived very quickly. You sat around doing nothing for 2.1 seconds, before discovering that the next byte had arrived.

There is no reason to delay() in your code AT ALL.