#include <Wire.h>
#include <RTClib.h>
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#define SCREEN_WIDTH 64
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS1307 rtc;
String time;
void setup() {
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000);
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
if (! rtc.begin()) {
Serial.println("errore rtc");
Serial.flush();;
while (true);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
time.reserve(10);;
}
void loop()
{
DateTime now = rtc.now();
time = "";
time += now.hour();
time += ':';
time += now.minute();
time += ':';
time += now.second();
oledDisplayCenter(time);
}
void oledDisplayCenter(String text)
{
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
oled.clearDisplay(); // clear display
oled.setCursor((SCREEN_WIDTH - width)/1, (SCREEN_HEIGHT - height)/2);
oled.println(text); // text to display
oled.display();
}