I don't understand the order of execution in Processing. [SOLVED THANK YOU]

Dear anyone,

I am having trouble with a very simple program. I get the time and date from the serial port, and I send it back with some analog sensor reading. The first time the program loops, the time and date values are blank - but each subsequent loop works just fine. Kindly look at my code and tell me if there is something very obvious that I am missing. Thanking you in advance,

UPDATE: Thank you Gardner. You set me on the right track. I put a delay before the gettimefunction executed. What was happening was I was plugging in the board to my computer, and as soon as it powered up, it immediately sent the gettimefunction before I had a chance to turn Gobetwino on, and the command went unanswered. Thanks again.

John

int analogPin = 3;
String currtime;
String currdate;
char commandbuffer[100];
int logValue1=0;
int logValue2=0;
int logValue3=0;

void setup() 
{ 
  Serial.begin(9600); 
} 
 
void loop() 
{
   gettimefunction();  // Why doesn't this run first??
   logValue1= analogRead(analogPin);    // read the input pin 
   logData(currtime,currdate,logValue1,logValue2,logValue3); //sends time, date, and 3 analog sensor values  //this runs first!

}

void logData( String value1, String value2, int value3, int value4, int value5) 
{
   delay(5000);  
   char buffer[5];
   Serial.print("#S|SEND|[");
   Serial.print(value1);
   Serial.print(",");
   Serial.print(value2);
   Serial.print(",");
   Serial.print(itoa((value3), buffer, 10));
   Serial.print(",");
   Serial.print(itoa((value4), buffer, 10));
    Serial.print(",");
   Serial.print(itoa((value5), buffer, 10));
   Serial.println("]#");
    
} 

 void gettimefunction () {

  Serial.println("#S|T|[]#"); //get time from Gobetwino
  delay(200);
   
    int i=0;
    
  if(Serial.available()){
     delay(100);
     while( Serial.available() && i< 99) {
        commandbuffer[i++] = Serial.read();
     }
     commandbuffer[i++]='\0';
currtime=commandbuffer;
  }



  Serial.println("#S|D|[]#"); //get date from Gobetwino
  delay(200);
   
    int f=0;
    
  if(Serial.available()){
     delay(100);
     while( Serial.available() && i< 99) {
        commandbuffer[f++] = Serial.read();
     }
     commandbuffer[f++]='\0';
currdate=commandbuffer;
  }


}
 Serial.println("#S|T|[]#"); //get time from Gobetwino
  delay(200);
   
    int i=0;
    
  if(Serial.available()){

Perhaps you are not waiting long enough for an answer. If Serial.available() does not indicate a reply is received, the code drops through and returns without setting the time. Next time when you do this again, the answer to the first query is queued up, and Serial.available() will show a timestamp ready for you to read.

Thank you for the reply. Increasing the delay to 1 second seems to have no effect. I am interfacing with the Gobetwino program. Here is the response from it's status window:

6/22/2011 2:47:45 PM Serial port : COM3 opened at 9600 baud 6/22/2011 2:47:50 PM Commandstring recieved : #S|SEND|[,,374,0,0]# //log data function runs first. 6/22/2011 2:47:50 PM Command parsed OK 6/22/2011 2:47:50 PM Executing command : SEND 6/22/2011 2:47:50 PM Data logged to C:\Documents and Settings\John\Desktop\MTSENSOR\SENSORDATA.txt 6/22/2011 2:47:50 PM Commandstring recieved : #S|T|[]# //gettimefunction runs second. 6/22/2011 2:47:50 PM Command parsed OK 6/22/2011 2:47:50 PM Executing command : T 6/22/2011 2:47:51 PM Commandstring recieved : #S|D|[]# 6/22/2011 2:47:51 PM Command parsed OK 6/22/2011 2:47:51 PM Executing command : D 6/22/2011 2:47:57 PM Commandstring recieved : #S|SEND|[02:47:50 PM,6/22/2011,368,0,0]#