title: GPS receiver + Ublox NEO-6M - Having trouble making a timeout
body: <p>I'm trying to write some code that will turn on the gps module and wait for data to arrive, so that I can then do things with it. I don't need a continuous stream; I only need the first lat/long  received from the gps unit, then I can turn it off. I also need this to just stop if it can't find it's location within a certain amount of time (20 seconds for example)</p>
  
  <p>Here is some simple code that I've tried just to get the gps to read (full code with timeout comes later)</p>
  
  <pre><code>    #include &lt;TinyGPS++.h&gt;
  
      #include &lt;SoftwareSerial.h&gt;
      TinyGPSPlus gps;  // The TinyGPS++ object
      SoftwareSerial ss(D4, D3); // The serial connection to the GPS     
      float latitude , longitude;
      int count = 0;
  
  
      void setup()
      {
  
        Serial.begin(9600);
        ss.begin(9600);
        Serial.println();
        Serial.print("Starting");
  
      }
  
      void loop()
      {
        while (ss.available() &gt; 0)
          if (gps.encode(ss.read()))
          {
            if (gps.location.isValid())
            {
              latitude = gps.location.lat();
              lat_str = String(latitude , 6);
              longitude = gps.location.lng();
              lng_str = String(longitude , 6);
            }
  
          }
        Serial.println(lat_str + ":" + lng_str);
        delay(100);
        count += 1;
        String Count = String(count);
        Serial.println(Count);
      }
  </code></pre>
  
  <p>When the delay above is 100, then everything works fine, when the delay is 1000, then suddenly no data comes through.</p>
  
  <p>Here is the full code I've tried that includes my timeout and breakout condition, again not working. The cutoff line is in the GPS_mode function here:  while ((stoploop &lt; 1) &amp;&amp; (previous - startTime &lt; TimeOut)).</p>
  
  <pre><code>    #include &lt;SoftwareSerial.h&gt;
      #include &lt;TinyGPS++.h&gt;
      TinyGPSPlus gps;  // The TinyGPS++ object
      SoftwareSerial ss(D4, D3); // The serial connection to the GPS d
  
      int stoploop = 0; //used for stopping gps once a signal lock or     timeout is reached
      unsigned long startTime = millis(); // timer used for gps timeout
      const int gpsCutoffPin = D1;
  
      void setup() {
        //initialise the serial monitor
        Serial.begin(9600);
        gps_ss.begin(9600);
        //initialise the transistor pins
        pinMode(gpsCutoffPin, OUTPUT);
        // Start the gps in an off state
        digitalWrite(gpsCutoffPin, LOW);
      }
  
      void loop() {
          Serial.println("Enter GPS mode");
          GPS_mode();
          Serial.println("Back to the main loop now...");
          Serial.println(stoploop);
          delay(100000);
      }
  
      void GPS_mode(){
        //turn on the gps
        digitalWrite(gpsCutoffPin, HIGH);
        stoploop = 0;
        startTime = millis();
        previous = millis();
        int TimeOut = 60*1000;
        gps_ss.listen();
        Serial.println(stoploop);
          while ((stoploop &lt; 1) &amp;&amp; (previous - startTime &lt; TimeOut))
        {
          while (gps_ss.available() &gt; 0)
          {
            if (gps.encode(gps_ss.read()))
            {
              logInfo();
            }
            yield();
          }
  
        }
        gpsLat = (gps.location.lat(), DEC);
        gpsLon = (gps.location.lng(), DEC);
  
  
        // turn off the gps
        digitalWrite(gpsCutoffPin, LOW);
      }
  
      void logInfo(){
        // Causes us to wait until we have satelite fix
        if(!gps.location.isValid())
        {
          Serial.println("Not a valid location. Waiting for satelite data.");
  
          //return;
        }
      else {
         //url += String(gps.location.lat(), DEC);
  
          //url += String(gps.location.lng(), DEC);
          Serial.println(gps.location.lat(), DEC);
          Serial.println(gps.location.lng(), DEC);
          stoploop = 2;
          //delay(1000);
      }
      previous = millis();
      }
  </code></pre>
  
  <p>I expect to see some gps data printed to the screen, but I actually don't see anything. I know that the gps is working and is receiving data because the blinking LED tell me so.</p>
  
  <p>I have no idea how to fix this. If anyone could help I'd very much appreciate it.</p>
  
  <p>Thanks</p>