Arduino & Crashing?? Please Help!

Hello GNU_Linux/Mem/Other,

If i run my code on the arduino and when it gets by the buzzer i need to wait between the 0.1/1 seconds... if i remove the timeout the arduino will crash...? Why???

C:\htdocs\public_html>perl final.cgi
DEBUG SEND: --! 016LED09001001 (Time: 0.000679 Seconds)
DEBUG SEND: --! 016LED09001000 (Time: 0.101607 Seconds)
DEBUG SEND: --! 016LED09001001 (Time: 0.202699 Seconds)
DEBUG SEND: --! 016LED09001000 (Time: 0.309574 Seconds)
DEBUG SEND: --! 016LED09001001 (Time: 0.410678 Seconds)
DEBUG SEND: --! 016LED09001000 (Time: 0.515625 Seconds)
DEBUG SEND: --! 016LED09001001 (Time: 0.616742 Seconds)
DEBUG SEND: --! 016LED09001000 (Time: 0.717655 Seconds)
DEBUG SEND: --! 016LED09000000 (Time: 0.818596 Seconds)
DEBUG SEND: --! 016SPK08003050 (Time: 1.8125 Seconds) <--- if i remove the 1 second after the beep from the speaker the arduino will crash....
DEBUG SEND: --! 016LED09000002 (Time: 2.8125 Seconds)
DEBUG SEND: --! 016LED09000004 (Time: 2.82349 Seconds)
DEBUG SEND: --! 016LED09000006 (Time: 2.835572 Seconds)
DEBUG SEND: --! 016LED09000008 (Time: 2.846491 Seconds)
DEBUG SEND: --! 016LED09000010 (Time: 2.857489 Seconds)
DEBUG SEND: --! 016LED09000012 (Time: 2.869548 Seconds)
DEBUG SEND: --! 016LED09000014 (Time: 2.881564 Seconds)
DEBUG SEND: --! 016LED09000016 (Time: 2.892487 Seconds)
DEBUG SEND: --! 016LED09000018 (Time: 2.903488 Seconds)
DEBUG SEND: --! 016LED09000020 (Time: 2.915486 Seconds)
DEBUG SEND: --! 016LED09000022 (Time: 2.926474 Seconds)
DEBUG SEND: --! 016LED09000024 (Time: 2.93747 Seconds)
DEBUG SEND: --! 016LED09000026 (Time: 2.948592 Seconds)
DEBUG SEND: --! 016LED09000028 (Time: 2.960558 Seconds)
DEBUG SEND: --! 016LED09000030 (Time: 2.971467 Seconds)
DEBUG SEND: --! 016LED09000032 (Time: 2.982548 Seconds)
DEBUG SEND: --! 016LED09000034 (Time: 2.994513 Seconds)
DEBUG SEND: --! 016LED09000036 (Time: 3.005455 Seconds)
DEBUG SEND: --! 016LED09000038 (Time: 3.016501 Seconds)
DEBUG SEND: --! 016LED09000040 (Time: 3.027559 Seconds)
DEBUG SEND: --! 016LED09000042 (Time: 3.03953 Seconds)
DEBUG SEND: --! 016LED09000044 (Time: 3.050449 Seconds)
DEBUG SEND: --! 016LED09000046 (Time: 3.061452 Seconds)
DEBUG SEND: --! 016LED09000048 (Time: 3.072489 Seconds)
DEBUG SEND: --! 016LED09000050 (Time: 3.084491 Seconds)
DEBUG SEND: --! 016LED09000052 (Time: 3.095442 Seconds)
DEBUG SEND: --! 016LED09000054 (Time: 3.106462 Seconds)
DEBUG SEND: --! 016LED09000056 (Time: 3.118506 Seconds)
DEBUG SEND: --! 016LED09000058 (Time: 3.130503 Seconds)
DEBUG SEND: --! 016LED09000060 (Time: 3.141439 Seconds)
DEBUG SEND: --! 016LED09000062 (Time: 3.152484 Seconds)
DEBUG SEND: --! 016LED09000064 (Time: 3.164469 Seconds)
DEBUG SEND: --! 016LED09000066 (Time: 3.175425 Seconds)
DEBUG SEND: --! 016LED09000068 (Time: 3.186463 Seconds)
DEBUG SEND: --! 016LED09000070 (Time: 3.197499 Seconds)
DEBUG SEND: --! 016LED09000072 (Time: 3.209492 Seconds)
DEBUG SEND: --! 016LED09000074 (Time: 3.220419 Seconds)
DEBUG SEND: --! 016LED09000076 (Time: 3.231448 Seconds)
DEBUG SEND: --! 016LED09000078 (Time: 3.243444 Seconds)
DEBUG SEND: --! 016LED09000080 (Time: 3.254488 Seconds)
DEBUG SEND: --! 016LED09000082 (Time: 3.284431 Seconds)
DEBUG SEND: --! 016LED09000084 (Time: 3.299421 Seconds)
DEBUG SEND: --! 016LED09000086 (Time: 3.310437 Seconds)
DEBUG SEND: --! 016LED09000088 (Time: 3.328125 Seconds)
DEBUG SEND: --! 016LED09000090 (Time: 3.340078 Seconds)
DEBUG SEND: --! 016LED09000092 (Time: 3.351056 Seconds)
DEBUG SEND: --! 016LED09000094 (Time: 3.362096 Seconds)
DEBUG SEND: --! 016LED09000096 (Time: 3.374271 Seconds) <--- it crashes here if i say it is an analog led pin must be brightnes 96 but it hangs and the arduino stops working till ill press the reset button and run the script again. See above with the '<---' mark

