Go Down

Topic: code minimizing (Read 634 times) previous topic - next topic

louish

Sep 21, 2011, 08:43 pm Last Edit: Sep 22, 2011, 12:13 am by louish Reason: 1
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);
  }
}
}
louis

AWOL

#1
Sep 21, 2011, 08:49 pm Last Edit: Sep 21, 2011, 08:53 pm by AWOL Reason: 1
Code: [Select]
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
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

louish

louis

AWOL

Like nostalgia, 100% isn't what it used to be.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
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.

AWOL

#5
Sep 21, 2011, 09:07 pm Last Edit: Sep 21, 2011, 09:11 pm by AWOL Reason: 1
Ah! I missed the italics < smacks forehead >  
< smacks OP for not using code box >
<further smacks OP for cross-posting >

( I'd still prefer to see explicit termination - I don't think that can be blamed on a lack of code box)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
I'd still prefer to see explicit termination

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

AWOL

I'm not sure - and I can do it !   ]:D
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

louish

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 :
Code: [Select]
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);
   }
}
louis

PaulS

Code: [Select]
      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.

Code: [Select]
   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.

Go Up