Cannot properly display GPS data on OLED display

I'm working on a simple project to display GPS data (from a cheap GY-NEO6MV2) on a 128x32 OLED display with a UNO R3.
I'm been able to correctly display and constantly update the data I want (speed, altitude, satellites, hdop), but as I was trying to set a default value for when the GPS data is not updated, I've failed miserably.
I don't even know what I don't understand. Here's the setup:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128     // OLED display width, in pixels
#define SCREEN_HEIGHT 32     // OLED display height, in pixels
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

// GPS
static const int RXPin = 3, TXPin = 4;
static const uint32_t GPSBaud = 9600;

int altitude;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  ss.begin(GPSBaud);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;  // Don't proceed, loop forever
  }

  // Clear the buffer and set default color
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.cp437(true);
  display.display();
}

void loop() {
  // put your main code here, to run repeatedly:
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);

  // GPS read and values update
  if (ss.available() > 0) {
    gps.encode(ss.read());

    if (gps.altitude.isUpdated()) {
      altitude = gps.altitude.meters();
      // Altitude update
      display.print(altitude);
    }
  } else {
    display.print("---");
  }
  display.display();
}

I can only display "---" for a brief moment and then the sreen goes black again. So I guess that it can read the serial at first and then

if (ss.available() > 0)

is not verified anymore, but I've tried to print raw data on the serial monitor and I can see it's correctly updated periodically. I've also try to switch to hardware serial but I have the same problem.
It's also very weird that in my first attempt, where I only displayed the altitude without trying to print the "---" when it's not updated, it worked flawlessly:

if (gps.altitude.isUpdated()) {

	altitude = gps.altitude.meters();

	// Altitude update
	display.fillRect(0, 0, 50, 8, SSD1306_BLACK); // To only delete the altitude
	display.setTextSize(1);
	display.setCursor(0, 0);
	display.print(altitude);

	display.display();
} 

Here, when I tried to put the "else" condition, the altitude was correctly replaced by the "---" when the signal was lost, but it would not show back when the signal was re-established (red LED blinking on the GPS board).

So as you can see same code but different problems, I'm very confused and anything will help :)

Could be memory usage issues! We get these frequently on the forum when Uno R3 is used with Adafruit OLED library. Running out of dynamic memory can lead to all kinds of unexpected and strange problems.

What dynamic memory usage statistics do you see when you compile the code? The Adafruit library will take an additional 25% when your code starts running, so if you are already above 55% before that, you are in the danger zone.

At first glance, well, yes. Because that's what your code tells it to do.

Let's strip all the GPS stuff and printing "---" out of the loop and see what's left.

void loop() {
  // put your main code here, to run repeatedly:
  display.clearDisplay();
.
.
.
  display.display();
}

So at a high level, you clear the display, do whatever you do to display something, display it, and then...

...clear the display right away again.

EDIT: which wouldn't matter as much if the OLED was a static display and didn't work in a manner similar to an old CRT, dynamically refreshing the display at a certain rate. You may be seeing a beat frequency effect, where your display updates are happening at just the right rate to beat with the display's update frequency and look like nothing's there.

I can remember playing with this with a Commodore PET; clearing the screen and displaying just the right length of string would produce a scrolling effect, like the string was being printed one character at a time with a delay between them.

More thoughts:

Imagine this:

  1. There are no characters available from the GPS. So you print "---".
  2. Next time through the loop, there is a character available from the GPS, but, you haven't yet got a complete command, let alone something that updates the altitude. So nothing gets printed to the display. So display.display(); prints a blank screen, because you did a 'display.clearDisplay();` at the top.

Most of the time, you're printing a blank screen.

The code for reading the GPS will cause issues.

The first error I see is this bit;

  if (ss.available() > 0) {
    gps.encode(ss.read());

    if (gps.altitude.isUpdated()) {
      altitude = gps.altitude.meters();
      // Altitude update
      display.print(altitude);
    }
  } else {
    display.print("---");
  }
  display.display();

For every character read from the GPS you print stuff out, by the time the display code is finished its likely you can easily miss the next character in the NMEA sentence so there is no valid data. Best to just read the GPS with no print updates until a value is actually updated and then print stuff.

This bit;

  if (gps.altitude.isUpdated()) 

Can also cause problems since for a full update you need to have read the $GPGGA and $GPRMC sentences and altitude is only in one of them, you should do this instead;

  if (gps.altitude.isUpdated() && gps.speed.isUpdated()) 

Tips on reading a GPS reliably here;

wouldn't this be more appropriate

void loop ()
{
    if (ss.available() > 0) {                   // check for gps update
        gps.encode (ss.read());

        if (gps.altitude.isUpdated()) {
            altitude = gps.altitude.meters ();  // get updated gps

            display.clearDisplay ();
            display.setCursor    (0, 0);
            display.print        (altitude);    // update display
            display.display      ();
        }
    }
}