#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4
int Uhr_h; // Variablen für Uhrzeit
int Sommer;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2022, 10, 9, 2, 59, 0));
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)// Check your I2C address and enter it here, in Our case address is 0x3C
display.clearDisplay();
display.display(); // this command will display all the data which is in buffer
display.setTextColor(WHITE, BLACK);
display.drawRect(118, 25, 3, 3, WHITE); // Put degree symbol ( ° )
draw_text(0, 25, "Temperatur =", 1);
draw_text(122, 25, "C", 1);
}
void loop() {
Sommer = Sommerzeit();
DateTime now = rtc.now();
/*============Display Date=================*/
display.setTextSize(1);
display.setCursor(0,0);
display.print(daysOfTheWeek[now.dayOfTheWeek()]);
char currentDate [16];
uint8_t thisDay, thisMonth ;
thisDay = now.day();
thisMonth = now.month();
sprintf (currentDate, "%02d/%02d/", thisDay, thisMonth); //add leading zeros to the day and month
display.setTextSize(1);
display.setCursor(62,0);
display.print(currentDate);
display.setTextSize(1);
display.setCursor(102,0);
display.print(now.year(), DEC);
/*================Display Time================*/
char buffer [16];
uint8_t thisSec, thisMin, thisHour;
thisSec = now.second();
thisMin = now.minute();
thisHour = (Uhr_h);
sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);
display.setTextSize(2);
display.setCursor(15,9);
display.print(buffer);
/*=============Display Temperature=====================*/
display.setTextSize(1);
display.setCursor(82,25);
display.print(rtc.getTemperature());
display.display();
}
void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
display.setCursor(x_pos, y_pos);
display.setTextSize(text_size);
display.print(text);
display.display();
Serial.println(Uhr_h);
Serial.println(Sommer);
}
// Subroutine Uhrzeit ---------------------------------------------------------------
void Uhrzeit() {
DateTime now = rtc.now(); // Datum und Zeit ermitteln
Uhr_h = int(now.hour()); // Stunden ermitteln
if (Sommer = true) { // bei Sommerzeit
Uhr_h = int(now.hour()+1); } // 1 Stunde addieren
// Uhrzeit
} // Ende Uhrzeit ----------------------------------------------------------------
// Subroutine Sommerzeit ermitteln --------------------------------------------------
boolean Sommerzeit() {
DateTime now = rtc.now();
if (now.month()<3 || now.month()>10) return false; // keine Sommerzeit Jan - Dez
if (now.month()>3 && now.month()<10) return true; // Sommerzeit Apr bis Sep
if (now.month()==3 && now.day()<25) return false; // keine Sommerzeit bis 25.März
if (now.month()==10 && now.day()<25) return true; // Sommerzeit bis 25.Okt.
if (now.month()==3 && (now.hour() + 24 * now.day())
>=(1 + 24*(31 - (5 * now.year() /4 + 4) % 7))
|| now.month()==10 && (now.hour() + 24 * now.day())
<(1 + 24*(31 - (5 * now.year() /4 + 1) % 7)))
return true;
else
return false;
} // Ende Sommerzeit -------------------------------------------------------------