#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> //OLED display library
#include <Wire.h> //Allows communication of Arduino board and OLED display
#include "RTClib.h" //Real time clock library
#include <Fonts/FreeSans9pt7b.h>
RTC_DS1307 rtc; //create an instant of RTC
DateTime previous;
DateTime current;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
int toggle_1;
int time_1;
int alpha_1;
float timer_1;
int trigger_1;
int alarm_1 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(6, INPUT);
//check rtc connection
if (!rtc.begin()) {
Serial.println("Can Not Find RTC");
Serial.flush();
abort();
}
previous = rtc.now();
//Calibrate the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
display.display();
delay(1000);
display.clearDisplay();
display.display();
toggle_1 = 0;
time_1 = 0;
alpha_1 = 0;
trigger_1 = 0;
attachInterrupt(digitalPinToInterrupt(2),pressIn,FALLING);
attachInterrupt(digitalPinToInterrupt(3),pressOut,RISING);
}
void loop() {
if(toggle_1 == 0){
DateTime now = rtc.now();
int hour = now.hour(); //Declare the hour
int minute = now.minute(); //Declare the minute
int second = now.second(); //Declare the seconds
// the following for loop changes the value of min if seonds reaches 60, same for hour
for(int i=0;i<10;i++){
clock(hour, minute, second);
delay(1000);
second++;
if(second >= 60)
{
minute++;
}
second = second % 60;
if(minute >= 60)
{
hour++;
}
minute = minute % 60;
hour = hour % 24;
if(toggle_1 == 1){
i = 11;
}
}
}
else{ //If the condition above is not true then the following code is executed
stopwatch();
}
}
//The following executes when the button is pressed in
void pressIn()
{
time_1 = millis();
}
//The following executes once the button is released
void pressOut()
{
alpha_1 = millis() - time_1;
if(alpha_1 > 300)
{
if(toggle_1 == 0)
{
toggle_1 = 1;
Serial.end();
Serial.begin(9600);
//Asks user to enter a value in seconds
Serial.println("Enter alarm value");
}
else
{
toggle_1 = 0;
}
}
else
{
if(trigger_1 == 0)
{
trigger_1 = 1;
timer_1 = 0;
}
else
{
trigger_1 = 0;
}
}
}
//The following function creates the stopwatch
void stopwatch()
{
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.setCursor(45,15);
display.print("TIME");
display.setCursor(45,34);
if(timer_1 > alarm_1)
{
analogWrite(4,200);
}
else
{
analogWrite(4,0);
}
if(trigger_1 == 1)
{
display.print(timer_1);
timer_1 = timer_1 + 0.1;
delay(10);
}
else
{
display.print(timer_1);
}
display.display();
while(Serial.available() == 0)
{
alarm_1 = (Serial.parseInt());
Serial.println(alarm_1);
}
}
//The following code creates the analog clock
void clock(int hour, int minute, int second)
{
display.setTextSize(1); //Sets size of the font
display.setTextColor(SSD1306_WHITE); //Sets font and lines color to white
const int r=15;
const double rot=-M_PI/2;
double x,y,x0,y0,anglerad;
display.clearDisplay();
display.drawPixel(24,24,WHITE);
display.drawCircle(24,24,r,WHITE);
for(int i=0;i<12;i++){
int angled=360/12*i;
anglerad=2*M_PI/12*i+rot;
x=r*cos(anglerad);
y=r*sin(anglerad);
x0=(r-3)*cos(anglerad);
y0=(r-3)*sin(anglerad);
display.drawLine(24+x0,24+y0,24+x,24+y,WHITE);
}
//Prints the "TIME" on the LCD display at set coordinates
display.setCursor(80,10);
display.print("TIME");
display.setCursor(70,24);
//Prints the digital clock hour and minutes
display.print(hour);
display.print(":");
display.print(minute);
display.print(":");
anglerad=2*M_PI/12*(hour%12)+2*M_PI/12/60*minute+rot;
x=(r-7)*cos(anglerad);
y=(r-7)*sin(anglerad);
x0=0;
y0=0;
display.drawLine(24+x0,24+y0,24+x,24+y,WHITE);
//minute
anglerad=2*M_PI/60*minute+rot;
x=(r-5)*cos(anglerad);
y=(r-5)*sin(anglerad);
x0=0;
y0=0;
display.drawLine(24+x0,24+y0,24+x,24+y,WHITE);
//second
display.setCursor(105,24);
display.print(second);
anglerad=2*M_PI/60*second+rot;
x=(r-2)*cos(anglerad);
y=(r-2)*sin(anglerad);
x0=0;
y0=0;
display.drawLine(24+x0,24+y0,24+x,24+y,WHITE);
display.display();
}