Garbled output on lower line with .96"OLED

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:
OLED_Garbled

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
//

Pasted your formatted code into a code block (the </> button) because I like it.

// 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.606Temperatuur + 0.0124Vochtigheid;

  // MEASURING DISTANCE
  // Average echotime for
  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
//

Thanks you, I guess that counts like a compliment.

It's actually the first sketch I am creating at the moment and slowly adding new things to it.
But as said before, this sketch gives me a bit of garbled output on the lower edge of the OLED screen and I haven't been able to understand why that would be.

First off. Please edit your original post #1. i.e. to put the code in a proper "Code Window".

When you paste directly to the message text the Forum software interprets things like asterisk as an italics sequence or bold text
Hence your code got garbled.

Anyway, I connected a HC-SR04 and a DHT11 to a OLED.
If I have "guessed correctly" about your code pasting errors the program works ok. e.g.

  // Corrected sound speed for temp and humidity
  Soundspeed = 331.3 + 0.606 * Temperatuur + 0.0124 * Vochtigheid; //.kbv

Which is why it is important to learn how to put code into a "Code Window".

David.

+1 for correcting your first post.

It's possible your Arduino is running short of RAM memory. Being a beginner, you forgot to mention which type of Arduino you are using. Many basic Arduino like Uno, Nano 3, Pro Mini, have only 2KB of RAM memory, and the Adafruit library for SSD1306 displays will use half of that at least. Then all the other libraries and your code have only 1KB remaining to use. Corrupt parts of the display are the kind of effect you might see when RAM memory runs out and parts of memory used for the display get accidentally overwritten with other data.

Forgot to mention, I run this sketch on a Arduino Nano and Uno. Both displayed the same problem.

I have solved the problem by doing something simple.
Instead of writing the main screen in the void_setup and only updating the measured values, I now just completely redraw the screen at every loop.

Am not sure if this is the right or best way to handle the OLED, but with the Arduino SSD1306- library, this works ok without any flickering on the screen.

My apologies. I had run (a guess of) your program on a Leo or Mega2560.

