#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include "RTClib.h" //include alll the libraries
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
RTC_DS1307 rtc;
int Year,Month,Date,Hour,Minute,Second;
int second,minute,hour;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int button = 2;
const float shortTimePress = 2000; // 2 sec to stopwatch press time
const float longTimePress = 4000;//4 sec to alarm press time
int lastState = LOW; // gets previous state from the button
int currentState; // the current reading from the button
unsigned long initialPressedTime = 0;
unsigned long releasedTime = 0;
DateTime pressed,released;
long pressDuration;
int state = 0;
int stopwatchStatus=0; //stopwatch variables
int displaySeconds;
int stopwatchCounter=0;
int displayMinutes;
int displayMilli;
DateTime sStartTime ;
DateTime current;
DateTime previous;
int pressedTimeAlarm, releasedTimeAlarm, pressDurationAlarm, AlarmMinute ,AlarmHour;//alarm variables
bool AlarmState = 0;
void setup(void) {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
previous = rtc.now();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
pinMode(button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button), but, CHANGE);//interupt from the button
Serial.println("Enter the clock Year "); //gets the input for setting the data and time manually using serial monitor
while (Serial.available()==0){}
String enteredYear = Serial.readString();
Serial.println("Enter the clock Month ");
while (Serial.available()==0){}
String enteredMonth = Serial.readString();
Serial.println("Enter the clock Day ");
while (Serial.available()==0){}
String enteredDate = Serial.readString();
Serial.println("Enter the clock Hour ");
while (Serial.available()==0){}
String enteredHour = Serial.readString();
Serial.println("Enter the clock Minute ");
while (Serial.available()==0){}
String enteredMinute = Serial.readString();
Serial.println("Enter the clock Second ");
while (Serial.available()==0){}
String enteredSecond = Serial.readString();
Year=enteredYear.toInt(); //entered data into integer
Month=enteredMonth.toInt();
Date=enteredDate.toInt();
Hour = enteredHour.toInt();
Minute = enteredMinute.toInt();
Second=enteredSecond.toInt();
DateTime newTime = DateTime(Year,Month,Date,Hour,Minute,Second);
rtc.adjust(newTime); //adjust the RTC
}
void loop() {
if (state == 0)
{
current = rtc.now(); //gets the data from rtc sensor(in this case the adjusted values )
display.clearDisplay();
display.setCursor(5,5);//sets the position in the screen to display data
int hour = current.hour();// gets the hour value from rtc
display.print(hour);
display.setCursor(20,5);
display.print(":");
display.setCursor(25,5);
int minute = current.minute();
display.print(minute);
display.setCursor(35,5);
display.print(":");
display.setCursor(45,5);
int second = current.second();
display.print(second);
display.setCursor(5,55);
display.print(current.day());
display.print('/');
display.print(current.month());
display.print('/');
display.print(current.year());
display.setCursor(5,35);
second = current.second();
minute = current.minute();
hour = current.hour()+(minute/60);
int xSecond= 90 + 25*(sin(6*second*PI/180));//converts into polar coordinates
int ySecond= 30 - 25*(cos(6*second*PI/180));
int xMinute= 90 + 19*(sin(6*minute*PI/180));
int yMinute= 30 - 19*(cos(6*minute*PI/180));
int xHour= 90 + 11*(sin(30*hour*PI/180));
int yHour= 30 - 11*(cos(30*hour*PI/180));
display.drawCircle(90,30,30,SSD1306_WHITE); //draws the cirlce for clock
display.drawLine(90,33,xSecond,ySecond,SSD1306_WHITE);// draws the second hand in the display
display.drawLine(90,33,xMinute,yMinute,SSD1306_WHITE); // draws the minute hand in the display
display.drawLine(90,33,xHour,yHour,SSD1306_WHITE); // draws the hour hand in the display
for (int i =1; i<13; i++) //draws the lines in the cirlce to indicate the marking in the clock
{
int x1= 90 + 25*(sin(30*i*PI/180));
int y1= 30- 25*(cos(30*i*PI/180));
int x2= 90 + 30*(sin(30*i*PI/180));
int y2= 30 - 30*(cos(30*i*PI/180));
display.drawLine(x1,y1,x2,y2,SSD1306_WHITE);
}
display.setTextSize(0.1);
display.setTextColor(WHITE);
display.setCursor(85,7 ); //set and displays the values to indicate the markings of the clock
display.println("12");
display.setCursor(88, 47 );
display.println("6");
display.setCursor(108, 27 );
display.println("3");
display.setCursor(67, 27 );
display.println("9");
display.display();
}
else if (state == 1){
display.clearDisplay(); //clear display
display.setCursor(10,0);
display.setTextSize(1);
display.println("Stopwatch");
if (digitalRead(button)==LOW){ //reads and delays for 500ms
delay(500);
if (stopwatchStatus == 0){ //if the status is set to 0 set back to 1 and reset count to zero
stopwatchStatus = 1;
stopwatchCounter=0;
}
else if(stopwatchStatus==1){ //if status set to 1,set to 0
stopwatchStatus = 0;
}
}
if (stopwatchStatus == 1){
stopwatchCounter = stopwatchCounter+1; //counts the sec
delay(1000);
displayMinutes = stopwatchCounter/60 ; // just display the mins
displaySeconds = stopwatchCounter - (60*displayMinutes);// display the sec
Serial.print(displayMinutes);
Serial.print(" ");
Serial.println(displaySeconds);
display.setTextSize(2);
display.setCursor(40,20);
display.print(displayMinutes);
display.print(":");
display.print(displaySeconds);
}
else if (stopwatchStatus == 0){
display.setCursor(40,40);
display.print("end");
display.setTextSize(2);
display.setCursor(20,20);
display.print(displayMinutes);
display.print(":");
display.print(displaySeconds);
}
display.display();
}
else if (state == 2){ //alarm state
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.println("Alarm");
Serial.println(AlarmState);
if( AlarmState == 0 ){
if (digitalRead(button)==LOW){
Serial.println("check3");
delay(100);
if(digitalRead(button)==HIGH){
Serial.println("check 2");
AlarmMinute = AlarmMinute +1;}
if (AlarmMinute > 59){
AlarmMinute = 0;
}
}
}
else if (AlarmState == 1){ //increase the time
if (digitalRead(button)==LOW){
delay(200);
if(digitalRead(button)==HIGH){
AlarmHour = AlarmHour +1;}
if (AlarmHour > 23){
AlarmHour = 0;
}
}
}
Serial.print(AlarmHour);
Serial.print(" : ");
Serial.println(AlarmMinute); //display alarm vlaues
display.setCursor(40,35);
display.setTextSize(1);
display.print(AlarmHour);
display.print(" : ");
display.println(AlarmMinute);
display.display();
}
if (current.hour() == AlarmHour & current.minute() == AlarmMinute){
tone(9,262,1000); //buzzer pin 9
}
};
void but(){ //interupt for button determining by time taken to change from clock,alarm,stopwatch
if(state == 2 ){
if (digitalRead(button)==LOW){
pressedTimeAlarm=millis();
}
else if(digitalRead(button)==HIGH){
Serial.println("check ");
releasedTimeAlarm=millis();
pressDurationAlarm = releasedTimeAlarm - pressedTimeAlarm;
if (pressDurationAlarm > 1000){
AlarmState= !AlarmState;
}
}
}
if (digitalRead(button)==LOW){
initialPressedTime=millis();
}
else if(digitalRead(button)==HIGH){
releasedTime=millis();
pressDuration = releasedTime- initialPressedTime;
if (pressDuration > shortTimePress & pressDuration < longTimePress){
if (state != 1){
state =1;
stopwatchCounter=0;
stopwatchStatus=0;
}
else if (state == 1){
state = 0;
}
}
else if( pressDuration > longTimePress){
if (state != 2){
state =2;
}
else if (state == 2){
state = 0;
}
}
}
}