TinyGPS++ don't run the sketch until GPS is fixed

Hello everyone, little help is needed here. How do I stop the Serial from printing ***** **** ***** when a GPS signal is unavailable? How do we wait until GPS is locked and then start the loop?

This is the sketch I've been trying to modify according to my needs. Thanks for looking

#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>

static const int RXPin = 0, TXPin = 1;
static const uint32_t GPSBaud = 9600;

// The TinyGPSPlus object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(115200);

  ss.begin(GPSBaud);

  Serial.println(F("Sats HDOP  Latitude   Longitude   Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum"));
  Serial.println(F("           (deg)      (deg)       Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail"));
  Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
}

void loop()
{
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;

  printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printInt(gps.location.age(), gps.location.isValid(), 5);
  printDateTime(gps.date, gps.time);
  printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);

  unsigned long distanceKmToLondon =
    (unsigned long)TinyGPSPlus::distanceBetween(
      gps.location.lat(),
      gps.location.lng(),
      LONDON_LAT, 
      LONDON_LON) / 1000;
  printInt(distanceKmToLondon, gps.location.isValid(), 9);

  double courseToLondon =
    TinyGPSPlus::courseTo(
      gps.location.lat(),
      gps.location.lng(),
      LONDON_LAT, 
      LONDON_LON);

  printFloat(courseToLondon, gps.location.isValid(), 7, 2);

  const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);

  printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);

  printInt(gps.charsProcessed(), true, 6);
  printInt(gps.sentencesWithFix(), true, 10);
  printInt(gps.failedChecksum(), true, 9);
  Serial.println();
  
  smartDelay(1000);

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

// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.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)
      Serial.print(' ');
  }
  smartDelay(0);
}

static 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[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    Serial.print(sz);
  }
  
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  printInt(d.age(), d.isValid(), 5);
  smartDelay(0);
}

static void printStr(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartDelay(0);
}

Use the isValid() function call to determine whether a satellite fix has been acquired, and if not, do other things in the meantime.

Study the GPS++ library docs carefully in order to learn how to best use it.

1 Like

Validity, Update status, and Age

You can examine an object’s value at any time, but unless TinyGPS++ has recently been fed from the GPS, it should not be considered valid and up-to-date. The **isValid()** method will tell you whether the object contains any valid data and is safe to query.

Similarly, **isUpdated()** indicates whether the object’s value has been updated (not necessarily changed) since the last time you queried it.

Lastly, if you want to know how stale an object’s data is, call its **age()** method, which returns the number of milliseconds since its last update. If this returns a value greater than 1500 or so, it may be a sign of a problem like a lost fix.

===========================

This is all the info from the Arduiniana... not much unfortunately

That is all you need.

Avoid using the smartDelay() function in the example you posted. There are much, much better ways of using GPS++. Choose ones that don't block other activities.

1 Like

Change that to:

  
  // Keep waiting until the fix is valid
  while( !gps.location.isValid() )
  {
    smartDelay(1000);

    if (millis() > 5000 && gps.charsProcessed() < 10)
      Serial.println(F("No GPS data received: check wiring"));
  }
}
1 Like

This worked great! Thank You John Wasser !

Hi John, at first I thought it was working but it seems this way is messing up my TIME in the log. Before adding your code my log looks like this

Testing Accel/Gyro... MPU6050 connection successful
Testing Mag...  HMC5883L connection successful
Testing Pressure...  BMP085 connection successful
Initializing SD card...SD Card Initialized.
====// Setup Complete //====
******** : 0.11 / -0.04 / 0.94 , -2.79 / 0.99 / -0.85 , -408 / -118 / -611 , 196.13 , **********  , *********** , *****  , ****** , 22.30 , 993.27	
22:28:28 : 0.11 / -0.03 / 0.94 , -2.83 / 1.27 / -1.05 , -408 / -118 / -611 , 196.13 , 52.354027   , 0.150183    , 1.04   , ****** , 22.30 , 993.19	
22:28:29 : 0.10 / -0.02 / 0.95 , -2.69 / 1.06 / -0.98 , -408 / -118 / -611 , 196.13 , 52.354027   , 0.150180    , 1.04   , 29.20  , 22.30 , 993.26	
22:28:29 : 0.10 / -0.03 / 0.94 , -2.63 / 1.24 / -0.94 , -408 / -118 / -612 , 196.13 , 52.354027   , 0.150180    , 0.94   , 29.20  , 22.30 , 993.26	
22:28:30 : 0.11 / -0.03 / 0.95 , -2.69 / 1.08 / -0.85 , -408 / -118 / -612 , 196.13 , 52.354023   , 0.150182    , 0.94   , 29.20  , 22.30 , 993.29	
22:28:30 : 0.11 / -0.04 / 0.95 , -2.70 / 1.24 / -0.99 , -409 / -118 / -610 , 196.09 , 52.354023   , 0.150182    , 0.24   , 29.20  , 22.30 , 993.20	
22:28:31 : 0.10 / -0.04 / 0.94 , -2.70 / 1.18 / -1.13 , -408 / -117 / -612 , 196.00 , 52.354012   , 0.150185    , 0.24   , 29.20  , 22.30 , 993.23	
22:28:31 : 0.10 / -0.03 / 0.95 , -2.89 / 1.24 / -0.84 , -408 / -118 / -611 , 196.13 , 52.354012   , 0.150185    , 1.24   , 29.20  , 22.30 , 993.25	
22:28:32 : 0.10 / -0.03 / 0.94 , -2.77 / 1.30 / -0.98 , -408 / -118 / -613 , 196.13 , 52.354012   , 0.150187    , 1.24   , 29.20  , 22.40 , 993.26	
22:28:32 : 0.11 / -0.04 / 0.93 , -2.60 / 1.15 / -0.93 , -407 / -119 / -612 , 196.30 , 52.354012   , 0.150187    , 1.26   , 29.20  , 22.40 , 993.25	
22:28:33 : 0.11 / -0.03 / 0.95 , -2.83 / 1.08 / -0.94 , -407 / -117 / -611 , 196.04 , 52.354008   , 0.150188    , 1.26   , 29.20  , 22.40 , 993.24	
22:28:33 : 0.10 / -0.04 / 0.95 , -2.76 / 1.26 / -1.18 , -408 / -117 / -611 , 196.00 , 52.354008   , 0.150188    , 0.70   , 29.20  , 22.40 , 993.25	
22:28:34 : 0.11 / -0.04 / 0.94 , -2.61 / 1.24 / -1.31 , -407 / -118 / -612 , 196.17 , 52.354008   , 0.150190    , 0.65   , 29.20  , 22.40 , 993.23	
22:28:34 : 0.10 / -0.03 / 0.95 , -2.65 / 1.17 / -0.96 , -408 / -118 / -613 , 196.13 , 52.354008   , 0.150190    , 0.65   , 29.20  , 22.40 , 993.15	
22:28:35 : 0.11 / -0.03 / 0.95 , -2.75 / 1.15 / -1.20 , -407 / -118 / -611 , 196.17 , 52.353996   , 0.150198    , 1.22   , 29.10  , 22.40 , 993.23	
22:28:36 : 0.11 / -0.03 / 0.95 , -2.79 / 1.15 / -0.99 , -408 / -118 / -612 , 196.13 , 52.353996   , 0.150202    , 1.22   , 29.10  , 22.40 , 993.26	
22:28:36 : 0.10 / -0.03 / 0.94 , -2.69 / 1.33 / -0.94 , -407 / -117 / -613 , 196.04 , 52.353996   , 0.150202    , 1.07   , 29.10  , 22.40 , 993.30	
22:28:37 : 0.10 / -0.03 / 0.94 , -2.76 / 1.02 / -0.85 , -408 / -118 / -613 , 196.13 , 52.353996   , 0.150205    , 1.07   , 29.10  , 22.40 , 993.22	
22:28:37 : 0.11 / -0.03 / 0.94 , -2.73 / 1.24 / -0.99 , -407 / -118 / -613 , 196.17 , 52.353996   , 0.150205    , 0.94   , 29.10  , 22.40 , 993.23	
22:28:38 : 0.11 / -0.02 / 0.94 , -2.73 / 1.15 / -0.79 , -407 / -118 / -612 , 196.17 , 52.353996   , 0.150207    , 0.94   , 29.10  , 22.40 , 993.27	
22:28:38 : 0.10 / -0.03 / 0.95 , -2.79 / 1.14 / -0.92 , -408 / -118 / -612 , 196.13 , 52.353996   , 0.150207    , 0.94   , 29.10  , 22.40 , 993.24	
22:28:39 : 0.11 / -0.03 / 0.94 , -2.74 / 1.18 / -0.91 , -406 / -118 / -613 , 196.21 , 52.353989   , 0.150210    , 0.94   , 29.10  , 22.40 , 993.27	
22:28:39 : 0.11 / -0.03 / 0.95 , -2.63 / 1.08 / -0.93 , -406 / -118 / -613 , 196.21 , 52.353989   , 0.150210    , 0.44   , 29.10  , 22.40 , 993.26	
22:28:40 : 0.10 / -0.04 / 0.95 , -2.63 / 1.11 / -0.99 , -407 / -117 / -612 , 196.04 , 52.353989   , 0.150213    , 0.44   , 29.10  , 22.40 , 993.25	
22:28:40 : 0.11 / -0.03 / 0.94 , -2.81 / 1.18 / -0.96 , -406 / -119 / -612 , 196.34 , 52.353989   , 0.150213    , 0.28   , 29.10  , 22.40 , 993.24	
22:28:41 : 0.10 / -0.03 / 0.94 , -2.61 / 1.20 / -0.81 , -407 / -118 / -612 , 196.17 , 52.353989   , 0.150213    , 1.06   , 29.10  , 22.40 , 993.21	
22:28:41 : 0.11 / -0.03 / 0.95 , -2.68 / 1.11 / -0.85 , -407 / -118 / -612 , 196.17 , 52.353989   , 0.150213    , 1.06   , 29.10  , 22.40 , 993.27	
22:28:42 : 0.11 / -0.03 / 0.94 , -2.73 / 1.11 / -0.93 , -407 / -118 / -612 , 196.17 , 52.353992   , 0.150215    , 0.87   , 29.10  , 22.40 , 993.26	
22:28:42 : 0.10 / -0.04 / 0.95 , -2.94 / 1.09 / -0.96 , -407 / -118 / -612 , 196.17 , 52.353992   , 0.150215    , 0.87   , 29.10  , 22.40 , 993.22	
22:28:43 : 0.11 / -0.03 / 0.94 , -2.79 / 1.05 / -1.00 , -407 / -118 / -612 , 196.17 , 52.353992   , 0.150218    , 0.50   , 29.10  , 22.40 , 993.22	

after adding your code the log looks like this:

Testing Accel/Gyro... MPU6050 connection successful
Testing Mag...  HMC5883L connection successful
Testing Pressure...  BMP085 connection successful
Initializing SD card...SD Card Initialized.
====// Setup Complete //====
******** : 0.09 / -0.04 / 0.95 , -2.71 / 1.38 / -0.79 , -407 / -117 / -613 , 196.04 , **********  , *********** , *****  , ****** , 22.40 , 993.34	
22:29:45 : 0.09 / -0.03 / 0.94 , -2.78 / 1.24 / -0.94 , -416 / -116 / -625 , 195.58 , 52.353909   , 0.150245    , *****  , 28.60  , 22.40 , 993.28	
22:29:45 : 0.09 / -0.03 / 0.95 , -2.76 / 1.15 / -0.88 , -416 / -116 / -627 , 195.58 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.22	
22:29:45 : 0.09 / -0.03 / 0.95 , -2.63 / 1.09 / -0.87 , -416 / -115 / -628 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.19	
22:29:45 : 0.08 / -0.03 / 0.94 , -2.69 / 1.33 / -0.96 , -416 / -116 / -625 , 195.58 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.28	
22:29:45 : 0.09 / -0.03 / 0.94 , -2.69 / 1.24 / -0.85 , -417 / -115 / -627 , 195.42 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.26	
22:29:45 : 0.08 / -0.03 / 0.94 , -2.77 / 1.39 / -0.89 , -416 / -115 / -625 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.24	
22:29:45 : 0.08 / -0.04 / 0.94 , -2.66 / 1.32 / -0.96 , -416 / -115 / -627 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.22	
22:29:45 : 0.08 / -0.03 / 0.94 , -2.66 / 1.13 / -0.82 , -415 / -116 / -626 , 195.62 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.25	
22:29:45 : 0.08 / -0.03 / 0.95 , -2.70 / 1.20 / -0.92 , -416 / -115 / -628 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.24	
22:29:45 : 0.08 / -0.03 / 0.94 , -2.88 / 1.16 / -0.97 , -417 / -114 / -625 , 195.29 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.26	
22:29:45 : 0.08 / -0.04 / 0.94 , -2.69 / 1.09 / -0.83 , -416 / -116 / -628 , 195.58 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.19	
22:29:45 : 0.08 / -0.03 / 0.94 , -2.54 / 1.22 / -0.94 , -416 / -114 / -625 , 195.33 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.22	
22:29:45 : 0.09 / -0.04 / 0.94 , -2.78 / 1.38 / -0.69 , -416 / -115 / -628 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.28	
22:29:45 : 0.08 / -0.03 / 0.96 , -2.71 / 1.27 / -0.95 , -416 / -114 / -625 , 195.33 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.40 , 993.22	
22:29:47 : 0.08 / -0.04 / 0.95 , -2.73 / 1.08 / -0.85 , -417 / -115 / -628 , 195.42 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.40 , 993.24	
22:29:47 : 0.09 / -0.03 / 0.95 , -2.79 / 1.17 / -0.92 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.40 , 993.28	
22:29:47 : 0.08 / -0.03 / 0.96 , -2.81 / 1.02 / -0.82 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.40 , 993.23	
22:29:47 : 0.09 / -0.03 / 0.94 , -2.73 / 1.18 / -0.92 , -415 / -115 / -626 , 195.49 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.40 , 993.25	
22:29:47 : 0.08 / -0.03 / 0.95 , -2.75 / 1.20 / -0.76 , -416 / -115 / -628 , 195.45 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.40 , 993.25	
22:29:47 : 0.09 / -0.03 / 0.95 , -2.57 / 1.15 / -0.98 , -416 / -115 / -627 , 195.45 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.50 , 993.24	
22:29:47 : 0.08 / -0.04 / 0.95 , -2.73 / 1.18 / -0.74 , -415 / -114 / -628 , 195.36 , 52.353909   , 0.150247    , 0.56   , 28.60  , 22.40 , 993.23	
22:29:47 : 0.08 / -0.04 / 0.95 , -2.74 / 1.17 / -0.81 , -417 / -116 / -626 , 195.55 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.23	
22:29:48 : 0.09 / -0.03 / 0.95 , -2.63 / 1.24 / -0.92 , -415 / -115 / -627 , 195.49 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.23	
22:29:48 : 0.09 / -0.03 / 0.94 , -2.61 / 1.18 / -0.84 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.40 , 993.22	
22:29:48 : 0.08 / -0.03 / 0.93 , -2.77 / 1.21 / -0.79 , -416 / -115 / -628 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.28	
22:29:48 : 0.08 / -0.02 / 0.95 , -2.83 / 1.09 / -0.89 , -416 / -115 / -625 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.27	
22:29:48 : 0.08 / -0.04 / 0.95 , -2.76 / 1.21 / -0.89 , -416 / -115 / -627 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.23	
22:29:48 : 0.08 / -0.03 / 0.95 , -2.85 / 1.40 / -0.91 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.23	
22:29:48 : 0.07 / -0.02 / 0.95 , -2.78 / 1.11 / -0.82 , -415 / -115 / -627 , 195.49 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.25	
22:29:48 : 0.08 / -0.03 / 0.94 , -2.65 / 1.11 / -0.98 , -415 / -115 / -627 , 195.49 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.19	
22:29:49 : 0.09 / -0.04 / 0.94 , -2.76 / 1.25 / -0.83 , -415 / -115 / -626 , 195.49 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.23	
22:29:49 : 0.08 / -0.04 / 0.94 , -2.66 / 1.21 / -0.88 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.21	
22:29:49 : 0.07 / -0.03 / 0.94 , -2.66 / 1.18 / -0.87 , -416 / -115 / -628 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.19	
22:29:49 : 0.08 / -0.04 / 0.95 , -2.76 / 1.20 / -0.90 , -416 / -116 / -625 , 195.58 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.21	
22:29:49 : 0.08 / -0.03 / 0.95 , -2.70 / 1.19 / -0.87 , -415 / -115 / -629 , 195.49 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.26	
22:29:49 : 0.08 / -0.03 / 0.95 , -2.88 / 1.27 / -0.87 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.26	
22:29:49 : 0.08 / -0.04 / 0.95 , -2.79 / 1.21 / -0.90 , -416 / -115 / -627 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.24	
22:29:50 : 0.09 / -0.03 / 0.94 , -2.74 / 1.21 / -0.86 , -416 / -116 / -626 , 195.58 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.25	
22:29:50 : 0.08 / -0.03 / 0.95 , -2.63 / 1.02 / -0.96 , -416 / -115 / -627 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.27	
22:29:50 : 0.08 / -0.04 / 0.95 , -2.73 / 1.49 / -0.99 , -416 / -115 / -626 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.26	
22:29:50 : 0.09 / -0.03 / 0.94 , -2.82 / 1.10 / -1.00 , -415 / -115 / -626 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.27	
22:29:50 : 0.08 / -0.03 / 0.95 , -2.73 / 1.21 / -0.82 , -416 / -115 / -626 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.24	
22:29:50 : 0.08 / -0.04 / 0.95 , -2.76 / 1.11 / -0.82 , -416 / -115 / -627 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.28	
22:29:50 : 0.08 / -0.03 / 0.94 , -2.76 / 1.15 / -0.91 , -415 / -115 / -627 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.27	
22:29:50 : 0.08 / -0.03 / 0.94 , -2.64 / 1.15 / -0.92 , -415 / -114 / -627 , 195.36 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.37	
22:29:51 : 0.07 / -0.03 / 0.95 , -2.80 / 1.24 / -1.00 , -417 / -115 / -625 , 195.42 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.23	
22:29:51 : 0.08 / -0.03 / 0.95 , -2.67 / 1.31 / -0.84 , -415 / -115 / -627 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.20	
22:29:51 : 0.09 / -0.03 / 0.95 , -2.80 / 1.29 / -0.85 , -416 / -115 / -625 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.29	
22:29:51 : 0.08 / -0.03 / 0.95 , -2.69 / 1.25 / -0.82 , -416 / -115 / -627 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.17	
22:29:51 : 0.08 / -0.03 / 0.95 , -2.70 / 1.24 / -1.10 , -416 / -115 / -626 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.28	
22:29:51 : 0.08 / -0.03 / 0.94 , -2.67 / 1.19 / -0.87 , -417 / -114 / -627 , 195.29 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.26	
22:29:51 : 0.09 / -0.03 / 0.95 , -2.79 / 1.20 / -0.97 , -415 / -114 / -626 , 195.36 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.23	
22:29:51 : 0.08 / -0.04 / 0.95 , -2.54 / 1.23 / -0.96 , -416 / -114 / -627 , 195.33 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.26	
22:29:51 : 0.09 / -0.03 / 0.94 , -2.82 / 1.09 / -0.94 , -415 / -115 / -625 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.19	
22:29:51 : 0.08 / -0.03 / 0.94 , -2.55 / 1.08 / -0.97 , -416 / -116 / -627 , 195.58 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.19	
22:29:51 : 0.09 / -0.03 / 0.94 , -2.86 / 1.14 / -0.92 , -415 / -115 / -626 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.22	
22:29:51 : 0.08 / -0.03 / 0.95 , -2.91 / 1.18 / -0.85 , -416 / -115 / -627 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.26	
22:29:51 : 0.08 / -0.03 / 0.95 , -2.66 / 1.15 / -0.79 , -416 / -115 / -626 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.26	
22:29:51 : 0.08 / -0.03 / 0.95 , -2.84 / 1.00 / -0.94 , -416 / -115 / -627 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.25	
22:29:51 : 0.08 / -0.03 / 0.95 , -2.79 / 1.27 / -1.15 , -415 / -115 / -625 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.23	
22:29:51 : 0.08 / -0.03 / 0.94 , -2.72 / 1.14 / -0.97 , -415 / -115 / -627 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.60 , 993.26	
22:29:51 : 0.09 / -0.03 / 0.94 , -2.63 / 1.24 / -0.96 , -416 / -115 / -626 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.22	
22:29:51 : 0.09 / -0.04 / 0.94 , -2.74 / 1.05 / -0.76 , -416 / -116 / -626 , 195.58 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.60 , 993.26	
22:29:51 : 0.08 / -0.03 / 0.94 , -2.66 / 1.12 / -0.79 , -414 / -115 / -626 , 195.52 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.22	
22:29:51 : 0.08 / -0.03 / 0.96 , -2.69 / 1.23 / -1.03 , -415 / -115 / -628 , 195.49 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.60 , 993.25	
22:29:51 : 0.09 / -0.04 / 0.95 , -2.69 / 1.18 / -0.94 , -416 / -115 / -626 , 195.45 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.60 , 993.25	
22:29:51 : 0.08 / -0.04 / 0.95 , -2.60 / 1.21 / -0.98 , -415 / -116 / -626 , 195.62 , 52.353905   , 0.150245    , 0.56   , 28.60  , 22.50 , 993.22	
22:29:51 : 0.08 / -0.04 / 0.95 , -2.89 / 1.27 / -0.82 , -415 / -115 / -625 , 195.49 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.20	
22:29:54 : 0.08 / -0.03 / 0.96 , -2.84 / 1.27 / -0.92 , -416 / -114 / -626 , 195.33 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.23	
22:29:54 : 0.09 / -0.04 / 0.95 , -2.60 / 1.37 / -1.01 , -415 / -115 / -627 , 195.49 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.21	
22:29:54 : 0.09 / -0.04 / 0.94 , -2.76 / 1.08 / -1.02 , -416 / -114 / -627 , 195.33 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.23	
22:29:54 : 0.08 / -0.03 / 0.95 , -2.69 / 1.15 / -0.89 , -416 / -115 / -624 , 195.45 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:54 : 0.09 / -0.03 / 0.95 , -2.82 / 1.04 / -0.76 , -415 / -115 / -626 , 195.49 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.19	
22:29:54 : 0.08 / -0.04 / 0.94 , -2.74 / 1.12 / -0.79 , -415 / -114 / -626 , 195.36 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.29	
22:29:54 : 0.07 / -0.04 / 0.94 , -2.75 / 1.28 / -0.89 , -415 / -114 / -627 , 195.36 , 52.353909   , 0.150243    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:54 : 0.08 / -0.03 / 0.94 , -2.84 / 1.16 / -0.99 , -416 / -114 / -626 , 195.33 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:55 : 0.08 / -0.03 / 0.94 , -2.76 / 1.33 / -0.91 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.31	
22:29:55 : 0.08 / -0.04 / 0.95 , -2.69 / 1.15 / -0.81 , -416 / -115 / -625 , 195.45 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.21	
22:29:55 : 0.08 / -0.04 / 0.95 , -2.64 / 1.11 / -0.89 , -415 / -114 / -626 , 195.36 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.29	
22:29:55 : 0.09 / -0.04 / 0.95 , -2.73 / 1.26 / -0.88 , -416 / -115 / -625 , 195.45 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.27	
22:29:55 : 0.08 / -0.04 / 0.95 , -2.61 / 1.11 / -0.89 , -416 / -116 / -626 , 195.58 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:55 : 0.08 / -0.04 / 0.94 , -2.61 / 1.14 / -0.87 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.25	
22:29:55 : 0.07 / -0.03 / 0.94 , -2.76 / 1.24 / -0.91 , -416 / -115 / -625 , 195.45 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.23	
22:29:55 : 0.08 / -0.03 / 0.95 , -2.77 / 1.29 / -0.92 , -415 / -114 / -627 , 195.36 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.27	
22:29:55 : 0.08 / -0.03 / 0.95 , -2.95 / 1.24 / -0.94 , -416 / -116 / -627 , 195.58 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.24	
22:29:55 : 0.08 / -0.03 / 0.95 , -2.72 / 1.19 / -0.73 , -416 / -115 / -626 , 195.45 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.25	
22:29:55 : 0.08 / -0.04 / 0.95 , -2.72 / 1.11 / -0.94 , -416 / -115 / -627 , 195.45 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.29	
22:29:55 : 0.08 / -0.03 / 0.95 , -2.73 / 1.31 / -0.81 , -415 / -115 / -626 , 195.49 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.25	
22:29:55 : 0.08 / -0.03 / 0.95 , -2.69 / 1.27 / -0.98 , -416 / -114 / -626 , 195.33 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:55 : 0.08 / -0.03 / 0.95 , -2.74 / 1.21 / -0.84 , -415 / -115 / -626 , 195.49 , 52.353909   , 0.150240    , 0.56   , 28.70  , 22.60 , 993.32	
22:29:55 : 0.08 / -0.03 / 0.94 , -2.67 / 1.10 / -0.88 , -415 / -114 / -627 , 195.36 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.26	
22:29:57 : 0.08 / -0.03 / 0.94 , -2.60 / 1.24 / -0.98 , -417 / -115 / -626 , 195.42 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.26	
22:29:57 : 0.08 / -0.04 / 0.94 , -2.77 / 1.13 / -0.85 , -417 / -115 / -626 , 195.42 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.27	
22:29:57 : 0.08 / -0.03 / 0.94 , -2.79 / 1.25 / -0.93 , -416 / -115 / -628 , 195.45 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.28	
22:29:57 : 0.08 / -0.03 / 0.95 , -2.66 / 1.17 / -0.97 , -415 / -115 / -626 , 195.49 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.29	
22:29:57 : 0.09 / -0.03 / 0.94 , -2.88 / 1.27 / -0.99 , -415 / -115 / -625 , 195.49 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.27	
22:29:57 : 0.09 / -0.03 / 0.93 , -2.79 / 1.25 / -1.07 , -416 / -114 / -627 , 195.33 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.27	
22:29:57 : 0.08 / -0.04 / 0.94 , -2.66 / 1.18 / -0.97 , -415 / -115 / -625 , 195.49 , 52.353916   , 0.150238    , 0.56   , 28.60  , 22.60 , 993.26	
22:29:57 : 0.08 / -0.03 / 0.94 , -2.66 / 1.12 / -0.87 , -416 / -115 / -626 , 195.45 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:58 : 0.08 / -0.03 / 0.96 , -2.54 / 1.11 / -0.85 , -416 / -115 / -625 , 195.45 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.25	
22:29:58 : 0.09 / -0.03 / 0.94 , -2.79 / 1.21 / -0.85 , -415 / -116 / -625 , 195.62 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:58 : 0.08 / -0.03 / 0.94 , -2.76 / 1.23 / -0.84 , -416 / -115 / -626 , 195.45 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.33	
22:29:58 : 0.09 / -0.04 / 0.95 , -2.60 / 1.18 / -0.84 , -415 / -115 / -626 , 195.49 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.30	
22:29:58 : 0.08 / -0.03 / 0.95 , -2.66 / 1.10 / -0.89 , -416 / -114 / -626 , 195.33 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.29	
22:29:58 : 0.08 / -0.03 / 0.95 , -2.64 / 1.21 / -0.92 , -415 / -115 / -626 , 195.49 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.19	
22:29:58 : 0.08 / -0.03 / 0.94 , -2.64 / 1.06 / -1.02 , -414 / -114 / -626 , 195.40 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.30	
22:29:58 : 0.08 / -0.03 / 0.94 , -2.71 / 1.12 / -0.79 , -416 / -115 / -625 , 195.45 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.27	
22:29:58 : 0.09 / -0.03 / 0.95 , -2.83 / 1.28 / -1.00 , -416 / -114 / -625 , 195.33 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.26	
22:29:58 : 0.08 / -0.04 / 0.94 , -2.69 / 1.21 / -0.89 , -416 / -114 / -626 , 195.33 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.23	
22:29:58 : 0.08 / -0.03 / 0.94 , -2.82 / 0.98 / -0.93 , -415 / -114 / -626 , 195.36 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.27	
22:29:58 : 0.08 / -0.03 / 0.95 , -2.73 / 1.31 / -0.79 , -415 / -115 / -627 , 195.49 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.21	
22:29:58 : 0.08 / -0.03 / 0.95 , -2.59 / 1.21 / -0.91 , -416 / -115 / -626 , 195.45 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.60 , 993.20	
22:29:58 : 0.08 / -0.03 / 0.94 , -2.64 / 1.37 / -0.93 , -416 / -114 / -626 , 195.33 , 52.353920   , 0.150237    , 0.56   , 28.70  , 22.70 , 993.26	
22:29:58 : 0.08 / -0.03 / 0.94 , -2.82 / 1.25 / -1.06 , -415 / -114 / -626 , 195.36 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.70 , 993.26	
22:30:00 : 0.08 / -0.04 / 0.94 , -2.74 / 1.21 / -0.98 , -414 / -114 / -628 , 195.40 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.60 , 993.22	
22:30:00 : 0.08 / -0.04 / 0.95 , -2.81 / 1.23 / -1.03 , -415 / -115 / -625 , 195.49 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.60 , 993.24	
22:30:00 : 0.09 / -0.03 / 0.95 , -2.61 / 1.27 / -0.95 , -415 / -114 / -626 , 195.36 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.70 , 993.28	
22:30:00 : 0.09 / -0.04 / 0.96 , -2.79 / 1.19 / -1.02 , -416 / -114 / -627 , 195.33 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.70 , 993.31	
22:30:00 : 0.08 / -0.03 / 0.95 , -2.60 / 1.15 / -0.95 , -416 / -115 / -627 , 195.45 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.70 , 993.27	
22:30:00 : 0.08 / -0.04 / 0.95 , -2.76 / 1.22 / -0.79 , -416 / -114 / -626 , 195.33 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.70 , 993.26	
22:30:00 : 0.09 / -0.03 / 0.96 , -2.72 / 1.15 / -0.88 , -415 / -115 / -626 , 195.49 , 52.353920   , 0.150233    , 0.56   , 28.70  , 22.70 , 993.25	
22:30:00 : 0.08 / -0.04 / 0.94 , -2.72 / 1.34 / -0.77 , -415 / -114 / -626 , 195.36 , 52.353924   , 0.150232    , 0.56   , 28.70  , 22.70 , 993.28	
22:30:01 : 0.09 / -0.04 / 0.95 , -2.75 / 1.15 / -1.05 , -416 / -115 / -627 , 195.45 , 52.353924   , 0.150232    , 0.56   , 28.70  , 22.70 , 993.28	
22:30:01 : 0.09 / -0.03 / 0.94 , -2.73 / 1.31 / -0.98 , -414 / -115 / -626 , 195.52 , 52.353924   , 0.150232    , 0.56   , 28.70  , 22.70 , 993.26	
22:30:01 : 0.08 / -0.04 / 0.94 , -2.73 / 1.20 / -0.90 , -415 / -114 / -627 , 195.36 , 52.353924   , 0.150232    , 0.56   , 28.70  , 22.70 , 993.29	
22:30:01 : 0.09 / -0.04 / 0.95 , -2.73 / 1.15 / -0.95 , -414 / -114 / -626 , 195.40 , 52.353924   , 0.150232    , 0.56   , 28.70  , 22.70 , 993.25	

