Problem eine Lebensuhr zu programieren

Hallo
ich bin neu hier und habe da ein Projekt zu dem ich irgendwie keine Lösung finde.
Ich habe ein Arduino Nano
Ein RTC Modul und ein Oleddisplay.
Das ziel ist das auf dem Display das aktuelle Datum sowie die Zeit angezeigt wird (Das habe ich schon hinbekommen).
Jetzt aber das Problem das ich habe:

Man soll selber ein Datum eingeben können und der Arduino soll die verbleibenden Tage bis dato errechnen und auch anzeigen.

Ich habe es mit unixtime versucht aber da nicht wirklich eine Lösung gefunden.

Könnte mir da jemand helfen?

Im englischen Teil des Forum müssen die Beiträge und Diskussionen in englischer Sprache verfasst werden. Deswegen wurde diese Diskussion in den deutschen Teil des Forums verschoben.

mfg ein Moderator.

1 Like

Unixtime ist eigentlich der beste Weg, um erst mal eine Differenz zu bekommen.
Versuche mal mit dieser Differenz localTime zu speisen oder mit gmtime eine Zeit zu bauen. Ungetestet und nur aus dem Gedächtnis.

Gruß Tommy

Hallo @atercor

herzlich willkommen im Arduino-Forum.
Arduino-Programmieren kann eine Menge Spaß machen.
Hier im Forum kann man - wenn man möchte - hunderte Fragen stellen.
Wenn erkennbar ist, dass der Fragende dazulernen will dann wird auch gerne geantwortet.

Du hast im Titel stehen "Lebensuhr"
Ach du Alarm ! Und dann sollen die verbleibenden Tage angezeigt werden?
Woher willst du wissen wie viel Tage "verbleiben"?
Ich kann nicht wirklich einschätzen was das bedeutet.
Vorsichtshalber poste ich erst einmal das:

Nicht direkt , habe was gefunden :wink:
https://namespace-cpp.de/std/doku.php/lernen/datum

1 Like

Lebensuhr , ja!
Oder einfach gesagt ein Countdown bis zu einem bestimmten Datum.

Danke
Das hilft mir schonmal ein klein wenig weiter.

Mein gedankenweg war eigentlich super einfach nur ich bekomme das nicht umgesetzt...

Gibt es eine library wo man einfach ein Datum eingibt und sie das in UNIX time umwandelt.
Dann kann man man das Enddatum auch in Unix umwandeln und das dann ausrechnen lassen und zurück in ein Datum umwandeln.

Weis einer ab es sowas schon gibt.
und wenn nicht kann jemand sowas programmieren.

cu

Hi @atercor ,

hier gab es im englischsprachigen Bereich des Forums

https://forum.arduino.cc/t/issue-with-0-96-display-code-issue/1195653

jemanden, der gerne eine Anwendung programmieren wollte, welche die Zeit bis Weihnachten in verschiedenen Zeiteinheiten anzeigt.

Die Lösung der Aufgabe, die ich für ihn auf Wokwi bereitgestellt habe, sollte alles beinhalten, was Du für Deine Anwendung benötigst. Die Berechnung der Zeit zwischen zwei Daten ist dort allgemein und im speziellen für die Zeit bis zum nächsten Heiligabend nachzuvollziehen.

/*

  Forum: https://forum.arduino.cc/t/issue-with-0-96-display-code-issue/1195653
  Wokwi: https://wokwi.com/projects/383021035087828993

*/


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

RTC_DS1307 rtc;
DateTime now, prev;

void setup() {
  Serial.begin(115200);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    for (;;); // Don't proceed, loop forever
  }
  // Clear the buffer
  display.clearDisplay();
  display.display();

  display.setTextSize(1);                 // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);    // Draw white text

}

void loop () {
  now = rtc.now();
  if (now.second() != prev.second()) {
    prev = now;
    printTimeToSerial();
    printTimeToDisplay();
  }
}


void printTimeToDisplay() {
  char buf[80];
  sprintf(buf, " %0.2d.%0.2d.%4d  %0.2d:%0.2d:%0.2d",
          now.day(), now.month(), now.year(),
          now.hour(), now.minute(), now.second()
         );
  display.clearDisplay();
  display.setCursor(0, 2);
  display.println(buf);
  display.println();
  display.println(" Still to go ...");
  display.setCursor(0, 30);
  display.print("    Days:    ");
  display.println(daysUntilXmas());
  display.print(" or Hours:   ");
  display.println(hoursUntilXmas());
  display.print(" or Minutes: ");
  display.println(minutesUntilXmas());
  display.print(" or Seconds: ");
  display.println(secondsUntilXmas());
  display.drawLine(0, 10, display.width(), 10, SSD1306_WHITE);
  display.display();
}



