Beginner tips for managing SRAM usage?

Hey there. I’m a raw beginner at programming in general, and I’ve been having some trouble with my first project.

As my sketch has grown, I started getting this error:

Sketch uses 19,252 bytes (59%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,698 bytes (82%) of dynamic memory, leaving 350 bytes for local variables. Maximum is 2,048 bytes.
Low memory available, stability problems may occur.

And now my sketch malfunctions in weird ways. It might be worth noting that I’m using a knock-off Sunfounder Arduino UNO. I’ve found that when I remove one of the libraries from the sketch (e.g. all of the servo stuff), the malfunctions with the rest of the sketch go away.

I assume that there are probably some glaring inefficiencies in my code, and I’d really appreciate any help in identifying and fixing them!

/* SD SD SD SD SD */
#include <SPI.h>
#include <SD.h>
File myFile;
/* END SD END SD END SD END SD */



/* SERVO SERVO SERVO SERVO */
#include <Servo.h>  // servo library
Servo servo1;  // servo control object
/* END SERVO SERVO SERVO SERVO */

/*ALTIMETER */
#include <Wire.h> 
#include "IntersemaBaro.h"
Intersema::BaroPressure_MS5607B baro(true);
/* END ALTIMETER */

void setup() { 
  
  
  /*SD SD SD SD SD SD SD SD SD SD SD SD SD */

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  /* END SD END SD END SD END SD END SD */
  
  
   /* SERVO SERVO SERVO */
 
   // initialize digital pin 13 as an output.
   pinMode(3, OUTPUT);
   servo1.attach(9);
   /* END SERVO SERVO SERVO */

    Serial.begin(9600);
    baro.init();
 
    int alt1 = baro.getHeightCentiMeters();
    delay(1000);
    int alt2 = baro.getHeightCentiMeters();
    delay(1000);
    int alt = (alt2)-(alt1);
  
    while (alt < 150) {
       int alt3 = baro.getHeightCentiMeters();
       delay(1000);
       alt = alt3-alt1;
  
       Serial.print("Centimeters: ");
       Serial.print((float)(alt));
       Serial.print(", Feet: ");
       Serial.println((float)(alt) / 30.48);
 
 /* SD SD SD SD SD */
 
       myFile = SD.open("test.txt", FILE_WRITE);
       myFile.print("Centimeters: ");
       myFile.print((float)(alt));
       myFile.print(", Feet: ");
       myFile.println((float)(alt) / 30.48);
       myFile.close();
       delay(1000);
 
/* END SD SD SD SD SD */

   }
  
  /* Open Vent */
  
    Serial.println("OPEN VENT OPEN VENT OPEN VENT OPEN VENT OPEN VENT ");
    myFile = SD.open("test.txt", FILE_WRITE); 
    myFile.println("OPEN VENT OPEN VENT OPEN VENT OPEN VENT OPEN VENT");
    myFile.close();
    servo1.write(150); 
  
  /* Wait for ascent rate to become less than 100 cm/s */

  int ascent = 0;
  
  while (ascent < 50) {
    int asc1 = baro.getHeightCentiMeters();
    delay(500);
    int asc2 = baro.getHeightCentiMeters();
    delay(500);
    
    Serial.print("Ascent rate (cm/s)");
    Serial.println((float)(asc2)-(asc1));
    
    myFile = SD.open("test.txt", FILE_WRITE); 
    myFile.print("Ascent rate:");
    myFile.println((float)(asc2)-(asc1));
    myFile.print("Altitude (cm):");
    myFile.println((float)(alt2));
    myFile.close();
    
  }
  

  
  /* CLOSE VENT */ 
  
    Serial.println("CLOSE VENT CLOSE VENT CLOSE VENT CLOSE VENT ");
    myFile = SD.open("test.txt", FILE_WRITE); 
    myFile.println("CLOSE VENT CLOSE VENT CLOSE VENT CLOSE VENT");
    myFile.close();
    servo1.write(35); 
  
  /* Wait 30 minutes and make sure vent doesnt ascend above 70,000 ft */
   
 
}

void loop() {}
Serial.println("initialization failed!");

Waste of RAM. Keep them in flash

Serial.println(F("initialization failed!"));

Serial.println("OPEN VENT OPEN VENT OPEN VENT OPEN VENT OPEN VENT ");

Ditto.

Thanks! So I should do that for all serial prints?

Every one that has a constant string

AWOL: Every one that has a constant string

Thanks a lot! That appears to have fixed almost everything!

One remaining weird problem though.. I've found that whenever I start the sketch, the first few line sin the serial monitor return weird garbled text.. often with special characters. Here's the latest example:

Initting to test.txt... done. Centimeters: 18.00, Feet: 0.59 Initializing SD card... initialization done. Writing to test.txt... done. Centimeters: -18.00, Feet: -0.59

Could this problem also be related to memory usage?

More likely to be the serial port synchronizing the baud rate. A small delay after opening serial (Serial.begin) might fix it.