//initializes the libraries that are required
#include "RTClib.h"
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
//the screen dimensions are specified
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_ADDR 0x3C
//appropriate variables are defined below
char userChoiceMem = '1';
char userChoice;
byte clockCentreX = 63;
byte clockCentreY = 31;
int hour, second, minute;
int x;
int y;
int x1;
int y1;
//stopwatch variables
int timerState = 0; // 0 = clock, 1 = timer, 2 = stopwatch running
String timeCount;
int timeStart;
int timeCurrent;
int secondRunning;
//objects are initialized so that functions from adafruit and RTC can be obtained
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);
RTC_DS1307 rtc;
DateTime currentTime;
void setup() {
Serial.begin(9600);
pinMode(10, INPUT_PULLUP);
//check rtc connection
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
//display is connected and started
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
//user prompt is displayed
Serial.println("Press 1: Digital Clock");
Serial.println("Press 2: Analog Clock");
}
void loop() {
//user choice is read from the serial monitor
userChoice = Serial.read();
//if the userchoice is 1 (digital time), it stores into the user choice memory, this applies for analog clock(2)
if (userChoice == '1')
{
//timer state is set to 0
timerState = 0;
userChoiceMem = '1';
}
else if (userChoice == '2')
{
//timer state is set to 0
timerState = 0;
userChoiceMem = '2';
}
//depending on the userchoice, digital clock and analog clock are displayed on the screen
if (userChoiceMem == '1')
{
//clears the display
display.clearDisplay();
//current time is obtained from the RTC
currentTime = rtc.now();
//heading for digital time is printed
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Digital time: ");
//position for the digital time is set
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 32);
String hour = String(currentTime.hour(), DEC);
String minute = String(currentTime.minute(), DEC);
String second = String(currentTime.second(), DEC);
String timeDisplay = hour + ":" + minute + ":" + second;
display.print(timeDisplay);
display.display();
delay(1000);
}
if (userChoiceMem == '2')
{
//current time is obtained from RTC
currentTime = rtc.now();
//hour, minute and second is obtained individually
hour = currentTime.hour();
minute = currentTime.minute();
second = currentTime.second();
//clears the display
display.clearDisplay();
//the border of the clock is drawn on the display screen
display.drawCircle(63, 31, 31, WHITE);
//functions are called to draw second, minute and hour hand
drawSecond(second);
drawMinute(minute);
drawHour(hour, minute);
//the display screen is updated
display.display();
//given a delay of 1 second
delay(1000);
}
//timer state validation is occured to change the state
if (timerState == 1) {
timer(timeCount);
} else if (timerState == 2) {
DateTime now = rtc.now();
//converting hour, minute, second to string
String strHourStart = String(now.hour(), DEC);
String strMinuteStart = String(now.minute(), DEC);
String strSecondStart = String(now.second(), DEC);
//converting the string start hour to integer
int hourStart = strHourStart.toInt();
//running function is called
timeCount = running(timeStart);
}
//when the button is pressed - theres few delays to check if the button is held and not just pressed
if (digitalRead(10) == LOW) {
userChoiceMem = 0;
delay(200);
if (digitalRead(10) == LOW) {
delay(500);
//when timer state = 0, it prints 00:00:00
if (timerState == 0) {
timerState = 1;
timeCount = "00:00:00";
//if the timer state = 1 or 2, reset it to 0 so it loops through the 3 states (0,1,2)
} else if (timerState == 1 || timerState == 2) {
timerState = 0;
}
}
//after a short delay, if the timer state = 1, change it to 2 and start the timer and display it on the OLED
delay(500);
if (timerState == 1) {
timerState = 2;
DateTime now = rtc.now();
String strHourStart = String(now.hour(), DEC);
String strMinuteStart = String(now.minute(), DEC);
String strSecondStart = String(now.second(), DEC);
int hourStart = strHourStart.toInt();
int minuteStart = strMinuteStart.toInt();
int secondStart = strSecondStart.toInt();
//setting start time is set
timeStart = ((hourStart * 60) + minuteStart) * 60 + secondStart;
String timeDisplay = strHourStart + ":" + strHourStart + ":" + strHourStart;
//when the timer state = 2, go back to 1 or 0 and if 0 depending on the user Choice print digital/analog clock
} else if (timerState == 2) {
timerState = 1;
} else {
timerState = 0;
if (userChoiceMem == '1') {
display.clearDisplay();
currentTime = rtc.now();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Digital time: ");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 32);
String hour = String(currentTime.hour(), DEC);
String minute = String(currentTime.minute(), DEC);
String second = String(currentTime.second(), DEC);
String timeDisplay = hour + ":" + minute + ":" + second;
display.print(timeDisplay);
display.display();
delay(1000);
}
else if (userChoiceMem == '2') {
currentTime = rtc.now();
hour = currentTime.hour();
minute = currentTime.minute();
second = currentTime.second();
display.clearDisplay();
display.drawCircle(63, 31, 31, WHITE);
drawSecond(second);
drawMinute(minute);
drawHour(hour, minute);
display.display();
delay(1000);
}
}
}
}
//function that draws the minute hand with the argument of minutes
void drawMinute(int minute)
{
//the formula for x and y position of the minute hand is given below using trig and centre positions of initial x and inital y
y = (24*cos(PI-(2*PI)/60*minute)) + clockCentreY;
x = (24*sin(PI-(2*PI)/60*minute)) + clockCentreX;
display.drawLine(clockCentreX,clockCentreY,x,y,WHITE);
}
//function that draws the hour hand with the argument of hour and minutes
void drawHour(int hour, int minute)
{
//the formula for x and y position of the hour hand is given below using trig and centre positions of initial x and inital y
y = (18*cos(PI-(2*PI)/12*hour-(2*PI)/720*minute))+clockCentreY;
x =(18*sin(PI-(2*PI)/12*hour-(2*PI)/720*minute))+clockCentreX;
//the hour hand is made a bit thicker to display that its hour hand
y1=(18*cos(PI-(2*PI)/12*hour-(2*PI)/720*minute))+clockCentreY;
x1=(18*sin(PI-(2*PI)/12*hour-(2*PI)/720*minute))+clockCentreX;
display.drawLine(clockCentreX,clockCentreY,x,y,WHITE);
display.drawLine(clockCentreX+1,clockCentreY+1,x1,y1,WHITE);
}
//function that draws the second hand with the argument of seconds
void drawSecond(int second)
{
//the formula for x and y position of the second hand (which is a circle) is given below using trig and centre positions of initial x and inital y
y= (24*cos(PI-(2*PI)/60*second))+clockCentreY;
x =(24*sin(PI-(2*PI)/60*second))+clockCentreX;
display.drawCircle(x, y, 2, WHITE);
}
//function for timer to print the 00:00:00
void timer (String timerValue) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 25);
display.print(timerValue);
display.display();
}
//function that starts the stopwatch and formats it accordingly
String running (int timeStart) {
DateTime now = rtc.now();
//converting hour, minute, second to string
String strHourCurrent = String(now.hour(), DEC);
String strMinuteCurrent = String(now.minute(), DEC);
String strSecondCurrent = String(now.second(), DEC);
//getting the current time in hour, minute and second
int hourCurrent = strHourCurrent.toInt();
int minuteCurrent = strMinuteCurrent.toInt();
int secondCurrent = strSecondCurrent.toInt();
timeCurrent = ((hourCurrent * 60) + minuteCurrent) * 60 + secondCurrent;
secondRunning = timeCurrent - timeStart;
//converting integer time to appropriate parameter as minutes cant go above 59 etc.
int intMinute = secondRunning / 60;
int intSecond = secondRunning - intMinute * 60;
int intHour = intMinute / 60;
intMinute = intMinute - intHour * 60;
String hour = String(intHour);
if (hour == "0" || hour == "1" || hour == "2" || hour == "3" || hour == "4" || hour == "5" || hour == "6" || hour == "7" || hour == "8" || hour == "9") {
hour = "0" + hour;
}
String minute = String(intMinute);
if (minute == "0" || minute == "1" || minute == "2" || minute == "3" || minute == "4" || minute == "5" || minute == "6" || minute == "7" || minute == "8" || minute == "9") {
minute = "0" + minute;
}
String second = String(intSecond);
if (second == "0" || second == "1" || second == "2" || second == "3" || second == "4" || second == "5" || second == "6" || second == "7" || second == "8" || second == "9" ) {
second = "0" + second;
}
String timerValue = hour + ":" + minute + ":" + second;
//prints the time and updates the stopwatch
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 25);
display.print(timerValue);
display.display();
return (timerValue);
}
Loading
ssd1306
ssd1306