I am a recent user of Arduino and am playing around a lot with sketches.
I have made a sketch based on the many examples which can be found here and elsewhere on the internet.
The sketch is a simple one. I measures temp and humidity from a DHT11 sensor to correct the default speed of sound to a more accurate one.
Then, using an ultrasonic HC-SR04 I measure distance to an object and correct the distanced based on the calculated speed of sound.
This info is then displayed on a 0.96" OLED Display with temp, humidity, SOS and distance.
The strange thing is however is that although the initial screen on the OLED is perfect, as soon as the sketch starts displaying the actual measurements, it garbles the lower line on the OLED.
The sketch itself works fine, the output is correct but the garbled line remains.
I tried all sort of stuff but the problem remains.
I even added a kind of count-down at startup but that part functions fine.
Here is a picture of the OLED:

And here is the sketch itself.
// Ultrasonic distance with corrected speed of sound, displayed on OLED
// 29-06-2022
//
// Libraries
#include <SPI.h> // Library voor SPI
#include <Wire.h> // Library voor OLED
#include <Adafruit_SSD1306.h> // Library voor SSD1306-OLED
//
// Defines voor OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // 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);
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// Ard.UNO: A4(SDA),A5(SCL) - Ard.MEGA 2560: 20(SDA),21(SCL) - Ard.LEONARDO: 2(SDA),3(SCL)
//
// Defines for Ultrasonic
#include "NewPing.h" // Library for Ultrasonic sensor
#include <DHT.h> // Basislibrary voor Ultrasonic sensor
#define Dhttype DHT11 // Selecteren type sensor
#define Dhtpin 2 // Input/output pin for KY015 Temperatuur/humid sensor
DHT dht(Dhtpin,Dhttype); // Initialiseer DHT sensor for normal 16mhz Arduino
// Variabelen voor Temperatuur en luchtvochtigheid
float Temperatuur = 0; // Temperature
float Vochtigheid = 0; // Humidity
float Soundspeed = 0; // Speed of sound corrected
// Variabelen voor afstandsmeting
int Triggerpin = 9; // Triggerpin for ultrasonic sensor
int Echopin = 10; // Echopin for ultrasonic sensor
int Maxafstand = 400; // Maximum distance for HC-SR04 sensor
int Iteraties = 5; // Number of interations for measurement (5)
double Echoduur = 0; // Echotime for distance
double Afstand = 0; // Calculated distance in cm
NewPing sonar(Triggerpin,Echopin,Maxafstand);
// Basis
void setup() {
// Initialiseer KY015 Temperatuur.sensor
dht.begin();
// Initialiseer OLED
display.display();
delay(2000); // Pause for 2 seconds
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
for(;;); // Don't proceed, loop forever
}
// OLED-Welcome tekst
display.clearDisplay();
delay(2000);
display.drawRect(0,0, display.width(), display.height(), SSD1306_WHITE);
display.setTextColor(WHITE, BLACK);
display.setCursor(20,9);
display.print("Please wait...");
display.setCursor(10,25);
display.print("Starting App in:");
display.display();
int i;
for(i=1;i<10;i=i+1) {
display.setCursor(60,44);
display.print(10-i);
display.display();
delay(1000);
}
// Clear the buffer
display.clearDisplay();
display.drawRect(0,0, display.width(), display.height(), SSD1306_WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE,BLACK);
display.setCursor(5,5);
display.print("Temp. C");
display.setCursor(5,18);
display.print("Humidity %");
display.setCursor(5,31);
display.print("V-Geluid m/s");
display.setCursor(5,44);
display.print("Afstand cm");
display.display();
delay(3000);
}
//
// Main loop
void loop() {
// MEASURE TEMP and HUMIDITY
Temperatuur = dht.readTemperature(); // Lees Temperatuur
Vochtigheid = dht.readHumidity(); // Lees vochtigheid
// Corrected sound speed for temp and humidity
Soundspeed = 331.3 + 0.606*Temperatuur + 0.0124*Vochtigheid;
// MEASURING DISTANCE
// Average echotime for <interaties>
Echoduur = sonar.ping_median(Iteraties);
// Calculating
Afstand = Echoduur * Soundspeed * 0.00005;
// Display Temp
display.setCursor(65,5);
display.print(Temperatuur,1);
// Display Humity
display.setCursor(65,18);
display.print(Vochtigheid,1);
// Display corrected speed of sound
display.setCursor(65,31);
display.print(Soundspeed,1);
// Display distance
display.setCursor(5,44);
display.print("Afstand cm");
if (Afstand < Maxafstand) {
display.setCursor(65,44);
display.print(Afstand,1);
} else {
display.setCursor(65,44);
display.print("000.0");
}
display.display();
// Wait 1 second
delay(1000);
}
//
// End of sketch
//