The time is not recorded constantly at all times but jumps from 2 to 3 seconds from time to time.
How Can I solve this?

Here is my full code:

#include <SD.h> // Include SD library 
#include <SPI.h> // Include SPI library
#include <Wire.h> // Include Wire library
#include <Servo.h>
#include <I2Cdev.h>
#include "BMP085.h"
#include "MPU6050.h"
#include "HMC5883L.h"
#include <PinButton.h>

#include <TinyGPSPlus.h>
#include <ServoEasing.hpp>
#include <SoftwareSerial.h>

#define GREEN_LED 12 // GREEN LED
#define RED_LED 26 // RED LED 
#define BLUE_LED 31 // BLUE LED 
#define LEFT_WING_PIN 41 // Left WING
#define RIGHT_WING_PIN 40 // Right WING

const int buzzer = 27; // Buzzer
const int chipSelect = BUILTIN_SDCARD; // Teensy 4.1 BUILDIN_SDCARD
static const int RXPin = 0, TXPin = 1; // GPS PIN connections
static const uint32_t GPSBaud = 9600; // GPS Baud Rate 
static const float ACCEL_SENS = 16384.0; // Accel Sensitivity with default +/- 2g scale
static const float GYRO_SENS = 131.0; // Gyro Sensitivity with default +/- 250 deg/s scale

