OLED not updating when printing variables

I’m trying to connect a GPS unit (GY-GPS6MV2) and an OLED screen using the Adafruit_SH1106 library. Here’s the code I’ve come up with:

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

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
static const int RXPin = 5, TXPin = 3;
static const uint32_t GPSBaud = 9600;
float vkph;
float vmps;

TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);

void setup(){
  Serial.begin(9600);
  ss.begin(GPSBaud);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop(){
  Serial.println("Trying to read...");
  while (ss.available() > 0){
    gps.encode(ss.read());
    vkph = gps.speed.kmph();
    vmps = gps.speed.mps();
    if (gps.location.isUpdated()){
      Serial.print("Position: "); 
      Serial.print(gps.location.lat(), 6);
      Serial.print(" : "); 
      Serial.println(gps.location.lng(), 6);
      Serial.print("Speed: ");
      Serial.print(vmps); 
      Serial.print(" m/s");
      Serial.print(", ");
      Serial.print(vkph);
      Serial.println(" km/hr"); 
      
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      display.println("Speed:");
//      display.print(vmps, 1);
      display.println("m/s");
      display.setTextSize(2);
      display.setTextColor(WHITE);
//      display.print(vkph, 1);
      display.println("km/hr");
      display.display();
    }
  }
}

The GPS outputs to the serial monitor fine, and the screen displays “Hello \n World” as expected. But when I uncomment the lines to show the values of the speed variables, the display just shows its splash screen, flashing when the GPS updates.

How can I get the display to show the speeds in m/s and km/hr?

Try updating the screen less often :slight_smile: