I've got a problem with the millis() function

Hey there !

I've got a problem with the millis() function.

My Setup: I've connected the LCD 1602 Module, the Ds3231 RTC Module and the Thermistor for temperature measuring.

In the first line of the LCD I've printed the clock At the second line of the LCD I wanna print the date and the temperature in a 2.5 second delay. If i use the delay() function the clock won't update every second so I used the millis() function. The problem now is, after the first refresh of the function the date is printed after the clock and the temperature is printed at the second line.

Sorry, if I've got something written wrong, I come from Austria

My Code:

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the “Temp” folder and manually remove it. It’s much easier to just view the code in your post.

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

// Bibliothek einbinden
#include <LiquidCrystal.h>
#include <Wire.h>
#include <DS3231.h>

// Initialisiere die Bibliothek mit den Nummern der Schnittstellenpins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//TEMPERATUR
int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, C;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

//UHR

DS3231 clock;
RTCDateTime dt;

//LCD

// Spalten und Zeilen des lcd
int lcdsize[] = {16, 2};



unsigned long previousMillis = 0;

const long interval = 2500;

void setup() {
  lcd.noDisplay();
  lcd.display();
  lcd.clear();
  lcd.begin(16, 2);
  Serial.begin(9600);

  // Initialize DS3231
  clock.begin();

  //Sketch-Kompilierzeit einstellen
  clock.setDateTime(__DATE__, __TIME__);
  // Set from UNIX timestamp
  // clock.setDateTime(1397408400);
  // Manual (YYYY, MM, DD, HH, II, SS
  // clock.setDateTime(2016, 12, 9, 11, 46, 00);
  dt = clock.getDateTime();
  
  // Richtet die Anzahl der Spalten und Zeilen der LCD ein:
  lcd.begin(lcdsize[0], lcdsize[1]);
  
  // Drucket eine Nachricht an das LCD.
  String s = clock.dateFormat("d F Y",   dt);
  lcd.setCursor(2, 1);
  lcd.print(s);



}
void loop() {
  unsigned long currentMillis = millis();
  dt = clock.getDateTime();
  lcd.display();

  String Uhr = clock.dateFormat("H:i:s", dt);
  lcd.setCursor(4, 0);
  lcd.print(Uhr);
  
  String s = clock.dateFormat("d F Y",   dt);
  
  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  T = T - 273.15;
  T = (T * 9.0) / 5.0 + 32.0;

  //Celsius
  C = ((T - 32) / 1.8000);

 if (currentMillis - previousMillis >= interval) {
     previousMillis = currentMillis;

    if (lcd.print(s)) {
      lcd.setCursor(1, 1);
      lcd.print("Temp = ");
      lcd.print(C);
      lcd.print(" C");
      lcd.print("");
    } else {
      lcd.setCursor(0, 1);
      lcd.print("");
      lcd.setCursor(2, 1);
      lcd.print(s);
      lcd.print("");
    }
  }
}
    if (lcd.print(s)) {

Do you know what the print() method returns? Why would you use THAT in an if statement?

stoeger: Sorry, if I've got something written wrong, I come from Austria

no problem. Us non-Austrians can (albeit very rarely) sometimes make mistakes.

this is printing where your cursor last left off, overwriting the last printed line:

if (lcd.print(s)) {

plus (as PaulS scolded you) it makes little sense to do it that way.

try like this

if (currentMillis - previousMillis >= interval) 
{
  previousMillis = currentMillis;
  lcd.setCursor(0, 1);
  // print line 0
  lcd.setCursor(1, 1);
  //print line 1
}