//Stopwatch - double click the button to start stopwatch. Click same button to stop stopwatch. It will close shortly after.
//Temperature and Alarm - long press the button (>1 second) and then enter selection in serial
//Alarm password is 20003
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneButton.h>
#include <Wire.h>
#include <SPI.h>
#include "RTClib.h"
RTC_DS1307 rtc;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, 4);
//clock coordinates
byte ClockMiddleX = 35;
byte ClockMiddleY = 32;
//Other variables
int check;
int time;
int hour;
int minute;
int second;
int RTC;
DateTime userTime;
// Variables used for poisitoning
int x;
int y;
int x1;
int y1;
// Variables used for Loops
int k = 1;
int z = 0;
int j = 0;
int q = 0;
// Variables for stopwatch
OneButton button(10, true);
int count = 0;
int select = 0;
int wait;
int timing;
int buttonPin = 10;
unsigned long ms;
unsigned long w = 0;
//Variables for alarm system
int alarmOn = 0;
int alarmOff = 0;
int password;
//Variables for temperature
int pinR = 5;
int pinG = 3;
int pinB = 4;
int temp = 0;
void setup () {
//temperature
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
digitalWrite(pinB, 0);
digitalWrite(pinG, 0);
digitalWrite(pinR, 0);
//stopwatch
wait = 1;
Serial.begin(9600);
Serial.setTimeout(10000);
button.attachClick(singleclick); // depending on clicks button will run function
button.attachDoubleClick(doubleclick);
button.attachLongPressStop(longclick);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
Serial.println("-- Initialising Clock --");
Serial.println("Please input the hour: ");
while (k != 0) {
if (Serial.available() > 0) { //assigns the hour and checks correct range
check = Serial.parseInt();
if (check >= 0 && check <= 24) {
hour = check;
Serial.print("Hour: ");
Serial.println(hour);
if (hour > 12) { //converts 24 hour time to 12 hour time.
hour = hour - 12;
}
k = 0;
z = 1;
}
else {
Serial.println("Invalid: out of range!");
}
}
}
while (z != 0) { //assigns the minutes and checks the range
Serial.println("Please input the minutes: ");
check = Serial.parseInt();
if (check >= 0 && check <= 59) {
minute = check;
Serial.print("Minutes: ");
Serial.println(minute);
z = 0;
q = 1; //allows for next step to take place
}
else {
Serial.println("Invalid: out of range!");
}
}
while (q != 0) { //assigns the seconds and checks the range
Serial.println("Please input the seconds: ");
check = Serial.parseInt();
if (check >= 0 && check <= 59) {
second = check;
Serial.print("Seconds: ");
Serial.println(second);
q = 0;
w = 1; //allows for next step to take place
}
else {
Serial.println("Invalid: out of range!");
}
}
while ( w != 0) {
Serial.print("Starting Time: ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
Serial.println("Press button to start stopwatch.");
w = 0;
rtc.adjust(DateTime(2022, 7, 24, hour, minute, second));
}
}
void clockFace(void) {
// places 12 hour marks around the clock
for (int w = 0; w < 12; w++) {
x = ((32 * sin(PI - (2 * PI) / 12 * w)) * 0.98) + ClockMiddleX; //outside coord
x1 = (28 * sin(PI - (2 * PI) / 12 * w)) + ClockMiddleX; //inside coord
y = ((32 * cos(PI - (2 * PI) / 12 * w)) * 0.98) + ClockMiddleY; //outside coord
y1 = (28 * cos(PI - (2 * PI) / 12 * w)) + ClockMiddleY; //inside coord
display.drawLine(x1, y1, x, y, WHITE);
}
//Creates space every 90* around circle
for (int k = 0; k < 4; k++) {
x = (25.5 * sin(PI - (2 * PI) / 4 * k)) + ClockMiddleX;
y = (25.5 * cos(PI - (2 * PI) / 4 * k)) + ClockMiddleY;
display.drawCircle(x, y, 6, BLACK);
display.fillCircle(x, y, 5, BLACK);
}
//12th, 3rd, 6th, 9th hour around clock
//change ClockMiddleX to move clock on OLED
display.setTextColor(WHITE);
display.setCursor(ClockMiddleX - 6, 0);
display.println("12");
display.setCursor(ClockMiddleX - 33, ClockMiddleY - 3);
display.println("9");
display.setCursor(ClockMiddleX - 2, 1.8 * ClockMiddleY);
display.println("6");
display.setCursor(ClockMiddleX + 27, ClockMiddleY - 3);
display.println("3");
// clock centre
display.drawCircle(ClockMiddleX, ClockMiddleY, 2, WHITE);
display.fillCircle(ClockMiddleX, ClockMiddleY, 2, WHITE);
}
void sketchSecond(int second) {
x = (24 * sin(PI - (2 * PI) / 60 * second)) + ClockMiddleX;
y = (24 * cos(PI - (2 * PI) / 60 * second)) + ClockMiddleY;
display.drawCircle(x, y, 1, WHITE);
display.fillCircle(x, y, 1, WHITE);
}
void sketchHour(int hour, int minute) {
x = (18 * sin(PI - (2 * PI) / 12 * hour - (2 * PI) / 720 * minute)) + ClockMiddleX; //left side
x1 = (18 * sin(PI - (2 * PI) / 12 * hour - (2 * PI) / 720 * minute)) + ClockMiddleX + 1; //right side
y = (18 * cos(PI - (2 * PI) / 12 * hour - (2 * PI) / 720 * minute)) + ClockMiddleY; //left side
y1 = (18 * cos(PI - (2 * PI) / 12 * hour - (2 * PI) / 720 * minute)) + ClockMiddleY + 1; //right side
display.drawLine(ClockMiddleX, ClockMiddleY, x, y, WHITE); //left side
display.drawLine(ClockMiddleX + 1, ClockMiddleY + 1, x1, y1, WHITE); //right side
}
void sketchMinute(int minute) {
x = (24 * sin(PI - (2 * PI) / 60 * minute)) + ClockMiddleX;
y = (24 * cos(PI - (2 * PI) / 60 * minute)) + ClockMiddleY;
display.drawLine(ClockMiddleX, ClockMiddleY, x, y, WHITE);
}
void singleclick() {
display.clearDisplay();
count = 1;
while (digitalRead(10) == HIGH && count == 1) {
display.drawCircle(69, 32, 50, BLACK);
display.fillCircle(69, 32, 50, BLACK);
display.setTextSize(1);
display.setCursor(20, 20);
display.setTextColor(WHITE);
display.println("Double press");
display.print(" to start...");
display.display();
display.clearDisplay();
}
}
void doubleclick() {
count = 2;
select = 1;
Serial.println("Press button to stop stopwatch. ");
while (digitalRead(10) == HIGH && count == 2) {
if (digitalRead(10) == HIGH) {
display.clearDisplay();
ms = millis();
int minute = 0;
while (digitalRead(10) == HIGH) {
unsigned long currentms = millis();
timing = (currentms - ms);
unsigned long millisecond = (timing % 1000); //Milliseconds
unsigned long second = (timing / 1000) % 60; //Seconds
unsigned long minute = (timing / 60000) % 60; //Minutes
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 25);
display.print(minute);
display.print(':');
display.print(second);
display.print(':');
display.print(millisecond);
display.display();
display.clearDisplay();
delay(10);
}
}
delay(5000);
break;
}
}
void longclick() {
Serial.println("1 - Alarm.");
Serial.println("2 - Temperature.");
int frog = Serial.parseInt();
if (frog == 1) {
alarm();
}
if (frog == 2) {
temperature();
}
}
void alarm() {
Serial.println("Alarm System Activated!");
alarmOn++;
while (alarmOn == 1) {
display.drawCircle(69, 32, 50, BLACK);
display.fillCircle(69, 32, 50, BLACK);
display.setTextSize(1);
display.setCursor(24, 20);
display.setTextColor(WHITE);
display.println("Alarm System");
display.println(" Activated!");
display.display();
display.clearDisplay();
delay(1000);
Serial.print("Enter password to disarm:");
password = Serial.parseInt();
if (password != 20003) {
Serial.println(password);
Serial.println("Incorrect! Alarm Activated!");
tone(A9, 290, 1000000);
}
if (password == 20003) {
noTone(A9);
Serial.println();
Serial.println("Welcome Back!");
break;
}
}
}
void temperature() {
while (temp == 0 ) {
//display.drawCircle(69, 32, 50, BLACK);
//display.fillCircle(69, 32, 50, BLACK);
display.setTextSize(2);
display.setCursor(35, 20);
display.setTextColor(WHITE);
display.display();
display.clearDisplay();
int analogValue = analogRead(A11);
int NUM = 3950;
int celsius = 1 / (log(1 / (1023. / analogValue - 1)) / NUM + 1.0 / 298.15) - 273.15;
display.println("Temp:");
display.setCursor(35, 48);
display.print(celsius);
if (celsius < 10) {
digitalWrite(pinB, 200);
digitalWrite(pinG, 0);
digitalWrite(pinR, 0);
}
if (celsius >= 10 && celsius < 25) {
digitalWrite(pinB, 0);
digitalWrite(pinG, 200);
digitalWrite(pinR, 0);
}
if (celsius >= 25 && celsius < 81) {
digitalWrite(pinB, 0);
digitalWrite(pinG, 0);
digitalWrite(pinR, 200);
}
}
}
void loop () {
button.tick(); // check the status of the button
userTime = rtc.now();
display.clearDisplay();
clockFace();
sketchSecond(userTime.second());
sketchMinute(userTime.minute());
sketchHour(userTime.hour(), userTime.minute());
//position for digital time
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(80, 29);
//print time in hh:mm:ss format
display.print(String(userTime.hour(), DEC) + ":" );
display.print(String(userTime.minute(), DEC) + ":" );
display.print(String(userTime.second(), DEC));
display.display();
display.clearDisplay();
delay(10);
}