void printTimeToSerial() {
  char buf[80];
  sprintf(buf, "Current time: %0.2d.%0.2d.%4d  %0.2d:%0.2d:%0.2d",
          now.day(), now.month(), now.year(),
          now.hour(), now.minute(), now.second()
         );
  Serial.println(buf);
  Serial.print("Days until Xmas: \t");
  Serial.println(daysUntilXmas());
  Serial.print("Hours until Xmas:\t");
  Serial.println(hoursUntilXmas());
  Serial.print("Minutes until Xmas:\t");
  Serial.println(minutesUntilXmas());
  Serial.print("Seconds until Xmas:\t");
  Serial.println(secondsUntilXmas());
}


// "days difference" means "days" not (24 hours per day)!!!
// e.g. on 12/23 there will be "1 day to go" until 23:59:59 o'clock
long daysUntilXmas() {
  int XmasYear = now.year();
  if (now.month() == 12 && now.day() > 24) {
    XmasYear++;
  }
  return daysDiff(now.year(), now.month(), now.day(),
                  XmasYear,          12,     24);

}


// Minutes difference based on 12/24 at 0 o'clock in the morning local RTC time
long minutesUntilXmas() {
  int XmasYear = now.year();
  if (now.month() == 12 && now.day() > 24) {
    XmasYear++;
  }
  return minutesDiff(now.year(), now.month(), now.day(), now.hour(), now.minute(),
                     XmasYear,          12,       24,           0,            0);
}

// Full hours difference based on minutesUntilXmas
long hoursUntilXmas() {
  long minutes = minutesUntilXmas();
  return (minutes / 60);
}


// Seconds difference based on minutesUntilXmas() plus rest of recent minute
long secondsUntilXmas() {
  long minutes = minutesUntilXmas();
  return (60 - now.second() + minutes * 60);
}

//
// Source https://forum.arduino.cc/t/datetime-calculations/354776/16
//  "Julian Day Number"
//
long JD(int year, int month, int day)
{ // COMPUTES THE JULIAN DATE (JD) GIVEN A GREGORIAN CALENDAR
  return day - 32075 + 1461L * (year + 4800 + (month - 14) / 12) / 4 + 367 * (month - 2 - (month - 14) / 12 * 12) / 12 - 3 * ((year + 4900 + (month - 14) / 12) / 100) / 4;
}

long daysDiff(int year1, int mon1, int day1, int year2, int mon2, int day2)
{
  return JD(year2, mon2, day2) - JD(year1, mon1, day1);
}

long minutesDiff(int year1, int mon1, int day1, int hour1, int min1, int year2, int mon2, int day2, int hour2, int min2 )
{
  int mDiff = (hour2 * 60 + min2) - (hour1 * 60 + min1);
  return daysDiff(year1, mon1, day1, year2, mon2, day2) * 1440 + mDiff;
}

Du kannst das Ergebnis beim Online-Simulator Wokwi anschauen und bei Bedarf auch auf dieser Plattform anpassen und testen.

https://wokwi.com/projects/383021035087828993

Viel Spaß damit!
ec2021

Hab ein ESP8266 mit Wifi ect. plus OLED mit sowas programmiert.
Code hier:

#include <Wire.h>
#include <ESP8266WiFi.h>
#include <U8g2lib.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <FS.h>
#include <LittleFS.h>
#include "secrets.h"
/*
  // OLED Configuration
  #define SCREEN_WIDTH 128
  #define SCREEN_HEIGHT 64
  U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, D3, D2);
*/
int myBackground = 0;
int myDrawColor = 1;

// WiFi and NTP Configuration
char* ssid;
char* password;
int trycnt; // Versuche zu verbinden
#define MAXTRY 30
const char* ntpServerName = "pool.ntp.org";
const long utcOffsetInSeconds = 3600;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServerName, utcOffsetInSeconds);

ESP8266WebServer server(80);
/* default Ziel: renteneinstieg hjs */
int targetYear = 2025;
int targetMonth = 11;
int targetDay = 11;