Can it be that i run things too fast??

Arduino Code:

#include <string.h>

int i = 0;

char *MSD;
char MSG[50];

char PIN[4];
char VAL[4];
char OPT[4];
char CMD[4];

int TPA, TPB, TPC, TPD;
int MSL;

void setup () {
  beginSerial (19200);
}

void loop () {
 
  if (Serial.available () > 0) {

       MSG[i] = Serial.read (); i++;

    if (i == 3) { 
      MSD = MSG;
      MSL=atoi(MSD); 
      //MSL *= 1;
    
      if (MSL <= 5) {
        Serial.println ("ERR001"); 
      }
    }

    if (i == MSL && MSL != '0') {
      
      strncpy(CMD, MSG +  3, 3);
      strncpy(PIN, MSG +  6, 2);
      strncpy(OPT, MSG +  8, 3);
      strncpy(VAL, MSG + 11, 3);
        
          TPA=atoi(PIN); //TPA *= 1;
          TPB=atoi(OPT); //TPB *= 1;
          TPC=atoi(VAL); //TPC *= 1;

      //-------------------------------------------------------
      // Functions
      //-------------------------------------------------------

      if (strncmp(CMD, "MSG", 3) == 0) {
          Serial.print (MSG);
      }

      //-------------------------------------------------------

      if (strncmp(CMD, "LED", 3) == 0) {
        
          pinMode(TPA, OUTPUT);
        
        if (TPB == 1) {
          if (TPC == 1) { digitalWrite(TPA, HIGH); }
          if (TPC == 0) { digitalWrite(TPA,  LOW); } 
        } else {
            analogWrite(TPA, TPC);
        }
      }

      //-------------------------------------------------------

      if (strncmp(CMD, "SPK", 3) == 0) {
        
          pinMode(TPA, OUTPUT);
        
        for (long l = 0; l < TPC * 1000L; l += TPB * 2) {
          digitalWrite(TPA, HIGH);
          delayMicroseconds(TPB);
          digitalWrite(TPA, LOW);
          delayMicroseconds(TPB);
        }
      }

      //-------------------------------------------------------

          i = 0;
        
    }
  }
}