#include <U8glib.h>//OLED display libary
U8GLIB_SSD1306_128X64 u8g;//enable OLED display
#include "RTClib.h"
const int buttonPin = 2;
bool mode=0;
float start;
RTC_DS1307 rtc;
volatile unsigned long DebounceTimer;//time since last button input to debounce signal
volatile int ButtonPressed=0;
volatile unsigned int delayTime = 100;
void setup() {
attachInterrupt(digitalPinToInterrupt(2), [] {if (ButtonPressed+= (millis() - DebounceTimer) >= (delayTime )) DebounceTimer = millis();}, RISING);// interrupt for button
Serial.begin(9600);
rtc.begin();
DateTime now= rtc.now();
Serial.println("Please enter hour");
while (Serial.available() == 0) {
}
int thour=Serial.parseInt();
delay(100);
Serial.println("Please enter minute");
while (Serial.available() <=1) {
}
int tmin=Serial.parseInt();
rtc.adjust(DateTime(now.year(), now.month(), now.day(),thour,tmin,0));
}
void loop(){
u8g.setFont(u8g_font_7x13);
while(ButtonPressed==0){//repeat until button is pushed
DateTime now= rtc.now();
u8g.firstPage();
do{
u8g.setColorIndex(1);
u8g.setPrintPos(47, 16);
if (now.hour()<10)u8g.setPrintPos(54, 16);
u8g.print(now.hour());
u8g.print(":");
if (now.minute()<10) u8g.print('0');
u8g.print(now.minute());
u8g.drawCircle(64,42,20);
u8g.drawLine(64, 42,8*sin(now.hour()*PI/6)+64, 42-8*cos(now.hour()*PI/6));
u8g.drawLine(64, 42,12*sin(now.minute()*PI/30)+64, 42-12*cos(now.minute()*PI/30));
u8g.drawLine(64, 42,14*sin(now.second()*PI/30)+64, 42-14*cos(now.second()*PI/30));
}while(u8g.nextPage());
}
ButtonPressed=0;
u8g.firstPage();
do{
u8g.setColorIndex(1);
u8g.setPrintPos(47, 16);
u8g.print("0.00");
}while(u8g.nextPage());
while(ButtonPressed==0){}
ButtonPressed=0;
start=millis();//set stopwatch start time
while(ButtonPressed==0){
u8g.firstPage();
do{
u8g.setColorIndex(1);
u8g.setPrintPos(47, 16);
u8g.print((millis()-start)/1000);
}while(u8g.nextPage());
}
ButtonPressed=0;
while(ButtonPressed==0){}
ButtonPressed=0;
}