Thanks for posting the actual sketch (in a code window in edited #1).

Now that you say you are using a Nano I tried it on a Nano. Sure enough you get the garbled display like your photo in #1.

The build report says:

Sketch uses 18486 bytes (60%) of program storage space. Maximum is 30720 bytes.

Global variables use 702 bytes (34%) of dynamic memory, leaving 1346 bytes for local variables. Maximum is 2048 bytes.

The Adafruit_SSD1306 library takes an extra 1024 bytes of SRAM at runtime. i.e. you have 322 bytes for local variables.
If you put your anonymous strings into Flash memory with F() the build report says:

Sketch uses 18558 bytes (60%) of program storage space. Maximum is 30720 bytes.

Global variables use 588 bytes (28%) of dynamic memory, leaving 1460 bytes for local variables. Maximum is 2048 bytes.

i.e. 436 bytes for local variables.

// 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(F("Please wait..."));
  display.setCursor(10,25);
  display.print(F("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(F("Temp.           C"));
  display.setCursor(5,18);
  display.print(F("Humidity        %"));
  display.setCursor(5,31);
  display.print(F("V-Geluid        m/s"));
  display.setCursor(5,44);
  display.print(F("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(F("Afstand         cm"));
  if (Afstand < Maxafstand) {
    display.setCursor(65,44);
    display.print(Afstand,1);
  } else {
    display.setCursor(65,44);
    display.print(F("000.0"));
  }
  display.display();
  // Wait 1 second
  delay(1000);
}
//
// End of sketch
//

Hi David,

Thanks for testing the sketch with a different approach.
Never realised that even a small & simple sketch could so easily overtax your available memory.
What you changes have accomplished would make it possible for me to put the main static screen build back in the void_setup again.

Still have a lot to learn obviously.
Thank you for all your trouble.

It surprised me. Your anonymous strings are not excessive.
You can normally use Adafruit_SSD1306 quite safely with Nano/Uno. i.e. with normal variables and F()

I assume that DHT.h or NewPing.h must be using significant SRAM.
But I have not looked at the source code.

The using of the display(F(text..)) function seems to have solved my problem with the display,

In the meantime, I have changed the output on the screen, have added both a LED and buzzer to the void loop and buttons to switch those off if wanted, use milis now instead of delay to keep the loop running. Had some issues with the Tone function but found another library that solved that.
Here is what the screen looks now.

And here is the more or less finished code:
All I now need to do is get this on a PCB and print a case for it.

// Project   : 1
// Subject   : Ultrasonic Short Distance Measuring tool with OLED 128x64-display
// Extra info: Sound speed is adjusted for temperature and humidity for more accuracy
// Made by   : Maarten van Maanen - Type-R ICT Service
// Date      : 06-07-2022

// Libraries
#include <Wire.h>                // Library voor OLED
#include <Adafruit_SSD1306.h>    // Library voor SSD1306-OLED
#include <NewTone.h>             // Library voor tone (to avoid conflict with NewPing)

// Defines voor OLED
#define OLED_RESET     -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C      // 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

// 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 Temp/humid sensor
DHT dht(Dhtpin,Dhttype);         // Initialiseer DHT sensor for normal 16mhz Arduino

// Variables for Temp and Humidity
float         Temp       = 0;    // Temperature
float         Humid      = 0;    // Humidity
float         Soundspeed = 0;    // Speed of sound corrected

// Variables for distance
byte          Triggerpin = 9;     // Triggerpin for ultrasonic sensor
byte          Echopin    = 10;    // Echopin for ultrasonic sensor
int           MaxDist    = 400;   // Maximum distance for HC-SR04 sensor
byte          Iteraties  = 6;     // Number of interations for measurement (5)
double        Echoduur   = 0;     // Echotime for distance
float         Dist1      = 0;     // Calculated distance with default sound speed
float         Dist2      = 0;     // Calculated distance with correct sound speed
NewPing sonar(Triggerpin,Echopin,MaxDist);

// Other Variabeles
byte          Start      = 3;     // Countdown to start
unsigned long Timecheck  = 0;     // Variable to check passed time

// Variables for LED light control
byte          LedPin     = 8;     // Pin for LED
byte          LedBtnpin  = 3;     // Pin for LED buttonswitch
byte          LedBtnStat = 0;     // Status LED buttonswitch
byte          LedLight   = 1;     // 1 is LED on, 0 is LED off

// Variables for Buzzer control
byte          BuzPin     = 7;     // Pin for Buzzer
byte          BuzBtnpin  = 4;     // Pin for Buzzer buttonswitch
byte          BuzBtnStat = 0;     // Status Buzzer buttonswitch
byte          Buzzer     = 1;     // 1 is Buzzer on, 0 is Buzzer off

// Basis
void setup() {
  // Initialiseer KY015 Temp.sensor
  dht.begin();

  // Initialiseer OLED
  display.display();
  
  // Initialiseer LED en Buzzer
  pinMode(LedBtnpin, INPUT);
  pinMode(LedPin, OUTPUT);
  pinMode(BuzBtnpin, INPUT);
  pinMode(BuzPin, OUTPUT);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    for(;;); // Don't proceed, loop forever
  }
  
  // Welcome text and countdown 
  display.clearDisplay();
  display.drawRect(0,0, display.width(), display.height(), SSD1306_WHITE);
  display.drawRect(0,0, display.width(), 15, SSD1306_WHITE);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(8,4);
  display.print(F("Type-R ICT Services"));
  display.setCursor(22,21);
  display.print(F("Please wait..."));
  display.setCursor(22,35);
  display.print(F("Starting in:"));
  display.display();
  for(byte i=0;i<Start;i=i+1) {
   display.setCursor(60,49);
   display.print(String(Start-i)+" ");
   display.display();
   delay(1000); 
  }
  // Main text settings
  display.setTextSize(1);      // Text size
  display.setTextColor(WHITE,BLACK);
  
   // Main Display screen
  display.clearDisplay();
  display.drawRect(0,0, display.width(), display.height(), SSD1306_WHITE);
  display.drawRect(0,0, display.width(), 12, SSD1306_WHITE);
  display.setCursor(5,2);
  display.print(F("LED        Sound"));
  display.setCursor(5,14);
  display.print(F("Temp.            C"));
  display.setCursor(5,24);
  display.print(F("Humidity         %"));
  display.setCursor(5,34);
  display.print(F("VSound Cor       m/s"));
  display.setCursor(5,44);
  display.print(F("Dist. Std        cm"));
  display.setCursor(5,54);
  display.print(F("Dist. Cor        cm"));
}
// Main loop
void loop() {
  // Start timer
  unsigned long Time1 = millis();
  byte          Wait  = 30;         // Base delay at end of loop
  int           Blink = 0;          // LED Blink time
  
  // MEASURE TEMP and HUMIDITY
  Temp  = dht.readTemperature();    // Read Temp
  Humid = dht.readHumidity();       // Read Humid 
  
  // Corrected sound speed for temp and humidity
  Soundspeed = 331.3 + 0.606*Temp + 0.0124*Humid;

  // MEASURING DISTANCE
  // Average echotime for <interaties> 
  Echoduur = sonar.ping_median(Iteraties);
  
  // Calculating
  Dist1 = Echoduur * 340 * 0.00005;        // Distance with default sound speed
  Dist2 = Echoduur * Soundspeed * 0.00005; // Distance with corrected sound speed

  // Display status LED
  display.setCursor(28,2);
  if (LedLight == 1) {
    display.print(F("ON "));
  } else {
    display.print(F("OFF"));
  }
  // Display status Buzzer
  display.setCursor(105,2);
  if (Buzzer == 1) {
    display.print(F("ON "));
  } else {
    display.print(F("OFF"));
  }
  
  // Display Temp
  display.setCursor(71,14);
  display.print(Temp,1);
  // Display Humity
  display.setCursor(71,24);
  display.print(Humid,1);
  // Display corrected speed of sound
  display.setCursor(71,34);
  display.print(Soundspeed,1);
  // Display distance
  display.setCursor(71,44);
  display.print(F("     "));
  display.setCursor(71,54);
  display.print(F("     "));  
  if (Dist1 > MaxDist) {
    Blink = 3000;
  } else {
    display.setCursor(71,44);
    display.print(Dist1,1);
    display.setCursor(71,54);
    display.print(Dist2,1);
    Blink = 6*round(Dist1);
  }
  // Update screen
  display.display();

  // Check LED-button status
  LedBtnStat = digitalRead(LedBtnpin);
  if (LedBtnStat == HIGH) {
    if (LedLight == 1) {
      LedLight  = 0;
    } else {
      LedLight  = 1;
    }
  }
  // Check Buzzer-button status
  BuzBtnStat = digitalRead(BuzBtnpin);
  if (BuzBtnStat == HIGH) {
    if (Buzzer == 1) {
      Buzzer = 0;
    } else {
      Buzzer = 1;
    }
  }

  // Check LED-status and change if Bink-time has passed
  if (Time1 - Timecheck >= Blink) {
    // Save last time changed LEDstate
    Timecheck = Time1;
    // Blink the LED for 15 ms if LedLight = 1
    if (LedLight == 1) {
      Wait = Wait - 15;
      digitalWrite(LedPin, HIGH);
      delay(10);
      digitalWrite(LedPin, LOW);
    }
    // Beep the buzzer for 20 ms if Buzzer = 1
    if (Buzzer == 1) {
      Wait = Wait - 10;
      NewTone(BuzPin,2500,10);
    }
  } 
  // Short pauze
  delay(Wait);
}
//
//
// End of sketch
//

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.