code minimizing

heyy every budy ... im trying to save 2 float number whom im receiving via xbee ... im using packet mrkers method {} () /// im able to save them in 2 variables but my code is very long so i was woundering if any one can help me minimizing it ... here is my code :

void setup()
{
Serial.begin(9600);
}
void loop()
{
char charIn = 0;
byte i = 0;
char stringIn[32] = "";

char startchar;
float availableData = Serial.available();

if ( availableData > 0 ) {
startchar = Serial.read();
if ( startchar == '{' ) {
while ( charIn != '}' ) {
if ( Serial.available() > 0 ) {
charIn = Serial.read();
if ( charIn == '}' ) {
break;
}
stringIn = charIn;

  • i += 1;*
  • }*
  • }*
  • float lat1= atof(stringIn);*
  • Serial.println(lat1,7);*
  • delay(3000);*
  • }*

if ( startchar == '(' ) {

  • while ( charIn != ')' ) {*
  • if ( Serial.available() > 0 ) {*
  • charIn = Serial.read();*
  • if ( charIn == ')' ) {*
  • break;*
  • }*
    _ stringIn = charIn;_
    * i += 1;*
    * }*
    * }*
    * float lon= atof(stringIn);*
    * Serial.println(lon,7);*
    * delay(3000);*
    * }*
    }
    }

stringIn = charIn;I can't see that that works at all - are you sure?

Have a look here:
http://arduino.cc/forum/index.php/topic,73042.0.html

it works 100%

Like nostalgia, 100% isn't what it used to be.

I can't see that that works at all - are you sure?

It is because the [ i ] got stripped out by the forum software, because the code is not in a code box. With the index in place, the code is perfectly valid.

Ah! I missed the italics < smacks forehead >
< smacks OP for not using code box >

( I'd still prefer to see explicit termination - I don't think that can be blamed on a lack of code box)

I'd still prefer to see explicit termination

I think that's a little extreme for not using a code box... 8)

I'm not sure - and I can do it ! ]:smiley:

heyy its me again ... i tryied paulS code and it works perfect for the first marker /// but for some reson its not getting the secound number itryed several wayes to modify the code to accept 2 markere with no succes here is my last code it only print the first number :

boolean started = false;
boolean ended = false;
boolean started2 = false;
boolean ended2 = false;

char inData[24]; // Size as appropriate
byte index = 0;
char inData2[24];
byte index2 = 0;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
   while(Serial.available() > 0)
   {
      char inChar = Serial.read();
      if(inChar == '{' && inChar != '(' )
      {
         started = true;
         index = 0;
         inData[index] = '\0';
      }
      else if(inChar == '}')
      {
         ended = true;
         break;
      }
      else if( inChar != '(')
      {
         if(index < 24-1) // Array size
         {
            inData[index++] = inChar;
            inData[index] = '\0';
         }
      }
   else   if(inChar == '(')
      {
         started2 = true;
         index2 = 0;
         inData2[index2] = '\0';
      }
      else if(inChar == ')')
      {
         ended2 = true;
         break;
      }
      else 
      {
         if(index2 < 24-1) // Array size
         {
            inData2[index2++] = inChar;
            inData2[index2] = '\0';
         }
      }
   }

   if(started && ended)
   {
      // Parse the data in inData here...
      float lat1= atof(inData);
         Serial.println(lat1,7);
         delay(3000);
   }
 if(started2 && ended2)
   {
      // Parse the data in inData here...
      float lon= atof(inData2);
         Serial.println(lon,7);
         delay(3000);
   }
}
      if(inChar == '{' && inChar != '(' )

If inChar IS a { it is NOT a ( by definition. The && clause is not needed.

Look at your tests. What causes a character to be written into the inData2 array?

Nothing. If the character is not a {, }, (, or ), it will be stored in inData (if there is room).

And this is OK. It just means that in the if(started2 && ended2) block, you need to use inData instead of inData2. It also means that inData2 and index2 are not needed.

It also means that started2 and ended2 are not needed.

   while(Serial.available() > 0)
   {
      char inChar = Serial.read();
      if(inChar == '{' || inChar == '(' )
      {
         started = true;
         index = 0;
         inData[index] = '\0';
      }
      else if(inChar == '}' || inChar == ')')
      {
         ended = true;
         break;
      }
      else
      {
         if(index < 24-1) // Array size
         {
            inData[index++] = inChar;
            inData[index] = '\0';
         }
      }

The only issue here is that in the if(started && ended) block, you do not know whether the packet markers were { and } or ( and ). If this is important, add a char variable to store the start marker, and set it in the first if block (to inChar), and test that character in the if(started && ended) block.