GPS not working in total sketch

So I have this sketch that works for a GPS, I get the coordinates perfectly as I wish:

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 7); // (TX, RX)
Adafruit_GPS GPS(&mySerial);

void setup()
{
  Serial.begin(9600);
  delay(5000);
  Serial.println("Adafruit GPS library basic test!");

  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  GPS.sendCommand(PGCMD_ANTENNA);
  delay(1000);
  mySerial.println(PMTK_Q_RELEASE);
}

uint32_t timer = millis();
void loop()
{
  char c = GPS.read();
  if ((c))
  if (GPS.newNMEAreceived()) {
  if (!GPS.parse(GPS.lastNMEA()))
      return;
  }
  
  if (millis() - timer > 2000) {
    timer = millis();

    if (GPS.fix) {
      Serial.print(GPS.latitudeDegrees, 4);
      Serial.print(", ");
      Serial.println(GPS.longitudeDegrees, 4);

    }
  }
}

But as soon as I put it in my total sketch it doesn't work:

#include <Servo.h>
#include <Adafruit_GPS.h>
#include <Wire.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

bool maxheight = false;
bool deploy = false;
float servoAltitude = 0;

Servo servo;

#define BME_SCK 15
#define BME_MISO 14
#define BME_MOSI 16
#define BME_CS 3

#define SEALEVELPRESSURE_HPA (1026.32) // Meet de druk op de grond!

Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);
SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);

void setup() {

  Serial.begin(9600);
  mySerial.begin(9600);
  
// BME680

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
  
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150);
  delay(5000);

// GPS

  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  GPS.sendCommand(PGCMD_ANTENNA);
  delay(1000);
  mySerial.println(PMTK_Q_RELEASE);

// Servo Motor

delay(200);
servo.attach(6);
delay(200);
servo.write(90);
delay(200);
servo.detach();
delay(200);

}

uint32_t timer = millis();

void loop() {

// Parsing

char c = GPS.read();
  if ((c))
  if (GPS.newNMEAreceived()) {
  if (!GPS.parse(GPS.lastNMEA()))
      return;
  }

  if (millis() - timer > 2000) {
    timer = millis();
    
// GPS

    if (GPS.fix) {
      Serial.print("\n");          
      Serial.print("Location:");
      Serial.print(GPS.latitudeDegrees, 4);
      Serial.print(", ");
      Serial.println(GPS.longitudeDegrees, 4);
      mySerial.print("\n");          
      mySerial.print("Location:");
      mySerial.print(GPS.latitudeDegrees, 4);
      mySerial.print(", ");
      mySerial.println(GPS.longitudeDegrees, 4);
    }

// BME680

      Serial.print("\n");
      Serial.print("Temperature = ");
      Serial.print(bme.temperature);
      Serial.println(" *C");
      mySerial.print("\n");
      mySerial.print("Temperature = ");
      mySerial.print(bme.temperature);
      mySerial.println(" *C");
    
      Serial.print("Pressure = ");
      Serial.print(bme.pressure / 100.0);
      Serial.println(" hPa");
      mySerial.print("Pressure = ");
      mySerial.print(bme.pressure / 100.0);
      mySerial.println(" hPa");
      
      Serial.print("Humidity = ");
      Serial.print(bme.humidity);
      Serial.println(" %");
      mySerial.print("Humidity = ");
      mySerial.print(bme.humidity);
      mySerial.println(" %");
      
      Serial.print("Gas = ");
      Serial.print(bme.gas_resistance / 1000.0);
      Serial.println(" KOhms");
      mySerial.print("Gas = ");
      mySerial.print(bme.gas_resistance / 1000.0);
      mySerial.println(" KOhms");
    
      Serial.print("Approx. Altitude = ");
      Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
      Serial.println(" m");
      mySerial.print("Approx. Altitude = ");
      mySerial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
      mySerial.println(" m");
      
      servoAltitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
      
// Servo Motor

if (servoAltitude >= 100) { Serial.println("Above 100 m"); maxheight = true; Serial.println(maxheight);}

  else { delay(1000); }
  
    if (maxheight == true and servoAltitude <= 50)
    {
        Serial.println("Deploying...");
        
        delay(100);
        servo.attach(6);

        delay(100);
        servo.write(0);
        delay(600);
        servo.write(90);
        delay(100);

        servo.detach();
        delay(100);
        
        maxheight = !maxheight;
    }
    else
    { delay(1000); }
   
  }
}

Does anyone know the problem?

Those delay(1000) calls will mean that you drop data from the GPS if they're ever called.

Why are you printing to MySerial? The GPS is using it.

we need to send the information to the groundstation while this gps is in the air

The delays will cause lots of problems with your project, try to use millis instead. There is a lot posted on how to do that. Remember when the processor is delaying it cannot do anything else, not even the next line of code. You need to check your print statements, wildbill found one of them. I am assuming it is wired correctly, if you are in doubt post the schematic.

noodlefish:
we need to send the information to the groundstation while this gps is in the air

That makes no sense at all on the information you have been prepared to reveal.

If you want help, describe the actual project.

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