Nested conditions

#include <Adafruit_GPS.h>
Adafruit_GPS GPS(&Wire);

String op = "";         // Init str
String gpsString = "";  // Init str
char g_data = "";

void setup() {
  GPS.begin(0x10);
  Serial.begin(115200);
  Serial.println("GPS data disp on SSD1306");
}

void loop() {
  g_data = GPS.read();
  if (g_data == "$") {
    op = String(g_data);
    while ((int(g_data)) != 10) {
      g_data = GPS.read();
      op = op + String(g_data);
    }
  }
  Serial.print(op);
}

Are using nested if's and while's supported please?

Yes, nested if's and while's are supported.

Ok thanks...

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  char OneChar;
  char *str;
  str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM
  // log_i("Free PSRAM before String: %d", ESP.getFreePsram());
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == '>')
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
               xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &str );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          strncat( str, &OneChar, 1 );
        }
        else
        {
          if ( OneChar == '<' )
          {
            strcpy( str, ""); // clear string buffer
            BeginSentence = true; // found beginning of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
    //        log_i( "fReceiveSerial_LIDAR " );
    //        log_i(uxTaskGetStackHighWaterMark( NULL ));
  }
  free(str);
  vTaskDelete( NULL );
} //void fParseSerial( void * parameters  )

The language is C++. Any language questions can be answered by referencing any of 1000s of C++ references and tutorials.

Thanks for the example, much appreciated..

Thanks for the note...

The compiler itself will be more than happy with any level of nesting you care to throw at it, but there's a catch: you won't!. In practice, anything deeper than 2 or 3 levels of nesting, especially if it spans several monitor windows, is going to become difficult to follow/understand, and the moment this happens, you will not be able to maintain it anymore. Let 6 or 12 months pass, the memory of why exactly you did this or that fades away a bit, and now who is going to touch up or add features to that mess?

It is much better to write small functions which do one small thing and call them as needed, have one function call another one and so on.

Yep, get it, pretty documentation obsessed. That little if, while will be void un to itself later, just to build up the whole string into a single variable so I can strip out the data I need to display etc. If I could have reduced it to a more simple set of instructions I would have. Thanks for the feedback...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.