TinyGPSPlus gps;  // The TinyGPSPlus object
SoftwareSerial ss(RXPin, TXPin); // The serial connection to the GPS device
ServoEasing Left_Wing; // Left Wing Servo Object
ServoEasing Right_Wing; // Right Wing Servo Object
PinButton myButton(10); // Mode Switching Button Object
File myFile; // SD card Object


// Magnetometer class default I2C address is 0x1E
// specific I2C addresses may be passed as a parameter here
// this device only supports one I2C address (0x1E)
HMC5883L mag;
int16_t mx, my, mz;

// Accel/Gyro class default I2C address is 0x68 (can be 0x69 if AD0 is high)
// specific I2C addresses may be passed as a parameter here
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int scale = 16384;

// Barometer class default I2C address is 0x77
// specific I2C addresses may be passed as a parameter here
// (though the BMP085 supports only one address)
BMP085 barometer;

float temperature;
float pressure;
int32_t lastMicros;

void setup() {
  
  bool state = false;
  unsigned int count = 0;

  pinMode(RED_LED, OUTPUT); // Set Red LED pin output
  pinMode(BLUE_LED, OUTPUT); // Set Blue LED pin output
  pinMode(GREEN_LED, OUTPUT); // Set GREEN LED pin output
  pinMode(buzzer, OUTPUT); // Set BUZZER - pin 9 as an output
  
  // Attach servo to pin and set servo to start position.
  Left_Wing.attach(LEFT_WING_PIN, 4);
  Right_Wing.attach(RIGHT_WING_PIN, 3);
  Left_Wing.setSpeed(140); // This speed is taken if no further speed argument is given.
  Right_Wing.setSpeed(140); // This speed is taken if no further speed argument is given.
  delay(500); // Wait for servo to reach start position.
  
  
  Serial.begin(9600);
  while (!Serial && (count < 30)) {
    delay(200); // Wait for serial port to connect with timeout. Needed for native USB
    //Blink GREEN LED
    digitalWrite(GREEN_LED, state);
    state = !state;
    count++;
  }
  digitalWrite(GREEN_LED, HIGH);
 // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();
  
  // ==================== MPU6050 ============================
  accelgyro.initialize();
  Serial.print("Testing Accel/Gyro... ");
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  // Starts up with accel +/- 2 g and gyro +/- 250 deg/s scale
  accelgyro.setI2CBypassEnabled(true); // set bypass mode
  // Now we can talk to the HMC5883l

  // ==================== HMC5883L ============================
  mag.initialize();
  Serial.print("Testing Mag...  ");
  Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");

  // ==================== BMP085 ============================
  barometer.initialize();
  Serial.print("Testing Pressure...  ");
  Serial.println(barometer.testConnection() ? "BMP085 connection successful" : "BMP085 connection failed");
 // ===================== SD CARD =========================
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    Serial.println("Try Again in 5 seconds...");
    digitalWrite(RED_LED, HIGH); // RED LED ON
    digitalWrite(GREEN_LED, LOW); // GREEN LED OFF
    tone(buzzer, 200); // Make Sound 
    delay(750);
    noTone(buzzer); // Stop sound... 
    while (1) {
       // Here put the code were we will try again in 5 seconds
     }
  }
  Serial.println("SD Card Initialized.");
  myFile = SD.open("FlightLog.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (myFile) {
    myFile.println("=== Time ===  |=== Accel === | === Gyro === | ======= Mag ======= |===== GPS DATA ===== |  ==== GPS DATA ==== | === Barometer === |");
    myFile.println("   H:M:s      |  X   Y   Z   |  X   Y   Z   |  X   Y   Z  Heading |    LAT      LNG     |    SPEED    ALT     |  Temp   Pressure  |");
    myFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("Error opening FlighLog.txt");
    digitalWrite(RED_LED, HIGH); // RED LED ON
    digitalWrite(GREEN_LED, LOW); // GREEN LED OFF
    tone(buzzer, 200); // Make Sound
    delay(750);
    noTone(buzzer); // Stop sound... 
  }
   // ==================== GPS initialising ============================
  ss.begin(GPSBaud);

  Serial.println("====// Setup Complete //====");
  
  //CLOSE BOTH WINGS
  Left_Wing.write(95); // Left Wing CLOSED
  Right_Wing.write(0); // Right Wing CLOSED
}

