Arduino Mega 2560 freezing with Wireless SD Shield and XBEEs

Hello all,

I have been working on a project requiring an Arduino Mega to communicate with an XB router using an on-board XB coordinator on the wireless SD shield. I am having a fairly fatal problem with the XBEEs.

When the coordinator is connected to the shield, with the Serial select switch set to Micro, my uploaded sketch will freeze before even entering the setup loop. However, after I physically press the reset button, my code works as intended.

I have tried utilizing the Arduino’s Watchdog Timer to reset the system when it’s locked up, but the code will freeze before the watchdog can even initialize. I’ve also tried various hardware watchdog timers, but I haven’t dialed one of those down yet.

I’ve attached the current code that I’m running on the Arduino below. I engage the serial communication with the XBEE not during the setup loop but during the normal loop as one solution (XB failing to connect until reset? It was worth a shot)

#include <avr/wdt.h>
int xbd[] = {
  0,0};  // two integer data packet (03,A1)16 -> (3,160)10
int i;
int data;
int dataArray[20];
// XB packet is 22 groups long
// disgarding first delimeter bit (0X7E)
int xbi[] = {
  0,0};  //two bit XB identifier
int test = 34;
int reading = 22;

void setup() {
  wdt_enable(WDTO_8S);     // 8 second WDT timer inverval 
  wdt_reset();  // reset the timer to avoid a system reset
//  Serial.println("Reset.");
  pinMode(test,OUTPUT);
  pinMode(reading,OUTPUT);
  digitalWrite(test,HIGH);
  delay(1000);
  digitalWrite(test,LOW);
}

void loop () {
  wdt_reset(); 
  Serial.begin(9600);

  data = 0;
  while(!data) {
    digitalWrite(reading,HIGH);
    if(Serial.available()>=21) {
      wdt_reset();
      if(Serial.read() == 0x7E) {
        wdt_reset();
        for(i=0; i<22;++i) {
          wdt_reset();
          dataArray[i] = Serial.read();
        }
      }
      digitalWrite(reading,LOW);
      data = 1; 
    }
  }
  digitalWrite(test,HIGH);
  delay(500);
  digitalWrite(test,LOW);
  wdt_reset();
}

Has anyone seen this problem before? I would really appreciate any help here; there’s a lot riding on this project for me…