Go Down

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

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