void modeOne() {
  //CLOSE BOTH WINGS
  Left_Wing.write(0); // Left Wing OPEN
  Right_Wing.write(105); // Right Wing OPEN
  
  static unsigned long ms = 0;
  static boolean state = !state;

  // Serial Output Format
  // === Time ===  |=== Accel === | === Gyro === | ======= Mag ======= |===== GPS DATA ===== |  ==== GPS DATA ==== | === Barometer === |
  //    H:M:s      |  X   Y   Z   |  X   Y   Z   |  X   Y   Z  Heading |    LAT      LNG     |    SPEED    ALT     |  Temp   Pressure  |

  if (millis() - ms > 100) {
    printTime(gps.time); // Print GPS TIME
    Serial.print(": ");
    accelgyro.getMotion6( & ax, & ay, & az, & gx, & gy, & gz); // read raw accel/gyro measurements
    // display tab-separated accel/gyro x/y/z values
    Serial.print(ax / ACCEL_SENS);
    Serial.print(" / ");
    Serial.print(ay / ACCEL_SENS);
    Serial.print(" / ");
    Serial.print(az / ACCEL_SENS);
    Serial.print(" , ");
    Serial.print(gx / GYRO_SENS);
    Serial.print(" / ");
    Serial.print(gy / GYRO_SENS);
    Serial.print(" / ");
    Serial.print(gz / GYRO_SENS);
    Serial.print(" , ");
    
    // read raw heading measurements
    mag.getHeading( & mx, & my, & mz);

    // display tab-separated mag x/y/z values
    Serial.print(mx);
    Serial.print(" / ");
    Serial.print(my);
    Serial.print(" / ");
    Serial.print(mz);
    Serial.print(" , ");

    // To calculate heading in degrees. 0 degree indicates North
    float heading = atan2(my, mx);
    if (heading < 0) heading += 2 * M_PI;
    Serial.print(heading * 180 / M_PI);
    Serial.print(" , ");

    //Display LON & LAT
    printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
    Serial.print(" , "); 
    printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
    Serial.print(", ");
    
    //Display SPEED & ALTITUDE
    printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2); //Speed
    Serial.print(" , ");    
    printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2); //Altitude    
    Serial.print(", ");
    // request temperature
    barometer.setControl(BMP085_MODE_TEMPERATURE);

    // wait appropriate time for conversion (4.5ms delay)
    lastMicros = micros();
    while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

    // read calibrated temperature value in degrees Celsius
    temperature = barometer.getTemperatureC();
    
    // request pressure (3x oversampling mode, high detail, 23.5ms delay)
    barometer.setControl(BMP085_MODE_PRESSURE_3);
    while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

    // read calibrated pressure value in Pascals (Pa)
    pressure = barometer.getPressure();

    // display measured values if appropriate
    Serial.print(temperature);
    Serial.print(" , ");   
    Serial.print(pressure / 100);
    Serial.println("\t");
    ms = millis();

   
    // write the GY87 values to SD card
      myFile = SD.open("FlightLog.txt", FILE_WRITE);

      if (myFile) {
      myFile.print((float) gps.time.hour(), 0); myFile.print(":");
      myFile.print((float) gps.time.minute(), 0); myFile.print(":"); 
      myFile.print((float) gps.time.second(), 0); myFile.print(" | ");     
      myFile.print((float) ax / scale);  myFile.print(" "); 
      myFile.print((float) ay / scale);  myFile.print(" "); 
      myFile.print((float) az / scale);  myFile.print(" "); 
      myFile.print(" , ");
      myFile.print((float) gx / scale);  myFile.print(" "); 
      myFile.print((float) gy / scale);  myFile.print(" "); 
      myFile.print((float) gz / scale);  myFile.print(" "); 
      myFile.print(" , ");
      myFile.print((float) mx / scale);  myFile.print(" "); 
      myFile.print((float) my / scale);  myFile.print(" "); 
      myFile.print((float) mz / scale);  myFile.print(" "); 
       myFile.print(" , "); 
      myFile.print((float) heading * 180 / M_PI); myFile.print(" "); 
      myFile.print(" , "); 
      myFile.print((float) gps.location.lat(), 6); myFile.print(" "); 
      myFile.print((float) gps.location.lng(), 6); 
      myFile.print(" , ");
      myFile.print((float) gps.speed.kmph()); 
      myFile.print(" , ");
      myFile.print((float) gps.altitude.meters()); 
      myFile.print(" , ");
      myFile.print((float) temperature);
      myFile.print(" , ");
      myFile.println((float) pressure); 
      
      myFile.close();
    }
    // blink LED to indicate activity
    digitalWrite(BLUE_LED, state);
    state = !state;
    }

  // Keep waiting until the fix is valid
   while(!gps.location.isValid())
   {
    smartDelay(500);

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

// This custom version of delay() ensures that the gps object is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
    gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.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)
      Serial.print(' ');
  }
  smartDelay(0);
}

