#include <Wire.h>
#include <DS3231.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define i2c_Address 0x3c
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DS3231 myRTC;
#define SW1_PIN 7
#define SW2_PIN 8
#define SW3_PIN 9
#define SW4_PIN 10
#define SW5_PIN 11
char dataHari[7][12] = { "Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu" };
char DayData[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
char dataBulan[12][5] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
String hari, bulan, day;
bool century = false;
bool mode12 = false;
bool h12;
bool hPM;
byte theSecond, theMinute, theHour, theDoW, theDate, theMonth, theYear;
void setup() {
Serial.begin(9600);
oled.begin(i2c_Address, true);
pinMode(SW1_PIN, INPUT_PULLUP);
pinMode(SW2_PIN, INPUT_PULLUP);
pinMode(SW3_PIN, INPUT_PULLUP);
pinMode(SW4_PIN, INPUT_PULLUP);
pinMode(SW5_PIN, INPUT_PULLUP);
// setMyRTC(0, 13, 4, 59, 3, 6, 8, 24);
}
char timeString[9];
char dateString[12];
void loop() {
readMyRTC(0, 1, &theHour, &theMinute, &theSecond, &theDoW, &theDate, &theMonth, &theYear);
// hari = dataHari[theDoW - 1];
sprintf_P(timeString, PSTR("%2d:%02d:%02d"), theHour, theMinute, theSecond);
sprintf_P(dateString, PSTR(", %02d-%02d-%02d"), theDate, theMonth, theYear);
day = DayOfTheWeek(theDoW);
show(2, 15, 10, timeString);
show(1, 15, 30, day + dateString);
// show(1, (oled.width() - day.length())/3, 38, day);
oled.display();
oled.clearDisplay();
// Serial.println((oled.width() - day.length())/2);
}
void show(uint8_t textSize, uint8_t x, uint8_t y, String s) {
oled.setTextSize(textSize);
oled.setTextColor(SH110X_WHITE);
oled.setCursor(x, y);
oled.print(s);
}
String DayOfTheWeek(uint8_t theDoW) {
String DayText;
for(int i = 0; i < 7; i++){
if (theDoW - 1 == i) DayText = DayData[i];
}
return DayText;
}
String AddLeadingZero(uint8_t x) {
String AddLeadingZeroText;
if (x < 10) AddLeadingZeroText = "0";
else AddLeadingZeroText = "";
AddLeadingZeroText = AddLeadingZeroText + x;
return AddLeadingZeroText;
}
String CurrentTime(uint8_t theHour, uint8_t theMinute, uint8_t theSecond) {
String CurrentTimeText = "";
CurrentTimeText = CurrentTimeText + AddLeadingZero(theHour) + ":" + AddLeadingZero(theMinute) + ":" + AddLeadingZero(theSecond);
return CurrentTimeText;
}
String CurrentDate(uint8_t theDate, uint8_t theMonth, uint8_t theYear) {
String CurrentDateText = "";
CurrentDateText = CurrentDateText + AddLeadingZero(theDate) + "-" + AddLeadingZero(theMonth) + "-" + theYear;
return CurrentDateText;
}
void setMyRTC(bool mode12, byte theHour, byte theMinute, byte theSecond, byte theDoW, byte theDate, byte theMonth, byte theYear) {
myRTC.setClockMode(mode12);
myRTC.setHour(theHour);
myRTC.setMinute(theMinute);
myRTC.setSecond(theSecond);
myRTC.setDoW(theDoW);
myRTC.setDate(theDate);
myRTC.setMonth(theMonth);
myRTC.setYear(theYear);
}
void readMyRTC(bool h12, bool hPM, byte *theHour, byte *theMinute, byte *theSecond, byte *theDoW, byte *theDate, byte *theMonth, byte *theYear) {
*theHour = myRTC.getHour(h12, hPM);
*theMinute = myRTC.getMinute();
*theSecond = myRTC.getSecond();
*theDoW = myRTC.getDoW();
*theDate = myRTC.getDate();
*theMonth = myRTC.getMonth(century);
*theYear = myRTC.getYear();
}