State control - using commands sent over XBee wireless?

Hey everyone.

I have a project that's been going along quite nicely... until now. I have a clock hooked up to a motion sensor, which after 15 minutes will send motion data wirelessly to a hub, which I have hooked up via ethernet to a database. Unfortunately for me, I cannot get rid of that middle 'wireless gap' step, and my problem revolves around it. Basically I need to come up with a clearly deliniated series of 'states' for my two arduinos to go between, and I'm having trouble setting it up.

My setup:
Clock & Motion Sensor -> Arduino 1 ('Clock') <--> XBee 1 ----Wireless---- XBee 2 <--> Arduino 2 ('Hub') -> Ethernet -> Database.

Here's a basic diagram of the flow of what I'm trying to do:
(Sorry, huge picture, didn't want to embed it here.)


so basically:
1 - Both enter 'setup', and have to wait for a "ready" signal to continue.
2 - 'Hub' fetches current time, and sends it to 'Clock', which is waiting.
3 - Once the Clock's time is set, it watches for 15 minutes, and collects data.
4 - At the end of this period, it packages it up, and sends it to the 'Hub'
5 - 'Hub' sends the data on to the database, and returns to fetch the current time (just in case the clock is off.)
6 - Return to step #2.

And finally, here's my current 'outline' code for both.

Clock:

#define XBEE_ID 1
int check = 0;

void setup() {
  Serial.begin(57600);
  delay(1000);
  Serial.print("READY!");
}

void loop() {
  
  switch (check){
    case 0:
    {
      //Wait for Timestamp
      waitToRead("TIME");
      Serial.println("Time now set");
      check = 1;
      break;
    }
    
    case 1:
    {
      //Collect Data
      delay(3000);
      
      check = 2;
      break;
    }
    
    case 2:
    {
     //Send Data to Hub
     delay(1000);
     Serial.print("DATAHUB!");
     check = 0;
    }
    
  }
}

void waitToRead(String wantString){
 String readStr = "";
 
 //looking for a string terminated by a ! - "hello!" "bye!" "66r3!", etc.
 while(!wantString.equals(readStr)){
  while (Serial.available()) {
    delay(10);  //small delay to allow input buffer to fill

    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '!') {
      break;
    }  //breaks out of capture loop to print readstring
    readStr += c; 
  } //makes the string readString  
 }
}

and

Hub:

#define XBEE_ID 0
int check = 0;

void setup() {
  Serial.begin(57600);
  delay(100);
  //FIRST CONNECTION
  
  waitToRead("READY");
}

void loop() {
  switch (check){
   case 0:
   {
     delay(2000);
     Serial.print("TIME!");
     check = 1;
   }
   case 1:
   {
     waitToRead("DATAHUB");
     check = 0;
   }
  }
  
}

void waitToRead(String wantString){
 String readStr = "";
 
 //looking for a string terminated by a ! - "hello!" "bye!" "66r3!", etc.
 while(!wantString.equals(readStr)){
  while (Serial.available()) {
    delay(10);  //small delay to allow input buffer to fill

    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '!') {
      break;
    }  //breaks out of capture loop to print readstring
    readStr += c; 
  } //makes the string readString  
 }
 
}

Now obviously that isn't everything - I actually have the entirety of the "meat" of the programs written in another file, where everything's running simultaneously, but I want to put it into this framework and get more control over everything. Aaaand I'm stuck. Nothing really seems to work. I'm not coming up with a clean enough way to switch between 'reading' and 'sending', and the XBees are either getting nothing at all, or compounding the message with the last thing received and going into a sort of feedback loop.

Any advice at all as to how to control this system better would be wonderful. Unfortunately the obvious one (change the hardware!) isn't going to happen. Gotta work with what I've got. However, all my hardware is (at least at the moment!) set up correctly - my XBees are sending and receiving perfectly, if I load simple programs. The problem now is just the code.

Also - since the XBees are using the Serial port, is there a nice way for me to print debugging code without getting in their way? Not being able to test parts of my code has been a huge pain.

First question is why doesn't the hub simply tell the clock to start collecting data, and then tell the clock to send the data? That way, the hub is responsible for the time, and the clock does not care precisely what time it is?

Also - since the XBees are using the Serial port

Why? If you are using crappy shields that enforce this, get decent ones that don't. I like these: SparkFun XBee Shield - WRL-12847 - SparkFun Electronics

Ah, that probably is a slightly simpler way of handling the timing, I'll probably change that. Good catch.

As for the other question - I was actually doing that originally, but was given a big PCB that I'm using the TLC5940 library to interface with for the clock (There's a big LED system as well). It's eating up nearly all of my other pins, including 2 & 3.

Definitely worth revisiting whether or not I can figure out a way around using the Serial port for my XBees, but I haven't found a way as of yet. Sorry, I should have made that clearer.

but was given a big PCB that I'm using the TLC5940 library to interface with for the clock (There's a big LED system as well). It's eating up nearly all of my other pins, including 2 & 3.

So, you're not using a shield? What pins do you have free? Are the analog pins all being used, too? If not, they can be used as digital pins.