static void printTime(TinyGPSTime &t)
{
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  smartDelay(0);
}
/*static 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[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}

static void printStr(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartDelay(0);
} */


void modeTwo() {

  // START TESTING MODE:
  Serial.println("--== TEST MODE ==-- ");
  digitalWrite(BLUE_LED, HIGH);
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);

  tone(buzzer, 1000); // 
  delay(200);
  tone(buzzer, 500); // 
  delay(250);
  tone(buzzer, 1000); // 
  delay(10);
  noTone(buzzer); // Stop sound...

  //OPEN BOTH WINGS
  Left_Wing.write(0); // Left Wing OPEN
  Right_Wing.write(105); // Right Wing OPEN

  delay(2000);

  //TEST LEFT WING
  Left_Wing.write(0); // Left Wing OPEN
  delay(500);
  Left_Wing.write(95); // Left Wing CLOSED
  delay(500);
  Left_Wing.write(0); // Left Wing OPEN
  delay(1000);

  //TEST RIGHT WING
  Right_Wing.write(105); // Right Wing OPEN
  delay(500); 
  Right_Wing.write(0); // Right  Wing CLOSED
  delay(500);
  Right_Wing.write(105); // Right Wing OPEN
  delay(500);

  //CLOSE BOTH WINGS
  Left_Wing.write(95); // Left Wing CLOSED
  Right_Wing.write(0); // Right Wing CLOSED
  
  tone(buzzer, 1000); // 
  delay(10);
  tone(buzzer, 500); // 
  delay(250);
  tone(buzzer, 1000); // 
  delay(250);
  noTone(buzzer); // Stop sound...
  Serial.println("--== TEST MODE FINISHED SUCESSFULLY ==--");
  digitalWrite(BLUE_LED, LOW);
 
}
void modeOff() {
  Left_Wing.write(95); // Left Wing CLOSED
  Right_Wing.write(0); // Right Wing CLOSED
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(BLUE_LED, LOW);
  digitalWrite(RED_LED, HIGH);
  tone(buzzer, 200); // 
  delay(750);
  noTone(buzzer); // Stop sound... 
  digitalWrite(RED_LED, LOW); 
}


void loop() {
  
  digitalWrite(GREEN_LED, HIGH);
  // remember the selected mode; 255 indicates no mode is active
  static uint8_t mode = 255;
  
  myButton.update();
  if (myButton.isSingleClick()) {

   mode = 1;

  }

  if (myButton.isDoubleClick()) {
   
    mode = 2;
  
  }

  if (myButton.isLongClick()) {

    mode = 0;

  }
// honour the mode
  switch (mode)
  {
    case 0:
      modeOff();
      mode = 255;  // if you want to prevent modeOff to be repeated, set mode to 255
      break;
    case 1:
      modeOne();
      break;
    case 2:
      modeTwo();
      mode = 255; // Preventing Test Mode from repeating - 255
      break;     
  }

}
// close void loop

Try this version. It waits in setup() until the first valid fix and then records data up to ten times per second.

#include <SD.h> // Include SD library 
#include <SPI.h> // Include SPI library
#include <Wire.h> // Include Wire library
#include <Servo.h>
#include <I2Cdev.h>
#include "BMP085.h"
#include "MPU6050.h"
#include "HMC5883L.h"
#include <PinButton.h>

