// Add libraries
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//
// setup OLED display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
//#endif
//
// Setup RTC
RTC_DS1307 RTC;
char monthString[37] = {"JanFebMarAprMayJunJulAugSepOctNovDec"};
int monthIndex[122] = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33};
const int button = 2;
long prev = 0;
long times = 0;
int y = 0;
int z = 0;
int s_w = 0;
int w = 0;
int a_c = 0;
int a = 0;
int as = 0;
int ah = 0;
int am = 0;
void mine()
{
if (s_w == 1)
{
w++;
}
else if (a_c == 1)
{
a++;
}
else
{
y = 1;
}
delay(500);
}
void setup() {
pinMode(button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button), mine, LOW);
Serial.begin(9600);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
// Use I2C Scanner to check the address, if necessary change the 0x3C in the line below
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
// init done
// set font size
display.setTextSize(1); // small font size
display.setTextColor(WHITE);
display.clearDisplay();
}
void loop() {
if (y == 1)
{
z++;
y = 0;
if ((z == 2) && ((millis() - prev) < 2000))
{
Serial.println("Stop Watch");
z = 0;
s_w = 1;
stop_watch();
s_w = 0;
}
else if ((z == 2) && ((millis() - prev) > 2000))
{
Serial.println("Alarm");
a_c = 1;
alarms();
a_c = 0;
z = 0;
}
prev = millis();
}
Clock();
}
// **************** End Main Loop *****************
void printDigits(int digits)
{
// utility function for digital clock display: prints preceding colon and leading 0
display.print(":");
if (digits < 10)
display.print('0');
display.print(digits);
}
void Clock()
{
//***** RTC **********
DateTime now = RTC.now();
display.setCursor(0, 2);
display.print(now.hour(), DEC);
printDigits(now.minute());
printDigits(now.second());
display.setCursor(0, 30);
display.print("A=");
display.print(ah, DEC);
display.print(":");
display.print(am, DEC);
display.print(":");
display.print(as, DEC);
if ((now.hour() == ah) && (now.minute() == am)&&(now.second() == 0))
{
tone(6, 1000);
delay(2000);
noTone(6);
}
//
// Now draw the clock face
int radius = 30;
display.drawCircle(display.width() / 2 + 20, display.height() / 2, 2, WHITE);
//draw clock
for ( int i = 0; i < 360; i = i + 30 ) {
float angle = i ;
angle = (angle / 57.29577951) ;
int x1 = (64 + (sin(angle) * radius));
int y1 = (32 - (cos(angle) * radius));
int x2 = (64 + (sin(angle) * (radius - 5)));
int y2 = (32 - (cos(angle) * (radius - 5)));
display.drawLine(x1 + 20, y1, x2 + 20, y2, WHITE);
}
float angle = now.second() * 6 ;
angle = (angle / 57.29577951) ;
int x2 = (64 + (sin(angle) * (radius)));
int y2 = (32 - (cos(angle) * (radius)));
display.drawLine(84, 32, x2 + 20, y2, WHITE);
angle = now.minute() * 6 ;
angle = (angle / 57.29577951) ;
x2 = (64 + (sin(angle) * (radius - 3)));
y2 = (32 - (cos(angle) * (radius - 3)));
display.drawLine(84, 32, x2 + 20, y2, WHITE);
angle = now.hour() * 30 + int((now.minute() / 12) * 6 );
angle = (angle / 57.29577951) ;
x2 = (64 + (sin(angle) * (radius - 11)));
y2 = (32 - (cos(angle) * (radius - 11)));
display.drawLine(84, 32, x2 + 20, y2, WHITE);
// update display with all data
display.display();
delay(100);
display.clearDisplay();
}
void alarms()
{
long pr = millis();
//a
while (1)
{
if (millis() - pr < 20000)
{
if (a > 0)
{
am++;
if (am==60)
{am=0;}
a = 0;
}
}
else if (millis() - pr < 40000)
{
if (a > 0)
{
ah++;
if (ah==60)
{ah=0;}
a = 0;
}
}
else if (millis() - pr < 60000)
{
break;
}
display.setCursor(0, 2);
display.print(ah, DEC);
display.print(":");
display.print(am, DEC);
display.print(":");
display.print(as, DEC);
display.display();
delay(100);
display.clearDisplay();
}
}
void stop_watch()
{
int h = 0;
int m = 0;
int s = 0;
long prev_1 = millis();
int f = 0;
while (1)
{
if (w > 0)
{
f++;
w = 0;
long prev_1 = millis();
}
Serial.println(f);
if (f == 1)
{
s = (millis() - prev_1) / 1000;
if (s == 60)
{
s = 0;
m++;
prev_1 = millis();
}
if (m == 60)
{
m = 0;
h++;
}
}
else if (f == 2)
{
s = s;
}
else if (f == 3)
{
break;
}
display.setCursor(0, 2);
display.print(h, DEC);
display.print(":");
display.print(m, DEC);
display.print(":");
display.print(s, DEC);
display.display();
delay(100);
display.clearDisplay();
}
}