Why void loop is skipping lines of code when using oled display

my code is using keypad library and oled display. Without the oled code added everything works just fine. as soon as I add code for that, void loop stops working halfway and jump out and call another function at the end. I added several print statements to check this.

#include <Wire.h>                                                              
#include <Adafruit_SSD1306.h>                                                   
#include <Adafruit_GFX.h>                                                       
#define OLED_WIDTH 128#define OLED_HEIGHT 64                                    
#define OLED_ADDR   0x3C
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);

in setup- works fine until here

display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  displayOled(); //welcome screen

in loop- works until serial.println(guess) then skips check yourself function , print(result) and play sound to setup newgame. Why not doing while loop? (this part works perfectly fine when oled display was not added)

void loop() { 
  char guess = 'n';
  String result = "miss";
  boolean isAlive = true;
  while (isAlive){
    guess = getGuess();
    if (guess != 'n') {

      Serial.print("You guessed:");
      result = checkYourself(guess);
      if (result.compareTo("kill") == 0) isAlive = false;
      Serial.println("Enter a new guess (1-8):");


Is the library causing this? please help thanks

what's the code for the checkYourself() function?
what's the delay(500); used for ?

Don't post snippets (Snippets R Us!)

Is this a typo/transcription error:

#define OLED_WIDTH 128#define OLED_HEIGHT 64

What Arduino are you using? Do you get any memory-low warnings when compiling?

This is my full code where I am using 8 leds connected with a shift register , oled display , keypad input and sound. Thisis a battleship game. Whenever I input a guess, it checks it (check yourself()) then returns result. if result == miss /hit /kill it will play a sound and light up respective led.
if I comment out display.begin for oled screen it works perfectly fine. When I uncomment it does the skipping part.

sketch used only 56% of memory

Using Arduino uno. sketch uses 56% memory only.
just added delay to see if that would help. but apparently not.

I don’t have an Uno but compiled and ran it on one of my 2560s. When I do a free RAM check at the end of setup() I’m shown 6276 bytes. With 8192 bytes of RAM this means a total use of 1916 bytes.

I get messages in the serial monitor so it seems like the program is running on the Mega. Maybe you’re just running out of RAM on the Uno.

does both the sound function and oleddsiplax function work on yours?

does both the sound function and oleddsiplax function work on yours?

Didn't try either; don't have your setup. Just loaded the code into a 2560 and saw serial output that suggested it was otherwise running.

My main interest was actually seeing the full memory usage (not just the static stuff called out by the IDE at compile time...)