#include <TinyGPSPlus.h>
#include <ServoEasing.hpp>
#include <SoftwareSerial.h>

#define GREEN_LED 12 // GREEN LED
#define RED_LED 26 // RED LED 
#define BLUE_LED 31 // BLUE LED 
#define LEFT_WING_PIN 41 // Left WING
#define RIGHT_WING_PIN 40 // Right WING

const int buzzer = 27; // Buzzer
const int chipSelect = BUILTIN_SDCARD; // Teensy 4.1 BUILDIN_SDCARD
static const int RXPin = 0, TXPin = 1; // GPS PIN connections
static const uint32_t GPSBaud = 9600; // GPS Baud Rate
static const float ACCEL_SENS = 16384.0; // Accel Sensitivity with default +/- 2g scale
static const float GYRO_SENS = 131.0; // Gyro Sensitivity with default +/- 250 deg/s scale

TinyGPSPlus gps;  // The TinyGPSPlus object
SoftwareSerial ss(RXPin, TXPin); // The serial connection to the GPS device
ServoEasing Left_Wing; // Left Wing Servo Object
ServoEasing Right_Wing; // Right Wing Servo Object
PinButton myButton(10); // Mode Switching Button Object
File myFile; // SD card Object


// Magnetometer class default I2C address is 0x1E
// specific I2C addresses may be passed as a parameter here
// this device only supports one I2C address (0x1E)
HMC5883L mag;
int16_t mx, my, mz;

// Accel/Gyro class default I2C address is 0x68 (can be 0x69 if AD0 is high)
// specific I2C addresses may be passed as a parameter here
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int scale = 16384;

// Barometer class default I2C address is 0x77
// specific I2C addresses may be passed as a parameter here
// (though the BMP085 supports only one address)
BMP085 barometer;

float temperature;
float pressure;
int32_t lastMicros;

void setup()
{

  bool state = false;
  unsigned int count = 0;

  pinMode(RED_LED, OUTPUT); // Set Red LED pin output
  pinMode(BLUE_LED, OUTPUT); // Set Blue LED pin output
  pinMode(GREEN_LED, OUTPUT); // Set GREEN LED pin output
  pinMode(buzzer, OUTPUT); // Set BUZZER - pin 9 as an output

  // Attach servo to pin and set servo to start position.
  Left_Wing.attach(LEFT_WING_PIN, 4);
  Right_Wing.attach(RIGHT_WING_PIN, 3);
  Left_Wing.setSpeed(140); // This speed is taken if no further speed argument is given.
  Right_Wing.setSpeed(140); // This speed is taken if no further speed argument is given.
  delay(500); // Wait for servo to reach start position.


  Serial.begin(9600);
  while (!Serial && (count < 30))
  {
    delay(200); // Wait for serial port to connect with timeout. Needed for native USB
    //Blink GREEN LED
    digitalWrite(GREEN_LED, state);
    state = !state;
    count++;
  }
  digitalWrite(GREEN_LED, HIGH);
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();

  // ==================== MPU6050 ============================
  accelgyro.initialize();
  Serial.print("Testing Accel/Gyro... ");
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  // Starts up with accel +/- 2 g and gyro +/- 250 deg/s scale
  accelgyro.setI2CBypassEnabled(true); // set bypass mode
  // Now we can talk to the HMC5883l

  // ==================== HMC5883L ============================
  mag.initialize();
  Serial.print("Testing Mag...  ");
  Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");

  // ==================== BMP085 ============================
  barometer.initialize();
  Serial.print("Testing Pressure...  ");
  Serial.println(barometer.testConnection() ? "BMP085 connection successful" : "BMP085 connection failed");
  // ===================== SD CARD =========================
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    Serial.println("Try Again in 5 seconds...");
    digitalWrite(RED_LED, HIGH); // RED LED ON
    digitalWrite(GREEN_LED, LOW); // GREEN LED OFF
    tone(buzzer, 200); // Make Sound
    delay(750);
    noTone(buzzer); // Stop sound...
    while (1)
    {
      // Here put the code were we will try again in 5 seconds
    }
  }
  Serial.println("SD Card Initialized.");
  myFile = SD.open("FlightLog.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (myFile)
  {
    myFile.println("=== Time ===  |=== Accel === | === Gyro === | ======= Mag ======= |===== GPS DATA ===== |  ==== GPS DATA ==== | === Barometer === |");
    myFile.println("   H:M:s      |  X   Y   Z   |  X   Y   Z   |  X   Y   Z  Heading |    LAT      LNG     |    SPEED    ALT     |  Temp   Pressure  |");
    myFile.close();
  }
  // if the file isn't open, pop up an error:
  else
  {
    Serial.println("Error opening FlighLog.txt");
    digitalWrite(RED_LED, HIGH); // RED LED ON
    digitalWrite(GREEN_LED, LOW); // GREEN LED OFF
    tone(buzzer, 200); // Make Sound
    delay(750);
    noTone(buzzer); // Stop sound...
  }
  // ==================== GPS initialising ============================
  ss.begin(GPSBaud);

  Serial.println("====// Waiting for valid GPS fix //====");

  // Keep waiting until the GPS fix is valid
  while (!gps.location.isValid())
  {
    smartDelay(500);
    Serial.print('.');

    if (millis() > 5000 && gps.charsProcessed() < 10)
    {
      Serial.println(F("No GPS data received: check wiring"));
      while (1) {}
    }
  }

  Serial.println("\n====// Setup Complete //====");

  //CLOSE BOTH WINGS
  Left_Wing.write(95); // Left Wing CLOSED
  Right_Wing.write(0); // Right Wing CLOSED
}