void setup() {
  Serial.begin(115200);
  // Initialize the display
  u8g2.begin();
  u8g2.firstPage();
  delay(100); // bis Serielle Schnittstelle bereit?
  Serial.println("\nSetup startet");
  Status(1, "Setup startet");
  { // Connect to Wi-Fi
    ssid = SSID1;
    password = PASS1;
    Serial.print("\nTry connect to ");
    Serial.print(ssid);
    WiFi.begin(ssid, password);
    while ((WiFi.status() != WL_CONNECTED) && (trycnt < MAXTRY)) {
      delay(1000);
      Serial.print(".");
      trycnt++;
    }
    if (trycnt < MAXTRY) {
      Serial.print(" Connected!");
      // Display the IP address on the serial monitor
      Serial.print(" IP Address: ");
      Serial.println(WiFi.localIP());
    }
    else {
      trycnt = 0;
      ssid = SSID2;
      password = PASS2;
      Serial.print("\nTry connect to ");
      Serial.print(ssid);
      WiFi.begin(ssid, password);
      while ((WiFi.status() != WL_CONNECTED) && (trycnt < MAXTRY)) {
        delay(1000);
        Serial.print(".");
        trycnt++;
      }
      if (trycnt < MAXTRY) {
        Serial.print("Connected!");
        // Display the IP address on the serial monitor
        Serial.print(" IP Address: ");
        Serial.println(WiFi.localIP());
      }
      else { // Try 3
        trycnt = 0;
        ssid = SSID3;
        password = PASS3;
        Serial.print("\nTry connect to ");
        Serial.print(ssid);
        WiFi.begin(ssid, password);
        while ((WiFi.status() != WL_CONNECTED) && (trycnt < MAXTRY)) {
          delay(1000);
          Serial.print(".");
          trycnt++;
        }
        if (trycnt < MAXTRY) {
          Serial.print("Connected!");
          // Display the IP address on the serial monitor
          Serial.print(" IP Address: ");
          Serial.println(WiFi.localIP());
        }
        else {
          Serial.println("\n\nNO WLAN --- stop it!");
          while (true) {}
        }
      }
    }
  }
  { // Initialize OTA
    ArduinoOTA.onStart([]() {
      Serial.println("\nOTA update starting");
    });
    ArduinoOTA.onEnd([]() {
      Serial.println("\nOTA update finished");
    });
    ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
      Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
    });
    ArduinoOTA.onError([](ota_error_t error) {
      Serial.printf("OTA Error[%u]: ", error);
      if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
      else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
      else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
      else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
      else if (error == OTA_END_ERROR) Serial.println("End Failed");
    });
    ArduinoOTA.begin();
  }
  // Initialize NTPClient
  timeClient.begin();
  { // Setup web server routes
    server.on("/", HTTP_GET, handleRoot);
    server.on("/setdate", HTTP_POST, handleSetDate);
    server.begin();
  }
}

void loop() {
  ArduinoOTA.handle();  // Handle OTA updates
  server.handleClient(); // Browser IO

  { // Update the time & display ect.
    timeClient.update();

    // Convert epoch time to tm struct
    time_t rawTime = timeClient.getEpochTime();
    struct tm * timeInfo = localtime(&rawTime);

    // Get current date
    int currentYear = timeInfo->tm_year + 1900;
    int currentMonth = timeInfo->tm_mon + 1;
    int currentDay = timeInfo->tm_mday;

    // Calculate remaining days to target
    int remainingDays = calculateRemainingDays(currentYear, currentMonth, currentDay);
    int remainingHours = remainingDays * 24 + calculateRemainingHours(timeInfo->tm_hour);

    if (timeInfo->tm_sec >= 30)    {
      myBackground = 0;
      myDrawColor = 1;
    }
    else    {
      myBackground = 1;
      myDrawColor = 0;
    }
    // Display on OLED
    do {
      u8g2.setDrawColor(myBackground);
      u8g2.drawBox(0, 0, 127, 63);
      u8g2.setDrawColor(myDrawColor);
      u8g2.setFont(u8g2_font_ncenB08_tr);
      u8g2.setCursor(0, 10);
      u8g2.print("Date: ");
      if (currentDay < 10) u8g2.print("0");
      u8g2.print(currentDay);
      u8g2.print(".");
      if (currentMonth < 10) u8g2.print("0");
      u8g2.print(currentMonth);
      u8g2.print(".");
      u8g2.println(currentYear);

      u8g2.setCursor(0, 21);
      u8g2.print("Time: ");
      u8g2.print(timeInfo->tm_hour);
      u8g2.print(":");
      if (timeInfo->tm_min < 10) u8g2.print("0");
      u8g2.print(timeInfo->tm_min);
      u8g2.print(":");
      if (timeInfo->tm_sec < 10) u8g2.println("0");
      u8g2.print(timeInfo->tm_sec);

      u8g2.setCursor(0, 32);
      u8g2.print("Days until ");
      u8g2.print(targetDay);
      u8g2.print(".");
      if (targetMonth < 10) u8g2.print("0");
      u8g2.print(targetMonth);
      u8g2.print(".");
      u8g2.print(targetYear);
      u8g2.setCursor(32, 62);
      u8g2.print(WiFi.localIP());
      u8g2.setCursor(5, 51);
      u8g2.setFont(u8g2_font_ncenB14_tf);
      u8g2.print(remainingDays);
      u8g2.setFont(u8g2_font_ncenB08_tr);
      u8g2.print(" < ");
      u8g2.print(remainingHours);
      u8g2.print(" h");
    } while (u8g2.nextPage());
  }
  delay(1000);
}

