Too many libraries

TheMemberFormerlyKnownAsAWOL:
BTW who is going to be reading all the Serial prints during deployment?

Think about this one

Dump the GPS library - just hose NMEA sentences into SD, and decode later.
Dump the 680 library - hose readings straight into SD.

I'm sure we've already been through this.

The quote is very true, I can remove all serial prints. Not sure if that'll be enough scrapping but i'll try!

Edit: Turns out it's not enough

I can't hose readings to SD, I am required to do it BOTH ways

What do you mean by "BOTH"?

Have you noticed that a lot of your code looks very similar, the only difference being the destination of the serial stream?

Maybe there are gains to be made by factoring your code.

// BME680 SD Card

  if (myFile) {

    mySerial.print("Temperature = ");
    mySerial.print(bme.temperature);
    mySerial.println(" *C");

Something wrong there, I think.

Yeah sorry, as I said a lack of testing leads to these dumb mistakes. Also "Both" means through the apc220 AND on the SD Card. I need the SD Card to function as an airplane's black box AND I need the information on the ground during the flight.

Drop the SD library. Write individual data records as whole, consecutive sectors.

I'll leave you to your dumb mistakes now, I'm out.

How do I do that, write individual data records a whole, consecutive sectors?

I merged all of the myFile{ } code together. I no longer have 3 opening and closing brackets but made it one whole, is this what you meant?

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

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

Servo servo;
File myFile;

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

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

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

void setup() {

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

// SD Card

  if (!SD.begin(4)) {
    mySerial.println("initialization failed!");
    while (1);
  }
  
  myFile = SD.open("CanSat_Data.txt", FILE_WRITE);

// BME680

  if (!bme.begin()) {
    mySerial.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); // 320*C for 150 ms
  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(200);
  
//  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) {
      mySerial.print("\n");
      mySerial.print("Location: ");
      mySerial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      mySerial.print(", ");
      mySerial.println(GPS.longitude, 4); Serial.println(GPS.lon);
    }
  
// BME680

      mySerial.print("Temperature = ");
      mySerial.print(bme.temperature);
      mySerial.println(" *C");
    
      mySerial.print("Pressure = ");
      mySerial.print(bme.pressure / 100.0);
      mySerial.println(" hPa");
      
      mySerial.print("Humidity = ");
      mySerial.print(bme.humidity);
      mySerial.println(" %");
      
      mySerial.print("Gas = ");
      mySerial.print(bme.gas_resistance / 1000.0);
      mySerial.println(" KOhms");

      mySerial.print("Approx. Altitude = ");
      mySerial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
      mySerial.println(" m");

// BME680 SD Card

  if (myFile) {

    myFile.print("Temperature = ");
    myFile.print(bme.temperature);
    myFile.println(" *C");
    
    myFile.print("Pressure = ");
    myFile.print(bme.pressure / 100.0);
    myFile.println(" hPa");
    
    myFile.print("Humidity = ");
    myFile.print(bme.humidity);
    myFile.println(" %");

    myFile.print("Gas = ");
    myFile.print(bme.gas_resistance / 1000.0);
    myFile.println(" KOhms");

    myFile.print("Approx. Altitude = ");
    myFile.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    myFile.println(" m");

// GPS SD Card

    myFile.print("\n");
    myFile.print("Location: ");
    myFile.print(GPS.latitude, 4); Serial.print(GPS.lat);
    myFile.print(", ");
    myFile.println(GPS.longitude, 4); Serial.println(GPS.lon);

    myFile.close();
    }
      
// Servo Motor

servoAltitude = servoAltitude - 20; //bme.readAltitude(SEALEVELPRESSURE_HPA);
mySerial.println(servoAltitude);

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

  else { delay(1000); }
  
    if (maxheight == true and servoAltitude <= 50)
    {
        mySerial.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); }
   
  }
}

If all you need to do is 'Log' data the peplace the SD card breakout with an Openlog, it will 'Log' everything that goes ou the serial port. Proabaly smaller than the average SD breakout board too.

I see the project is a CANSAT, if there is a competion involved, do we get a prize too ?

Hmmm I'm not sure if I understand, but if I have "Openlog" in my program it will automatically log everything I send to my serial monitor on my SD Card ??

Also, yes, the prize is a cup so you will win a picture of the cup if we win :slight_smile:

