#include "RTClib.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
int16_t x1, y1;
uint16_t textWidth, textHeight;
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup () {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
void loop () {
display.clearDisplay();
DateTime now = rtc.now();
if (now.hour() >= 10 && now.minute() >= 10 && now.second() >= 10) {
String tempString = String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
if (now.hour() >= 10 && now.minute() >= 10 && now.second() < 10) {
String tempString = String(now.hour()) + ":" + String(now.minute()) + ":" + "0" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
if (now.hour() >= 10 && now.minute() < 10 && now.second() >= 10) {
String tempString = String(now.hour()) + ":" + "0" + String(now.minute()) + ":" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
if (now.hour() < 10 && now.minute() >= 10 && now.second() >= 10) {
String tempString = "0" + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
if (now.hour() >= 10 && now.minute() < 10 && now.second() < 10) {
String tempString = String(now.hour()) + ":" + "0" + String(now.minute()) + ":" + "0" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
if (now.hour() < 10 && now.minute() < 10 && now.second() >= 10) {
String tempString = "0" + String(now.hour()) + ":" + "0" + String(now.minute()) + ":" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
if (now.hour() < 10 && now.minute() < 10 && now.second() < 10) {
String tempString = "0" + String(now.hour()) + ":" + "0" + String(now.minute()) + ":" + "0" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
if (now.hour() < 10 && now.minute() >= 10 && now.second() < 10) {
String tempString = "0" + String(now.hour()) + ":" + String(now.minute()) + ":" + "0" + String(now.second());
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.getTextBounds(tempString, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print(tempString);
}
Serial.print("Weite cursor: ");
Serial.print(64 - (textWidth / 2));
Serial.print(" ");
Serial.print("Weite: ");
Serial.print(textWidth);
Serial.print("\n");
Serial.print("Höhe cursor: ");
Serial.print(32 - (textHeight / 2));
Serial.print(" ");
Serial.print("Höhe: ");
Serial.print(textHeight);
Serial.print("\n\n");
String Tempreratur = "88.8C";
display.setTextSize(1);
display.getTextBounds(Tempreratur, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 48 - (textHeight / 2));
display.print(Tempreratur);
/*
String Feuchtigkeit = "88.8%";
display.setTextSize(1);
display.getTextBounds(Feuchtigkeit, 0, 0, &x1, &y1, &textWidth, &textHeight);
display.setCursor(64 - (textWidth / 2), 32 - (textHeight / 2));
display.print
*/
Serial.print("Weite cursor: ");
Serial.print(64 - (textWidth / 2));
Serial.print(" ");
Serial.print("Weite cursor: ");
Serial.print(textWidth);
Serial.print("\n");
Serial.print("Höhe cursor: ");
Serial.print(48 - (textHeight / 2));
Serial.print(" ");
Serial.print("Höhe: ");
Serial.print(textHeight);
Serial.print("\n\n");
display.display();
}