Problem with while loop

Hello everyone

This is my first post and I hope you can help me.

I’m making a project and I get the values from a gps Ublox-NEO-6M, and send those values via xbee when I send a character like the letter ‘s’.

Now what i want is send another character like ‘a’ and turn on a led.

To get the gps values I have a do-while infinity loop for get constantly the values from gps. I thought that inside the do-while loop there should be another condition like an if loop or another do while loop but I don’t have any succes with that.

This is a copy from my code. i hope anyone can help me. Thanks!

Code:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;

SoftwareSerial ss(RXPin, TXPin);

SoftwareSerial xb(6,5);

char inicio;
int val=0;

void setup()
{
xb.begin(9600);
ss.begin(GPSBaud);
pinMode(9,OUTPUT);
}

void loop()
{
xb.listen();
if(xb.available()>0)
{
char inicio = xb.read();

if(inicio == ‘s’)//Do-While loop for getting the values from gps
{
val=1;

do{
printInt(gps.satellites.value(),gps.satellites.isValid(), 5);xb.print(",");
printFloat(gps.location.lat(),gps.location.isValid(),11,6);xb.print(",");
printFloat(gps.location.lng(),gps.location.isValid(),12,6);xb.print(",");
printDateTime(gps.date,gps.time);xb.print(",");
printFloat(gps.altitude.meters(),gps.altitude.isValid(),7,2);xb.print(",");
printFloat(gps.speed.mps(),gps.speed.isValid(),6,2);xb.print("&");

xb.println();
smartDelay(250);

if (inicio==‘a’) //Loop for turn on the led (NOT WORKING)
{
digitalWrite(9,HIGH);
}

if (millis() > 5000 && gps.charsProcessed() < 10)
xb.println(F(“No GPS data received: check wiring”));
}

while(val=1);
}
}

}

//Only functions for the data from gps

void smartDelay(unsigned long ms)
{
unsigned long start = millis();

ss.listen();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}

void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
xb.print(’*’);
xb.print(’ ‘);
}
else
{
xb.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
xb.print(’ ');
}
smartDelay(0);
}

void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = “*****************”;
if (valid)
sprintf(sz, “%ld”, val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz = ’ ';

  • if (len > 0)*

  • sz[len-1] = ’ ';*

  • xb.print(sz);*

  • smartDelay(0);*
    }
    void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
    {

  • if (!d.isValid())*

  • {*
    _ xb.print(F("********** "));_

  • }*

  • else*

  • {*

  • char sz[32];*

  • sprintf(sz, “%02d/%02d/%02d ,”, d.month(), d.day(), d.year());*

  • xb.print(sz);*

  • }*

  • if (!t.isValid())*

  • {*
    _ xb.print(F("******** "));_

  • }*

  • else*

  • {*

  • char sz[32];*

  • sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());*

  • xb.print(sz);*

  • }*

  • smartDelay(0);*
    }
    void printStr(const char *str, int len)
    {

  • int slen = strlen(str);*

  • for (int i=0; i<len; ++i)*
    _ xb.print(i<slen ? str : ’ ');_
    * smartDelay(0);*
    }

To get the gps values I have a do-while infinity loop for get constantly the values from gps.

No, you don't. loop() is already an infinite loop.

smartDelay() is a stupid function. Get rid of it. Do it right.

void loop()
{
   while (ss.available())
   {
      if(gps.encode(ss.read())
      {
         // A complete sentence was received. Do something useful with the data
      }
   }

   // Whatever else you need to do goes here
}

like an if loop

There is no such thing as an if loop. An if statement does not loop.

You are beating a dead horse trying to listen to two different software serial ports. Give it up now. That horse isn't going anywhere.