This

    mySerial.print("Temperature = ");
      mySerial.print(bme.temperature);
      mySerial.println(" *C");
    
      mySerial.print("Pressure = ");
      mySerial.print(bme.pressure / 100.0);
      mySerial.println(" hPa");
      
      mySerial.print("Humidity = ");
      mySerial.print(bme.humidity);
      mySerial.println(" %");
      
      mySerial.print("Gas = ");
      mySerial.print(bme.gas_resistance / 1000.0);
      mySerial.println(" KOhms");

      mySerial.print("Approx. Altitude = ");
      mySerial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
      mySerial.println(" m");

Is effectively identical to this

    myFile.print("Temperature = ");
    myFile.print(bme.temperature);
    myFile.println(" *C");
    
    myFile.print("Pressure = ");
    myFile.print(bme.pressure / 100.0);
    myFile.println(" hPa");
    
    myFile.print("Humidity = ");
    myFile.print(bme.humidity);
    myFile.println(" %");

    myFile.print("Gas = ");
    myFile.print(bme.gas_resistance / 1000.0);
    myFile.println(" KOhms");

    myFile.print("Approx. Altitude = ");
    myFile.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    myFile.println(" m");

That's what I meant by factoring.

You need to slow down, and think - "What can I get rid of, and still fulfill the spec?" and if that proves fruitless, "Can I use a better-suited, better specced, processor?"

char c = GPS.read();
  if ((c))

-1 will evaluate as true.
Needs more attention to detail

noodlefish:
Hmmm I'm not sure if I understand, but if I have "Openlog" in my program it will automatically log everything I send to my serial monitor on my SD Card ??

If you were logging data so you are seeing it in the Serial Monitor, when you have your flight you just plug in the Openlog and it records everything that would otherwise have been seen in the Serial Monitor.

Hmm the Openlog is still a bit weird to me, i'll look into in more next week.

AWOL, as I said, I need to both have the information on mySerial and myFile so I thought this was the easyest way to do so.

Maybe if I use srnet's method I can solve both at once.

noodlefish:
AWOL, as I said, I need to both have the information on mySerial and myFile so I thought this was the easyest way to do so.

But you're not writing the same information, which I would have thought to be important, and anyway, you're not looking for the easiest way, you're looking for the way that suits your resources.

Just out of interest, what was the memory usage before you added the SD library, and what is it now?

uhh I gotta be honest, I have no idea where to look for that
and what do you mean it's not the same information?

noodlefish:
uhh I gotta be honest, I have no idea where to look for that

In the lower pane of the IDE, every time you compile.

and what do you mean it's not the same information?

The samples are taken at different times.

No big deal when it's sitting on a bench, but when it's plummeting through the atmosphere...

Ah, I think I have just worked it out, @noodlefish is participating in the UK 2021 CANSAT competition, hence the use of the APC220 and SD card for logging.

text section exceeds available space in board
Sketch uses 36440 bytes (127%) of program storage space. Maximum is 28672 bytes.
Global variables use 2274 bytes (88%) of dynamic memory, leaving 286 bytes for local variables. Maximum is 2560 bytes.

That's after removing SoftwareSerial and adding the F() macro.

Commenting out SPI.h and Adafruit_Sensor.h didn't help since they are apparently not used.

      mySerial.print("Temperature = ");
      mySerial.print(bme.temperature);
      mySerial.println(" *C");
    
      mySerial.print("Pressure = ");
      mySerial.print(bme.pressure / 100.0);
      mySerial.println(" hPa");

WHY are you logging data to the GPS? Did you mean 'myFile' and not 'mySerial'?

I don't think the GPS is supposed to be logging, the outbound data is going to the APC220 (guessing here), but that kinda screws with the config messages to the GPS.

I don't think the OP has thought this one through.

I'd just log raw sensor data, and leave the smarts to the ground / post mortem stations, if all that's available is a Pro Micro, 'cos making a dent in that 27% is going to be tough without handcrafted custom code.

The OP said that the SD library tipped the balance, but I'd bet it was pretty close to 100% before that. Time to break out the laptop.

I bet a Teensy 4 would work just great though.

@Johnwasser - did you refactor the Stream stuff in your calculation?

TheMemberFormerlyKnownAsAWOL:
@Johnwasser - did you refactor the Stream stuff in your calculation?

I'm not sure I understand the question. Are you suggesting that calling .write() instead of .print() and .println() could avoid loading the Stream functions?

No, sorry I was suggesting putting the debug / serial log (radio) / SD file prints into a single function, and passing a reference to the appropriate Stream, to reduce duplication of code.

I'll give it a shot this afternoon - it's still not going to make much of a dent on the 27% though.