Code not showing Values on Second input

This my COde that takes values like or or further as you can see on further processing the ‘<’ & ‘>’ are removed and subsequently pl and pr are also removed and just the number is left that is then fed into analogWrite for pwm, The problem is for the first time it works but for the second time it shows Zero it means the second time the code is going haywire which im not till now able to find why.

char inData[20]; // Allocate some space for the string
char inChar = 0; // Where to store the character read
int index = 0; // Index into array; where to store the character
int slaveSel = 0;
String Data;
int intData, pan;
boolean started, ended, panTimer;
long previousMillis = 0; 
long interval = 2420;
int pan1 = 9;
int pan2 = 6;
int tilt1 = 5;
int tilt2 = 3;

void setup() 
{
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  pinMode(pan1, OUTPUT);
  pinMode(pan2, OUTPUT);
  pinMode(tilt1, OUTPUT);
  pinMode(tilt2, OUTPUT);
  Serial.begin(9600);  
}
void loop() 
{
while(Serial.available() > 0) // Don't read unless
    // there you know there is data
  {
      inChar = Serial.read(); // Read a character//read the character
      if(inChar =='<') //not sure what to put in if statement to run until end
      {
        started = true;
        index=0;
      }
      else if(inChar =='>')
      {
        ended = true;
        break;
      } 
      if(started)
      { 
       inData[index] = inChar; // Store it
       index++; // Increment where to write next
       inData[index] = '\0'; // Null terminate the string    
      }
  } 
  if (ended) 
  {
      Data = inData;//values of acclerometer like "545X" etc enter and stored in Data
      //char CharData = Data.charAt(6);
      //intData = int(CharData);
      process();
  }
  
}

