#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
RTC_DS1307 rtc; //create an instant of RTC
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DateTime current;
unsigned long time;
long int seconds=0;
long int mills=0;
int minutes=0;
int hours=0;
int set=0;
int reset=0;
const byte Button = 2;
void setup(){
// initialize OLED display with address 0x3C for 128x64
pinMode(Button, INPUT);
attachInterrupt(digitalPinToInterrupt(2), stopwatch, CHANGE);
Serial.begin(115200);
//check rtc connection
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
}
void loop() {
// put your main code here, to run repeatedly:
oled.clearDisplay();
stopwatch();
oled.display();
}
void display(DateTime now){
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(17, 22); // position to display
oled.print(now.hour(), DEC);//get current hour
oled.print(':');
oled.print(now.minute(), DEC); //get current minute
oled.print(':');
oled.print(now.second(), DEC); //get current second
oled.println();
oled.display(); // show on OLED
oled.clearDisplay();
}
/////////////////////digital clock
void digitalClock(){
current = rtc.now(); //get the current time
display(current);
delay(1000);
}
//////////////////////stopwatch starts
void setStopwatch(){
seconds++; //counts seconds from 0
delay(1000); //a delay of 1000 milliseconds is given for each second count
if (seconds>59)
{
seconds=0; //whenever second is greater than 59 than second is made equal to
minutes++; // zero and a minute is counted for it
}
if (minutes>59)
{hours++;
minutes=0;
}
if(hours>23)
{
hours=0;
}
}
void stopwatch(){
if(digitalRead(Button) && !reset) // check if button is pressed /held "HIGH" and reset is false
{
time = millis();
reset = true;
}
if(!digitalRead(Button) && reset) //Check if button is released "LOW" and reset is true
{
reset = false;
}
if(digitalRead(Button) && (millis() - time > 3000)) // if button is HIGH and 3 seconds has passed, LED on.
{
setStopwatch();
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(17, 22); //sets the cursor to 0th column and 1st row,numbering starts from 0
{
if(hours<10) //suppose 4
{
oled.print("0"); //LCD first prints 0 and stopwatch shows 0
oled.print(hours); //LCD then prints 4. So value printed is 04 stopwatch shows 04
}
else
{
oled.print(hours);
}
}
oled.print(":");
{
if(minutes<10)
{
oled.print("0");
oled.print(minutes);
oled.print(":");
}
else
{
oled.print(minutes);
oled.print(":");
}
}
{
if(seconds<10)
{
oled.print("0");
oled.print(seconds);
}
else
{
oled.print(seconds);
}
}
}
}