#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int pause=1000;
RTC_DS1307 rtc;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_1 12
bool state_button_1 = false;
int sleepCounter = 0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup() {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
pinMode(BUTTON_1, INPUT_PULLUP);
digitalWrite(BUTTON_1, HIGH);
display.display(); //display initial Adafruit logo
delay(2000);
// Clear the buffer
display.clearDisplay();
display.display();
}
String formatOrdinal(uint8_t x) {
String suffix;
if (x >= 11 && x <= 13) {
suffix = "th";
} else {
switch (x % 10) {
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default:
suffix = "th";
break;
}
}
return String(x) + suffix;
}
String DayMonthYear(uint8_t Day, uint8_t Month, uint16_t Year){
String DayMonthYearText;
if (Month == 1) DayMonthYearText = "JAN ";
if (Month == 2) DayMonthYearText = "FEB ";
if (Month == 3) DayMonthYearText = "MAR ";
if (Month == 4) DayMonthYearText = "APR ";
if (Month == 5) DayMonthYearText = "MAY ";
if (Month == 6) DayMonthYearText = "JUN ";
if (Month == 7) DayMonthYearText = "JUL ";
if (Month == 8) DayMonthYearText = "AUG ";
if (Month == 9) DayMonthYearText = "SEP ";
if (Month == 10) DayMonthYearText = "OCT ";
if (Month == 11) DayMonthYearText = "NOV ";
if (Month == 12) DayMonthYearText = "DEC ";
DayMonthYearText = DayMonthYearText + formatOrdinal(Day) + " " + Year;
return DayMonthYearText;
}
String AddLeadingZero(uint8_t x){
String AddLeadingZeroText;
if (x < 10) AddLeadingZeroText = "0";
else AddLeadingZeroText = "";
AddLeadingZeroText = AddLeadingZeroText + x;
return AddLeadingZeroText;
}
String CurrentTime(uint8_t h, uint8_t i){
String CurrentTimeText = "";
CurrentTimeText = CurrentTimeText + AddLeadingZero(h) + ":" + AddLeadingZero(i);
return CurrentTimeText;
}
void drawCentreString(String text, int y, int textSize=1, int textColor=SSD1306_BLACK) {
// Calculate the length of the text to determine the center position
int16_t positionX, positionY;
uint16_t textWidth, textHeight;
display.setTextSize(textSize);
display.getTextBounds(text.c_str(), 0, 0, &positionX, &positionY, &textWidth, &textHeight);
int16_t textX = (SCREEN_WIDTH - textWidth) / 2;
// Display text on center
display.setCursor(textX, y);
display.setTextSize(textSize);
display.setTextColor(textColor);
display.println(text);
}
void clockState() {
DateTime now = rtc.now();
display.fillRect(0, 0, 128, 20, SSD1306_WHITE);
display.fillRect(0, 22, 128, 35, SSD1306_WHITE);
display.fillRect(0, 35, 128, 46, SSD1306_BLACK);
// display Day of Week
String daysOfTheWeekStr = daysOfTheWeek[now.dayOfTheWeek()];
drawCentreString(daysOfTheWeekStr, 2, 2);
// display Day Month Year
String dayMonthYearStr = DayMonthYear(now.day(), now.month(), now.year());
drawCentreString(dayMonthYearStr, 24);
// display CurrentTime
display.setCursor(0, 40);
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.println(CurrentTime(now.hour(), now.minute()));
display.setCursor(88, 40);
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.println(":");
display.setCursor(102, 44);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.println(AddLeadingZero(now.second()));
display.display();
delay(1000);
}
void enterSleepMode(bool mode) {
if (mode) display.ssd1306_command(SSD1306_DISPLAYOFF);
else display.ssd1306_command(SSD1306_DISPLAYON);
}
void loop() {
if (digitalRead(BUTTON_1) == 0) {
state_button_1 = !state_button_1;
Serial.println(state_button_1);
delay(300);
}
if (!state_button_1) {
enterSleepMode(false);
clockState();
} else {
enterSleepMode(true);
}
}