void process() 
{
       Data = Data.replace('<', ' ');
       Data = Data.replace('>', ' ');
       Data = Data.trim();
       char chkPan = Data.charAt(0);
       char chkPan1 = Data.charAt(1);
       if (chkPan == 'p' && chkPan1 == 'r') 
       {
         Data = Data.replace('p', ' ');
         Data = Data.replace('r', ' ');
         Data = Data.trim(); 
         char DataChar[5];//char Array to store the char conversion values from DataToInt
         Data.toCharArray(DataChar, sizeof(DataChar));
         intData = atoi(DataChar);
         //if (panTimer == true) pan=0;  else panTimer = true;
         analogWrite(pan1, intData);
         digitalWrite(pan2, LOW);
         Serial.println(intData);
         for(int i=0;i<10;i++)
           {
           inData[i]=0;
           }
         index=0;
         intData = 0;
         Serial.flush(); 
       }  
        if (chkPan == 'p' && chkPan1 == 'l') 
       {
         Data = Data.replace('p', ' ');
         Data = Data.replace('l', ' ');
         Data = Data.trim(); 
         char DataChar[5];//char Array to store the char conversion values from DataToInt
         Data.toCharArray(DataChar, sizeof(DataChar));
         intData = atoi(DataChar);
         //if (panTimer == true) pan=0;  else panTimer = true;
         analogWrite(pan2, intData);
         digitalWrite(pan1, LOW);
         Serial.println(intData);
         for(int i=0;i<10;i++)
           {
           inData[i]=0;
           }
         index=0;
         intData = 0;
         Serial.flush(); 
       }      
         if (Data == "pstp") {
         digitalWrite(pan1, LOW);
         digitalWrite(pan2, LOW);
         Serial.println(Data);
         for(int i=0;i<10;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();   
         } 
        if (Data == "tfr")//Here put the condition as per the values received from the Accelerometer
         {
         digitalWrite(tilt1,HIGH);
         digitalWrite(tilt2, LOW);
         Serial.println(Data);
         //Serial.println(millis());
         for(int i=0;i<10;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();        
         } 
        if (Data == "tst") {
         digitalWrite(tilt1, LOW);
         digitalWrite(tilt2, LOW);
         Serial.println(Data);
         for(int i=0;i<10;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();   
       }               
       if (Data == "tbk")//Here put the condition as per the values received from the Accelerometer
       {
         digitalWrite(tilt1,LOW);
         digitalWrite(tilt2, HIGH);
         Serial.println(Data);
         //Serial.println(millis());
         for(int i=0;i<10;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();        
       }      /*
       if (panTimer == true) {
         unsigned long currentMillis = millis();
         if(currentMillis - previousMillis > interval) {
         
          previousMillis = currentMillis;   
          pan++;
          if (pan == 7) {
             digitalWrite(pan1, LOW);
             digitalWrite(pan2, LOW);
             panTimer == false;
             pan = 0;
          }
        }
       }*/
}

You take a perfectly good null-terminated string, Copy it into a String, do a load of memory intensive fiddling with it, then turn it back into a null-terminated string, just to convert it to a number. Interesting technique.

You don't seem to be resetting started and ended after you have processed your data.

I think the problem is that you leave ‘ended’ set to true when you process your first input. Then the second time through if there is not a full message in the buffer it gets processed anyway because ‘ended’ is true.

Add these lines:

            started = true;
            ended = false;   //////////  Add this
            index = 0;
 if (ended) 
  {
 ended = false;  /////////  Add this

Alternatively you can use a single state variable:

enum {WAITING_FOR_START, STARTED} state = WAITING_FOR_START;

switch (state)
    {
case WAITING_FOR_START:
    if (inChar == '<')
        {
        state = STARTED;
        index = 0;
        }
    break

case STARTED:

    if (inChar == '>')
        {
        inData[index] = '\0';  // Terminate the string
        process();
        state = WAITING_FOR_START;
        }
    else
        inData[index++] = inChar;
    break;
   }

Note that this code doesn’t put the ‘<’ and ‘>’ in the buffer so they don’t need to be removed in process().

just to convert it to a number. Interesting technique.

actually this is NOT TO JUST HAVE A number the p stands for pan and r and small L stands for right and left pan movement , when the comes it makes sure to drive that condition only that will put analogWrite PWM values to turn it right but on a specific PWM speed ,all these values are received over XBee pro and it seems to work well with this sketch TOTALLY FINE untill i got those Speed PWM functioning in there, Still the TILT of the BASCOR PAN TILT works very very fine so i DO not think the resetting of started and ended is needed(if definitely needed please tell some detailed viewpoint)

TOTALLY FINE WORKING SKETCH:

char inData[20]; // Allocate some space for the string
char inChar = 0; // Where to store the character read
int index = 0; // Index into array; where to store the character
int slaveSel = 0;
String Data;
int intData, pan;
boolean started, ended, panTimer;
long previousMillis = 0; 
long interval = 2420;
int pan1 = 9;
int pan2 = 6;
int tilt1 = 5;
int tilt2 = 3;

void setup() 
{
  pinMode(13, OUTPUT);
  pinMode(pan1, OUTPUT);
  pinMode(pan2, OUTPUT);
  pinMode(tilt1, OUTPUT);
  pinMode(tilt2, OUTPUT);
  Serial.begin(9600);  
  for (int pinNumber = 3; pinNumber <= 7; pinNumber++)
  {//function to read a jumper out of 5 jumpers 
       pinMode(pinNumber, INPUT); 
       if (digitalRead(pinNumber)==1) {
          slaveSel = pinNumber;
       }
  }
}
void loop() 
{
while(Serial.available() > 0) // Don't read unless
    // there you know there is data
  {
      inChar = Serial.read(); // Read a character//read the character
      if(inChar =='<') //not sure what to put in if statement to run until end
      {
        started = true;
        index=0;
      }
      else if(inChar =='>')
      {
        ended = true;
        break;
      } 
      if(started)
      { 
       inData[index] = inChar; // Store it
       index++; // Increment where to write next
       inData[index] = '\0'; // Null terminate the string    
      }
  } 
  if (ended) 
  {
      Data = inData;//values of acclerometer like "545X" etc enter and stored in Data

      char CharData = Data.charAt(6);//Taking the 4th last character from extreme Right and storing in CharData to check for AXIS
      intData = int(CharData);
      if (intData == slaveSel) 
      {  
        digitalWrite(13, HIGH); 
      }
      if (bitRead(PORTB, 6)){
        //process();
      }
  }
  process();
}

void process() 
{
       Data = Data.replace('<', ' ');
       Data = Data.replace('>', ' ');
       Data = Data.trim();
   if (Data == "panCLKwise")//Here put the condition as per the values received from the Accelerometer
       {
         if (panTimer == true) pan=0;  else panTimer = true;
         digitalWrite(pan1,HIGH);
         digitalWrite(pan2, LOW);
         Serial.println(Data);
         Serial.println(millis());
         for(int i=0;i<10;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();        
       } 
   if (Data == "PANSTOP") {
         digitalWrite(pan1, LOW);
         digitalWrite(pan2, LOW);
         Serial.println(Data);
         for(int i=0;i<10;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();   
       }      
   if (Data == "panACLKwise")//Here put the condition as per the values received from the Accelerometer
       {
         if (panTimer == true) pan=0;  else panTimer = true;
         digitalWrite(pan1,LOW);
         digitalWrite(pan2, HIGH);
         Serial.println(Data);
         Serial.println(millis());
         for(int i=0;i<10;i++)
         {
           inData[i]=0;
         }
         index=0;
         intData = 0;
         Serial.flush();        
       } 
         
       if (panTimer == true) {
         unsigned long currentMillis = millis();
         if(currentMillis - previousMillis > interval) {
         
          previousMillis = currentMillis;   
          pan++;
          if (pan == 7) {
             digitalWrite(pan1, LOW);
             digitalWrite(pan2, LOW);
             panTimer == false;
             pan = 0;
          }
        }
       }
}

Note that this code doesn’t put the ‘<’ and ‘>’ in the buffer so they don’t need to be removed in process().

That is a good Idea Sir.John

actually this is NOT TO JUST HAVE A number the p stands for pan and r and small L stands for right and left pan movement , when the comes it makes sure to drive that condition only that will put analogWrite PWM values to turn it right but on a specific PWM speed ,all these values are received over XBee pro and it seems to work well with this sketch TOTALLY FINE untill i got those Speed PWM functioning in there, Still the TILT of the BASCOR PAN TILT works very very fine so i DO not think the resetting of started and ended is needed(if definitely needed please tell some detailed viewpoint)

There is still no need to convert your null-terminated string into a String. It’s just wasting memory.

Resetting started and ended is necessary, because, as John said, process() will continue to be called whether a complete string has been received or not.

@ dxwood ,thanks for your reply again, ON SIR JOHN's suggestion on the second part here i have a doubt that if i add make false the ended then it will never run the if condition so should it not be TRUE? :

if (ended) 
  {
 ended = false;  /////////  Add this

EDIT.. OK OK i got it was going haywire, sorry for that

DONE!.....Fixed