void Status(int lineNr, char* text) {
  do {
    u8g2.setDrawColor(myBackground);
    u8g2.drawBox(0, 0, 127, 63);
    u8g2.setDrawColor(myDrawColor);
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(0, lineNr * 10);
    u8g2.print(text);
  } while  (u8g2.nextPage());
}
int calculateRemainingDays(int currentYear, int currentMonth, int currentDay) {
  int remainingDays = 0;
  if ((currentYear == targetYear) && (currentMonth == targetMonth)) remainingDays = targetDay - currentDay; // im gleichen Monat
  else { // Berechnung über Monatsgrenze
    remainingDays = daysInMonth(currentYear, currentMonth) - currentDay + targetDay; // verbleibende Tage in aktuellen Monat + Tage im Zielmonat
    if (currentYear == targetYear) { // Unterschiedliche Monate im gleichen Jahr
      if (currentMonth + 1 < targetMonth) for (int Month = currentMonth + 1; Month < targetMonth; Month++) remainingDays += daysInMonth(currentYear, Month);
    }
    else { // Berechnung über Jahresgrenze
      if (currentMonth < 12) for (int Month = currentMonth + 1; Month < 13; Month++) remainingDays += daysInMonth(currentYear, Month); // Restlichen vollen Monate bis Jahresende
      if (targetMonth > 1) for (int Month = 1; Month < targetMonth; Month++) remainingDays += daysInMonth(targetYear, Month); // Restlichen vollen Monate im Zieljahr
      if (currentYear + 1 < targetYear) for (int Year = currentYear + 1; Year < targetYear; Year++ ) for (int Month = 1; Month < 13; Month++) remainingDays += daysInMonth(Year, Month); // Jahre dazwischen
    }
  }
  return remainingDays - 1; // Aktueller Tag zählt nicht voll!
}

int calculateRemainingHours(int currentHour) { // berechne Stunden bis Mitternacht
  return (24 - currentHour);
}

int daysInMonth(int year, int month) {
  static const int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int daysInMonth = days[month];
  // Check for leap year
  if (month == 2 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) {
    daysInMonth = 29;
  }
  return daysInMonth;
}

void handleRoot() {
  String html = "<html><body>";
  html += "<h1>Berechne Tage bis</h1>";
  html += "<form action='/setdate' method='post'>";
  html += "Tag: <input type='text' name='day'><br><br>";
  html += "Monat: <input type='text' name='month'><br><br>";
  html += "Jahr: <input type='text' name='year'><br><br>";
  html += "<input type='submit' value='Set Date'>";
  html += "</form></body></html>";

  server.send(200, "text/html", html);
}

void handleSetDate() {
  targetYear = server.arg("year").toInt();
  targetMonth = server.arg("month").toInt();
  targetDay = server.arg("day").toInt();
  server.send(200, "text/plain", "Date set successfully!");
}

Das Filesystem hab ich noch nicht ausprogrammiert, also evtl. die includes einfach rausschmeissen.

in "secrets.h" stehen meine WLAN Daten (SSIDs und Passwörter). Im scetch versuche ich in 3 verschiedene WLAN zu kommen (Arbeit, zuHause, hotspot) - kann man sicher auch optimieren.

Danke für eure Hilfe ich schau mir das mal an.

meine aktuelle Lösung (ein wenig abgeändert was das Ergebnis angeht ) schaut so aus:


                                                                                                                  // benötigte Bibliothek einbinden