void modeOne()
{
  // OPEN BOTH WINGS
  Left_Wing.write(0); // Left Wing OPEN
  Right_Wing.write(105); // Right Wing OPEN

  static unsigned long ms = 0;
  static boolean state = !state;

  smartDelay(0);  // Process GPS input

  // Serial Output Format
  // === Time ===  |=== Accel === | === Gyro === | ======= Mag ======= |===== GPS DATA ===== |  ==== GPS DATA ==== | === Barometer === |
  //    H:M:s      |  X   Y   Z   |  X   Y   Z   |  X   Y   Z  Heading |    LAT      LNG     |    SPEED    ALT     |  Temp   Pressure  |

  if (millis() - ms > 100)
  {
    printTime(gps.time); // Print GPS TIME
    Serial.print(": ");
    accelgyro.getMotion6( & ax, & ay, & az, & gx, & gy, & gz); // read raw accel/gyro measurements
    // display tab-separated accel/gyro x/y/z values
    Serial.print(ax / ACCEL_SENS);
    Serial.print(" / ");
    Serial.print(ay / ACCEL_SENS);
    Serial.print(" / ");
    Serial.print(az / ACCEL_SENS);
    Serial.print(" , ");
    Serial.print(gx / GYRO_SENS);
    Serial.print(" / ");
    Serial.print(gy / GYRO_SENS);
    Serial.print(" / ");
    Serial.print(gz / GYRO_SENS);
    Serial.print(" , ");

    // read raw heading measurements
    mag.getHeading( & mx, & my, & mz);

    // display tab-separated mag x/y/z values
    Serial.print(mx);
    Serial.print(" / ");
    Serial.print(my);
    Serial.print(" / ");
    Serial.print(mz);
    Serial.print(" , ");

    // To calculate heading in degrees. 0 degree indicates North
    float heading = atan2(my, mx);
    if (heading < 0) heading += 2 * M_PI;
    Serial.print(heading * 180 / M_PI);
    Serial.print(" , ");

    //Display LON & LAT
    printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
    Serial.print(" , ");
    printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
    Serial.print(", ");

    //Display SPEED & ALTITUDE
    printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2); //Speed
    Serial.print(" , ");
    printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2); //Altitude
    Serial.print(", ");
    // request temperature
    barometer.setControl(BMP085_MODE_TEMPERATURE);

    // wait appropriate time for conversion (4.5ms delay)
    lastMicros = micros();
    while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

    // read calibrated temperature value in degrees Celsius
    temperature = barometer.getTemperatureC();

    // request pressure (3x oversampling mode, high detail, 23.5ms delay)
    barometer.setControl(BMP085_MODE_PRESSURE_3);
    while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds());

    // read calibrated pressure value in Pascals (Pa)
    pressure = barometer.getPressure();

    // display measured values if appropriate
    Serial.print(temperature);
    Serial.print(" , ");
    Serial.print(pressure / 100);
    Serial.println("\t");
    ms = millis();


    // write the GY87 values to SD card
    myFile = SD.open("FlightLog.txt", FILE_WRITE);

    if (myFile)
    {
      myFile.print((float) gps.time.hour(), 0); myFile.print(":");
      myFile.print((float) gps.time.minute(), 0); myFile.print(":");
      myFile.print((float) gps.time.second(), 0); myFile.print(" | ");
      myFile.print((float) ax / scale);  myFile.print(" ");
      myFile.print((float) ay / scale);  myFile.print(" ");
      myFile.print((float) az / scale);  myFile.print(" ");
      myFile.print(" , ");
      myFile.print((float) gx / scale);  myFile.print(" ");
      myFile.print((float) gy / scale);  myFile.print(" ");
      myFile.print((float) gz / scale);  myFile.print(" ");
      myFile.print(" , ");
      myFile.print((float) mx / scale);  myFile.print(" ");
      myFile.print((float) my / scale);  myFile.print(" ");
      myFile.print((float) mz / scale);  myFile.print(" ");
      myFile.print(" , ");
      myFile.print((float) heading * 180 / M_PI); myFile.print(" ");
      myFile.print(" , ");
      myFile.print((float) gps.location.lat(), 6); myFile.print(" ");
      myFile.print((float) gps.location.lng(), 6);
      myFile.print(" , ");
      myFile.print((float) gps.speed.kmph());
      myFile.print(" , ");
      myFile.print((float) gps.altitude.meters());
      myFile.print(" , ");
      myFile.print((float) temperature);
      myFile.print(" , ");
      myFile.println((float) pressure);

      myFile.close();
    }
    // blink LED to indicate activity
    digitalWrite(BLUE_LED, state);
    state = !state;
  }
}

// This custom version of delay() ensures that the gps object is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  }
  while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.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)
      Serial.print(' ');
  }
  smartDelay(0);
}

static void printTime(TinyGPSTime &t)
{
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  smartDelay(0);
}
/*static 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[i] = ' ';
  if (len > 0)
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
  }

  static void printStr(const char *str, int len)
  {
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartDelay(0);
  } */


void modeTwo()
{

  // START TESTING MODE:
  Serial.println("--== TEST MODE ==-- ");
  digitalWrite(BLUE_LED, HIGH);
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);

  tone(buzzer, 1000); //
  delay(200);
  tone(buzzer, 500); //
  delay(250);
  tone(buzzer, 1000); //
  delay(10);
  noTone(buzzer); // Stop sound...

  //OPEN BOTH WINGS
  Left_Wing.write(0); // Left Wing OPEN
  Right_Wing.write(105); // Right Wing OPEN

  delay(2000);

  //TEST LEFT WING
  Left_Wing.write(0); // Left Wing OPEN
  delay(500);
  Left_Wing.write(95); // Left Wing CLOSED
  delay(500);
  Left_Wing.write(0); // Left Wing OPEN
  delay(1000);

  //TEST RIGHT WING
  Right_Wing.write(105); // Right Wing OPEN
  delay(500);
  Right_Wing.write(0); // Right  Wing CLOSED
  delay(500);
  Right_Wing.write(105); // Right Wing OPEN
  delay(500);

  //CLOSE BOTH WINGS
  Left_Wing.write(95); // Left Wing CLOSED
  Right_Wing.write(0); // Right Wing CLOSED

  tone(buzzer, 1000); //
  delay(10);
  tone(buzzer, 500); //
  delay(250);
  tone(buzzer, 1000); //
  delay(250);
  noTone(buzzer); // Stop sound...
  Serial.println("--== TEST MODE FINISHED SUCESSFULLY ==--");
  digitalWrite(BLUE_LED, LOW);

}
void modeOff()
{
  Left_Wing.write(95); // Left Wing CLOSED
  Right_Wing.write(0); // Right Wing CLOSED
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(BLUE_LED, LOW);
  digitalWrite(RED_LED, HIGH);
  tone(buzzer, 200); //
  delay(750);
  noTone(buzzer); // Stop sound...
  digitalWrite(RED_LED, LOW);
}


void loop()
{

  digitalWrite(GREEN_LED, HIGH);

  // remember the selected mode; 255 indicates no mode is active
  static uint8_t mode = 255;

  myButton.update();
  if (myButton.isSingleClick())
  {
    mode = 1;
  }

  if (myButton.isDoubleClick())
  {
    mode = 2;
  }

  if (myButton.isLongClick())
  {
    mode = 0;
  }
  // honour the mode
  switch (mode)
  {
    case 0:
      modeOff();
      mode = 255;  // if you want to prevent modeOff to be repeated, set mode to 255
      break;
    case 1:
      modeOne();
      break;
    case 2:
      modeTwo();
      mode = 255; // Preventing Test Mode from repeating - 255
      break;
  }

}
// close void loop
1 Like

Works like a charm :slight_smile:
Another opinion would be very welcome :slight_smile:

As We already mentioned that I have SD card to save the data, when the computer boots up it checks the SD and if not presented I'm stuck

// ===================== SD CARD =========================
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    Serial.println("Try Again in 5 seconds...");
    digitalWrite(RED_LED, HIGH); // RED LED ON
    digitalWrite(GREEN_LED, LOW); // GREEN LED OFF
    tone(buzzer, 200); // Make Sound
    delay(750);
    noTone(buzzer); // Stop sound...
    **while (1)**
**    {**
**      // STUCK HERE IF NO SD IS PRESENTED**
**    }**
  }

After little research I reach the conclusion that I can use WatchDog and if SD card is NOT presented we can reboot in 5 seconds and hopefully there will be one :slight_smile:

Like this:

#include <Watchdog.h> // Include library for watchdog

Watchdog watchdog; // Watchdog object
unsigned long enabled_time;

// ===================== SD CARD =========================
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    Serial.println("Try Again in 5 seconds...");
    digitalWrite(RED_LED, HIGH); // RED LED ON
    digitalWrite(GREEN_LED, LOW); // GREEN LED OFF
    tone(buzzer, 200); // Make Sound 
    delay(750);
    noTone(buzzer); // Stop sound... 
    while (1) {
        smartDelay(5000);
        // Watchdog reset
        watchdog.enable(Watchdog::TIMEOUT_1S);
        enabled_time = millis();
     }
  }

It works as desired, but is this redundant? Thank You :slight_smile:

  while (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    digitalWrite(RED_LED, HIGH); // RED LED ON
    digitalWrite(GREEN_LED, LOW); // GREEN LED OFF
    tone(buzzer, 200); // Make Sound
    delay(750);
    noTone(buzzer); // Stop sound...

    Serial.println("Try Again in 5 seconds...");
    delay(5000);
  }
1 Like

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