#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h> // Adafruit libraries
#include "RTClib.h" // reat time library
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_ADDR 0x3C
#define RED 0xF800
String inp;
int inp_mem;
int Centerx = 63;
int Centery = 31;
int x;
int y;
int x1;
int y1;
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT); // initializing the OLED Screen
RTC_DS1307 rtc; // Initializing the RTC Module
void setup() {
Serial.begin(115200);
Serial.println("Select Option.");
Serial.println("Option 1: Digital Clock.");
Serial.println("Option 2: Analog Clock.");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); // Starting up the display
display.clearDisplay();
display.display();
}
void loop() {
inp = Serial.readStringUntil("\n");
int input = inp.toInt();
if (input == 1) {
inp_mem = 1;
}
else if (input == 2) { // input memory to store user input
inp_mem = 2;
}
if (inp_mem == 1) {
DigitalClock();
}
else if (inp_mem == 2) { // compares user input memory and runs respective function
AnalogClock();
}
}
void AnalogClock() { // Analog Clock Function
DateTime now = rtc.now(); // Calls for the RTC Module
int h = now.hour(); // Real time Hour
int m = now.minute(); // Real time Minutes
int s = now.second(); // Real time Seconds
display.setTextColor(WHITE);
display.drawCircle(Centerx, Centery, 30, WHITE);
display.setCursor(20, 25); // display settings to display the clock
drawMinute(m);
drawHour(h, m);
drawSecond(s); // functions to draw the hours seconds and minutes
display.display();
display.clearDisplay();
delay(500);
}
void DigitalClock() { // Digital Clock Function
DateTime now = rtc.now();
int h = now.hour();
int m = now.minute();
int s = now.second(); // Grabs the real time hours minutes and seconds
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 25); // decides where to print on the screen
display.print(h);
display.print(':');
display.print(m);
display.print(':');
display.print(s);
display.display();
display.clearDisplay(); // displays all the real time
}
void drawMinute(int m) {
y = (24 * cos(PI - (2 * PI) / 60 * m)) + Centery; // x-coords of the minute hand
x = (24 * sin(PI - (2 * PI) / 60 * m)) + Centerx; // y-coords of the minute hand
display.drawLine(Centerx, Centery, x, y, WHITE); // draws a white line starting from center to the coordinate
}
void drawHour(int h, int m) {
y = (18 * cos(PI - (2 * PI) / 12 * h - (2 * PI) / 720 * m)) + Centery; // y-coords of the hour hand
x = (18 * sin(PI - (2 * PI) / 12 * h - (2 * PI) / 720 * m)) + Centerx; // x-coords of the hour hand
y1 = (18 * cos(PI - (2 * PI) / 12 * h - (2 * PI) / 720 * m)) + Centery + 1;
x1 = (18 * sin(PI - (2 * PI) / 12 * h - (2 * PI) / 720 * m)) + Centerx + 1; // secondary coords of the hour hand
display.drawLine(Centerx, Centery, x, y, WHITE);
display.drawLine(Centerx + 1, Centery + 1, x1, y1, WHITE); // draws two lines with a superposition of 1 so the line appears thicker
}
void drawSecond(int s) {
y = (24 * cos(PI - (2 * PI) / 60 * s)) + Centery; // x-coords of the second hand
x = (24 * sin(PI - (2 * PI) / 60 * s)) + Centerx; // y-coords of the second hand
display.drawLine(Centerx, Centery, x, y, WHITE); // draws a white line starting from center to the coordinate
}