#include "RTClib.h"
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>


                                                                                                                  // Name des RTC-Moduls (rtc)
RTC_DS3231 rtc;

                                                                                                                  // definieren des displays
#define OLED_RESET     -1                                                                                         // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_WIDTH 128                                                                                          // OLED display Breite, in pixels
#define SCREEN_HEIGHT 64                                                                                          // OLED display Höhe, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


int (lebenstag) = 22;
int (lebensmonat) = 10;
int (lebensjahr)= 2034;
int (tage);
int (monate);
int (jahre);
int (a);
int (b);
int (c) = 0;
int (d) = 0;
int (lm);
int (e);
int (f);
int (h);
int (i);

int pushplus = 4; 
int pushminus = 3; 
int pushaus = 2;




void setup()
{
  
Serial.begin(115200);
 
   pinMode(pushplus, INPUT);
  pinMode(pushminus, INPUT);
  pinMode(pushaus, INPUT);

  
  rtc.begin();
  
  
  Wire.begin();
  
                                                                                                                   // RTC einstellen nachdem hochladen wieder deaktivieren und neu hochladen
 //rtc.adjust(DateTime(2024, 1, 30, 17, 51, 00));
 
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                                                                         // I2C address = 0x3C


  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor (0, 0);
  display.print("Von Marco ");
  display.setCursor (0, 10);
  display.print("Programierte");
  display.setCursor (15, 30);
  display.setTextSize(2);
  display.print("Lebensuhr");
  display.display();
  delay(2000);
  display.clearDisplay();
  display.display();
 
}

