#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "RTClib.h"
//constants for OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
RTC_DS1307 rtc;
int x = 0;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char choice;
int CenterX = 64; //Centre coordinates for the LED display in pixels.
int CenterY = 32;
void setup() {
Serial.begin(9600);
while (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { //Checks if LED has been set up.
Serial.println("Display not connected!");
}
if (! rtc.begin()) { //Checks if RTC has been set up.
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
display.clearDisplay();
Serial.println("Analogue or Digital clock? A/D:");
}
void loop() {
int hours;
int minutes;
int seconds;
DateTime now = rtc.now();
//Getting the time from RTC.
hours = (now.hour());
minutes = (now.minute());
seconds = (now.second());
//Displaying Analog or Digital clock based on the input.
if (choice == 'A'){
analogClock(hours, minutes, seconds);
}else if (choice == 'D'){
digitalClock(hours, minutes, seconds);
}
}
//Digital clock display function.
void digitalClock(int hours, int minutes, int seconds){
display.setTextSize(2.5);
display.setTextColor(SSD1306_WHITE);
display.setCursor(18,22);
char buff[9];
sprintf(buff,"%02d:%02d:%02d", hours, minutes, seconds); //Formatting of data to display hours, mins, and seconds in 2 sig fig.
buff[8] = '\0';
display.print(buff);
display.display();
display.clearDisplay();
}
//Analog clock display function.
void analogClock(int hours, int minutes, int seconds){
int Radius = 25;
display.drawCircle(CenterX, CenterY, Radius, WHITE);
display.drawCircle(CenterX, CenterY, Radius+3, WHITE);
drawHour(hours, minutes);
drawMinute(minutes);
drawSeconds(seconds);
display.display();
display.clearDisplay();
}
//Drawing the hour hand on the analog clock.
void drawHour(int hour, int mins){
float total = (hour+(mins/60));
float hoursAngle = (total*(360/12));
//End coordinates of the hours hand based on the angle.
float EndX = CenterX+(10*sin(hoursAngle*0.017453));
float EndY = CenterY-(10*cos(hoursAngle*0.017453));
display.drawLine(CenterX, CenterY, EndX, EndY, WHITE);
}
//Drawing the minute hand on the analog clock.
void drawMinute(int min){
float minsAngle= (min*(360/60));
//End coordinates of the minute hand based on the angle.
float EndX = CenterX+(20*sin(minsAngle*0.017453));
float EndY = CenterY-(20*cos(minsAngle*0.017453));
display.drawLine(EndX, EndY, CenterX, CenterY, WHITE);
}
//Drawing the seconds hand on the analog clock.
void drawSeconds(int seconds){
float secsAngle= (seconds*(360/60));
float EndX = CenterX+(22*sin(secsAngle*0.017453));
float EndY = CenterY-(22*cos(secsAngle*0.017453));
display.drawLine(EndX, EndY, CenterX, CenterY, WHITE);
}
//Calls this function whenver there is data in the serial monitor. Used to convert between analog and digital clocks.
void serialEvent(){
String text = Serial.readStringUntil('\n');
if ((text[0] != 'A' && text[0]!= 'D')){
Serial.println("Invalid Selection");
}else{
choice = text[0];
}
Serial.println("Displaying " + String(choice));
Serial.println("Analogue or Digital clock? A/D:");
}