Display commands randomly crash code

Hi,
For some reason adding certain lines of display code will randomly crash the code. There are no compiling errors, the program takes up only 59% of the program storage and even uploads fine. But then the Arduino doesn’t do anything.

This works:

void loop(){
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("10 seconds until\nlight scanning");
  display.println("Place me on a surface...");
  display.display();
  delay(10000);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Setting light\nthreshold...");
  display.display();
  
  light_Threshold = 0;
  getDistance();
  while (cm > 5){
    getDistance();
    getLight();

    if (light_Value > light_Threshold){        // New brightest spots are set as the threshold
      light_Threshold = light_Value;
    }
    analogWrite(motorR_F_Pin, speed_Value);    // Turn the robot
    analogWrite(motorL_B_Pin, speed_Value);
    delay(200);
    digitalWrite(motorR_F_Pin, LOW);
    digitalWrite(motorL_B_Pin, LOW);
    delay(200);
  }

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Light threshold\nset at: ");
  display.println(light_Threshold);
  display.println("\nTime to go to the\nlight!");
  display.display();
  delay(4000);

  getLight();
  while (light_Value < (light_Threshold-10)){   // Turn the robot to face the brightest light
    digitalWrite(motorR_F_Pin, HIGH);
    digitalWrite(motorL_B_Pin, HIGH);
    delay(200);
    digitalWrite(motorR_F_Pin, LOW);
    digitalWrite(motorL_B_Pin, LOW);
    delay(200);
    getLight();
  }

  int found = 0;        // When this = 1 the robot is in front of the brightest light
  while (found != 1){
    getLight();
    getDistance();

    display.clearDisplay();
    display.setCursor(0, 0);
    display.display();
    
    if (ldrR_Value > ldrL_Value && light_Value <= light_Threshold){   // Light is on the right of robot
      
    }
  }
}

But as soon as I add one more print line, the code crashes and does nothing:

void loop(){
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("10 seconds until\nlight scanning");
  display.println("Place me on a surface...");
  display.display();
  delay(10000);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Setting light\nthreshold...");
  display.display();
  
  light_Threshold = 0;
  getDistance();
  while (cm > 5){
    getDistance();
    getLight();

    if (light_Value > light_Threshold){        // New brightest spots are set as the threshold
      light_Threshold = light_Value;
    }
    analogWrite(motorR_F_Pin, speed_Value);    // Turn the robot
    analogWrite(motorL_B_Pin, speed_Value);
    delay(200);
    digitalWrite(motorR_F_Pin, LOW);
    digitalWrite(motorL_B_Pin, LOW);
    delay(200);
  }

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Light threshold\nset at: ");
  display.println(light_Threshold);
  display.println("\nTime to go to the\nlight!");
  display.display();
  delay(4000);

  getLight();
  while (light_Value < (light_Threshold-10)){   // Turn the robot to face the brightest light
    digitalWrite(motorR_F_Pin, HIGH);
    digitalWrite(motorL_B_Pin, HIGH);
    delay(200);
    digitalWrite(motorR_F_Pin, LOW);
    digitalWrite(motorL_B_Pin, LOW);
    delay(200);
    getLight();
  }

  int found = 0;        // When this = 1 the robot is in front of the brightest light
  while (found != 1){
    getLight();
    getDistance();

    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("Centre light value: ");    // This is the line that breaks it all
    display.display();
    
    if (ldrR_Value > ldrL_Value && light_Value <= light_Threshold){   // Light is on the right of robot
      
    }
  }
}

This isn’t the whole code but I can post the entire code if that’s needed. Can anyone tell me what’s going on?
Thank you for your help!

You don’t say what the SRAM use was. I assume 59% was Flash use.

The display.display(); implies that it is Adafruit_SSD1306 which steals 1024 bytes of SRAM at runtime.

You can reduce the SRAM by putting your anonymous strings into Flash with F()

I’m very new to Arduino so I’m not sure how I can see how much SRAM is being used. Once compiled Arduino IDE tells me that the program takes up 60% of the program storage space (or 19544 bytes), and uses 42% of dynamic memory (or 872 bytes).

This is the compiling message:
Sketch uses 19544 bytes (60%) of program storage space. Maximum is 32256 bytes.
Global variables use 872 bytes (42%) of dynamic memory, leaving 1176 bytes for local variables. Maximum is 2048 bytes.

How can I reduce my SRAM usage? Do you think it’s this that’s giving me my program crashes?
Also I am using Adafruit yes. This is my library list if that gives you any more information:

//Libraries
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>

Sketch uses 19544 bytes (60%) of program storage space. Maximum is 32256 bytes.
Global variables use 872 bytes (42%) of dynamic memory, leaving 1176 bytes for local variables. Maximum is 2048 bytes.

Available SRAM = 1176 bytes.
Yes, you are using Adafruit_SSD1306. So it will steal 1024 bytes at runtime.

1176 - 1024 = 152 bytes left for local variables. This is why you crash.

Change all of your

display.print("message")

and display.println("message") statements that use literal anonymous strings e.g.

  display.println("10 seconds until\nlight scanning");

to

  display.println(F("10 seconds until\nlight scanning"));

You can use ctrl-F from the IDE editor to find and replace the appropriate statements.

David.