void loop ()
{

  display.clearDisplay();
  display.setTextSize(1);


  
int d = digitalRead(pushaus);
if (d == 1){
  c++;
  delay(200);
}

if (c > 5)
{
  c=0;
}

                                                                                                                    // Tage einstellen
if (c==1) {
     display.setTextSize(2);
     display.setCursor (0, 0);
     display.print("Sterbetag");
     
     
     int e = digitalRead(pushplus);
     if (e == 1){
     lebenstag++;
     delay(200);
   }

    if (lebenstag > 31)
  {
  lebenstag=1;
}
  int f = digitalRead(pushminus);
  if (f==1){
    lebenstag--;
    delay(200);
}
 if (lebenstag < 1)
  {
  lebenstag=31;
}

display.setTextSize(5);
display.setCursor (30,25);
display.print(lebenstag);
}
                                                                                                            //Monat einstellen

    if (c==2) { 
 
    display.setTextSize(2);
     display.setCursor (0, 0);
     display.print("Sterbemon.");



  
        
   int f = digitalRead(pushplus);
     if (f == 1){
     lebensmonat++;
     delay(200);
   }

    if (lebensmonat > 12)
  {
  lebensmonat=1;
}
 int g = digitalRead(pushminus);
     if (g == 1){
     lebensmonat--;
     delay(200);
   }

    if (lebensmonat < 1)
  {
  lebensmonat=12;
      
    }
display.setTextSize(5);
display.setCursor (30,25);
display.print(lebensmonat);


    
}
                                                                                                      // Jahr einstellen
    if (c==3)
    { 
   int h = digitalRead(pushplus);
     if (h == 1){
     lebensjahr++;
     delay(200);
   }

    if (lebensjahr > 2200)
  {
  lebensjahr=2024;
}
 int i = digitalRead(pushminus);
     if (i == 1){
     lebensjahr--;
     delay(200);
   }

    if (lebensjahr < 2024)
  {
  lebensjahr=2200;
      
    }
    display.setTextSize(2);
    display.setCursor (0, 0);
    display.print("Sterbejahr");
    display.setTextSize(5);
    display.setCursor (0,25);
    display.print(lebensjahr);
}


















if (c==0)
{

    display.setTextSize(1);


  DateTime now = rtc.now();
  DateTime aktuell = rtc.now();
  char Datum[] = "DD.MM.YYYY ";
  char Zeit[] = "hh:mm:ss";
  String Temperatur = String(rtc.getTemperature());
  Temperatur.replace(".", ",");


  Serial.print(aktuell.toString(Datum));
  Serial.print(aktuell.toString(Zeit));
  Serial.println(" Temperatur: " + Temperatur + "°C");
 

  display.setCursor (0, 0);
  display.print(aktuell.toString(Datum));
  display.setCursor (75, 0);
  display.print(aktuell.toString(Zeit));
  display.setCursor (90, 55);
  display.print(Temperatur + "C");

  


  

                                                                                                             // Berechnung der jahre                    2034 ändern
  
  display.setCursor (0, 35);
  display.print ("Jahre:");
  jahre = (lebensjahr - now.year () - b);
  display.setCursor (50, 35);
  display.print(jahre);

  
                                                                                                             // Berechnung der monate                    

  if (lebensmonat > now.month() ) {
    monate = (lebensmonat - now.month ()-a);
    b = 0;
  }
  
if (10 < now.month() ) {
    monate = (12 + lebensmonat - now.month ()-a);
    b = 1;
  }

  display.setCursor (0, 45);
  display.print ("Monate:");
  display.setCursor (50, 45);
  display.print(monate);

                                                                                                              // Tage berechnung                        

if (lebenstag  > now.day () ) {
  tage = (lebenstag - now.day ());
  a = 0;
}


if (lebenstag  < now.day () ) {
   tage = (lm - now.day () + lebenstag);
   a = 1;
}
   
  display.setCursor (0, 55);
  display.print ("Tage:");
  display.setCursor (50, 55);
  display.print(tage);
  

switch (now.month()) 
{
    
    case 1:  
      lm = 31;
      break;
    case 2:  
      lm = 28;
      break;
    case 3:    
      lm = 31;
      break;
    case 4:  
      lm = 30;
      break;
    case 5:  
      lm = 31;
      break;
    case 6:    
      lm = 30;
      break;
    case 7:  
      lm = 31;
      break;
    case 8:  
      lm = 31;
      break;
    case 9:    
      lm = 30;
      break;
    case 10:  
      lm = 31;
      break;
    case 11:  
      lm = 30;
      break;
    case 12:    
      lm = 31;
      break;
      case 13:    
      lm = 31;
      break;
 }



  


    display.setCursor (0, 24);
    display.print (lebenstag);
    display.setCursor (17, 24);
    display.print (lebensmonat);
    display.setCursor (36, 24);
    display.print (lebensjahr);
    display.setCursor (0, 12);
    display.print ("Sterbedatum:");
    display.drawRect(14, 29, 2, 2, WHITE);
    display.drawRect(32, 29, 2, 2, WHITE);
  }
  if (c==4)
  {
    DateTime aktuell = rtc.now();
    char Datum[] = "DD.MM.YYYY ";
   char Zeit[] = "hh:mm";
    String Temperatur = String(rtc.getTemperature());
   Temperatur.replace(".", ",");

  
    display.clearDisplay();
    display.setTextSize(3);
    display.setCursor (0, 0);
    display.print(aktuell.toString(Zeit));
    display.setCursor (0, 44);
    display.print(Temperatur + " C");
    display.setTextSize(2);
    display.setCursor (0, 26);
    display.print(aktuell.toString(Datum));






  
}

if (c==5) {
     DateTime now = rtc.now();
  DateTime aktuell = rtc.now();
  char Datum[] = "DD.MM.YYYY ";
  char Zeit[] = "hh:mm:ss";
  String Temperatur = String(rtc.getTemperature());
  Temperatur.replace(".", ",");

  
    display.clearDisplay();
    display.setTextSize(2);

    display.setCursor (0, 46);
    display.print ("Tage:");
    display.setCursor (85, 46);
    display.print(tage);
    display.setCursor (0,23);
    display.print ("Monate:");
    display.setCursor (85, 23);
    display.print(monate);
    display.setCursor (0, 0);
    display.print ("Jahre:");
    display.setCursor (85, 0);
    display.print(jahre);
  }

    display.display();
  }

hab da nur noch ein Problem mit den Schaltjahren

Das scheint nur so.
Du hast auch noch ein Problem mit flackerndem Displayinhalt (wenn die delay() nicht wären) und ein weiteres mit der Erkennung der Tastendrücke - die werden nämlich nach Aktivierung erst nach zwei Sekunden wieder mal angeschaut.

Das sollte eigentlich mit einem der Konstruktoren aus der RTCLib gehen:

// aus RTCLib.h:
// DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour = 0,  uint8_t min = 0, uint8_t sec = 0);

DateTime lebensDatum(lebensjahr, lebensmonat, lebenstag);

...und damit dann analog zum Christmas Countdown weiter verfahren.

Und ich habe noch ein Problem mit der Lesbarkeit Deines Codes. Bitte vor dem Posten wenigstens einmal auf STRG-T zum Formatieren drücken.

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