Pages: [1]   Go Down
Author Topic: code minimizing  (Read 569 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
   }
 }
 }
« Last Edit: September 21, 2011, 05:13:57 pm by louish » Logged

louis

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
« Last Edit: September 21, 2011, 01:53:32 pm by AWOL » Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it works 100%
Logged

louis

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49037
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
« Last Edit: September 21, 2011, 02:11:29 pm by AWOL » Logged

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49037
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'd still prefer to see explicit termination
I think that's a little extreme for not using a code box... smiley-cool
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure - and I can do it !   smiley-twist
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 32
Neither a lofty degree of intelligence nor imagination nor both together go to the making of genius. Love, love, love, that is the soul of genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
   }
}
Logged

louis

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49037
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      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:
   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.
Logged

Pages: [1]   Go Up
Jump to: