//Further copy
#include "U8glib.h"
#include <DHT.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_SSD1306.h>
#define ECHO_PIN 6
#define TRIG_PIN 7
U8GLIB_SSD1306_128X64 u8g; // create an instant of OLED display
RTC_DS1307 rtc;
volatile int device_state = 0, button_time;
volatile unsigned long start, button_down, button_up;
int interruptPin1 = 2, interruptPin2 = 3;
unsigned long current, HH, MM;
String elapsed, final_time, time_setting = "hours";
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
float celsius;
void setup() {
Serial.begin(115200);
Serial.println("clock");
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, INPUT);
pinMode(A8, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin1), state_checker, RISING);
attachInterrupt(digitalPinToInterrupt(interruptPin2), trigger, FALLING);
u8g.setFont(u8g_font_7x13); // set the font for text
//u8g.setColorIndex(1); set the display colour can be done outside picture loop
if (! rtc.begin()) {
//Serial.println("Couldn't find RTC");
//Serial.flush();
abort();
}
}
void loop() {
while (device_state == 0) {
time_mode_with_temperature();
/*
if (0 < celsius < 15) {
digitalWrite(11, HIGH);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
}
if (15 < celsius < 25) {
digitalWrite(11, LOW);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
}
if (25 < celsius < 30 ) {
digitalWrite(11, LOW);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
}
*/
delay(50);
if (device_state != 0) {
break;
}
}
while (device_state == 1) {
time_mode();
delay(50);
if (device_state != 1) {
break;
}
}
while (device_state == 2) {
stopwatch_logo();
delay(50);
if (device_state != 2) {
break;
}
}
while (device_state == 3) {
final_time = stop_watch(start);
delay(50);
if (device_state != 3) {
break;
}
}
while (device_state == 4) {
stop_display(final_time);
//Serial.println("Printing final time");
//Serial.println(final_time);
delay(50);
if (device_state != 4) {
break;
}
}
while (device_state == 5) {
Serial.println("--------");
Serial.println(button_time);
Serial.println("--------");
alarm();
delay(50);
}
}
void stop_display(String final_time) {
Serial.println("state=");
Serial.println(device_state);
u8g.firstPage();
unsigned long final_times = final_time.toInt();
do {
if (device_state != 4) {
break;
}
unsigned long durMS = (final_times % 1000); //Milliseconds
unsigned long durSS = (final_times / 1000) % 60; //Seconds
unsigned long durMM = (final_times / (60000)) % 60; //Minutes
unsigned long durHH = (final_times / (3600000)); //Hours
if (device_state != 4) {
break;
}
durHH = durHH % 24;
String timer_string = String(durHH) + ":" + String(durMM) + ":" + String(durSS) + ":" + String(durMS);
u8g.setColorIndex(1);
char time[16];
timer_string.toCharArray(time, 16);
u8g.drawStr(0, 50, time);
u8g.setColorIndex(0);
if (device_state != 4) {
break;
}
} while ( u8g.nextPage() );
};
void time_mode() {
DateTime now = rtc.now();
Serial.println("state=");
Serial.println(device_state);
u8g.firstPage(); //marks the beginning of the picture loop.
do {
u8g.setColorIndex(1); // set the colour to white
//u8g.drawFrame(4, 10, 128, 20);
String hour = String(now.hour());
String minute = String(now.minute());
String second = String(now.second());
String concat = hour + ":" + minute + ":" + second;
char time[16];
concat.toCharArray(time, 16);
u8g.drawStr(0, 50, time);
int center_y = 28, center_x = 90, radius = 28;
u8g.drawCircle(center_x, center_y, radius);
//----------------
int hour_angle, minute_angle, hour_int, minute_int;
hour_int = now.hour(); if (hour_int > 12) {
hour_int = hour_int - 12;
}
Serial.println(hour_int);
Serial.println(minute_int);
minute_int = now.minute();
for (int i = 1; i <= hour_int; i++) {
hour_angle = hour_angle + 30;
}
u8g.drawLine(center_x, center_y, 0, 60); // use this to draw hour and minute lines
for (int i = 1; i <= minute_int; i++) {
minute_angle = minute_angle + 6;
}
Serial.println(hour_angle);
Serial.println(minute_angle);
float hour_x, hour_y, minute_x, minute_y;
//-------------------
u8g.setColorIndex(0); //change the colour back to black
hour_angle=0;minute_angle=0;
} while ( u8g.nextPage() ); //marks the end of the body of the picture loop
}
void time_mode_with_temperature() {
DateTime now = rtc.now();
Serial.println("state=");
Serial.println(device_state);
u8g.firstPage(); //marks the beginning of the picture loop.
do {
u8g.setColorIndex(1); // set the colour to white
//u8g.drawFrame(4, 10, 128, 20);
String hour = String(now.hour());
String minute = String(now.minute());
String second = String(now.second());
String concat = hour + ":" + minute + ":" + second;
char time[16];
concat.toCharArray(time, 16);
u8g.drawStr(0, 50, time);
/* u8g.drawCircle(90, 28, 28);
u8g.drawLine(45, 60, 50, 80); // use this to draw hour and minute lines */
int analogValue = analogRead(A8);
celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
String celsius_string = String(celsius);
char celsiusarray[16];
celsius_string.toCharArray(celsiusarray, 16);
if (round(celsius) < 15) {
digitalWrite(11, HIGH);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
}
else if (15 <= round(celsius) <= 25) {
digitalWrite(8, HIGH);
digitalWrite(11, LOW);
digitalWrite(9, LOW);
}
else if (25 < round(celsius) ) {
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
digitalWrite(11, LOW);
}
u8g.drawStr(0, 30, celsiusarray);
u8g.setColorIndex(0); //change the colour back to black
} while ( u8g.nextPage() ); //marks the end of the body of the picture loop
}
void state_checker() {
//Serial.println("resetting button state");
button_time = 0;
button_down = millis();
//Serial.println("Interrupt has run");
state_switcher();
}
void state_switcher() {
switch (device_state) {
case 0:
device_state = 1;
/*Serial.println("Device state=Stopwatch logo");
Serial.println("----------");*/
break;
case 1:
device_state = 2;
/*Serial.println("Device state=stopwatch Start");
Serial.println("----------");*/
break;
case 2:
device_state = 3;
start = millis();
/*Serial.println("Device state=stopwatch End");
Serial.println("----------");*/
break;
case 3:
device_state = 4;
/*Serial.println("Device state=alarm");
Serial.println("----------");*/
break;
case 4:
device_state = 5;
/*Serial.println("Device state=alarm");
Serial.println("----------");*/
break;
case 5:
device_state = 0;
/*Serial.println("Device state=alarm");
Serial.println("----------");*/
break;
}
}
String stop_watch(unsigned long start) {
DateTime now = rtc.now();
char time[16];
Serial.println("state=");
Serial.println(device_state);
u8g.firstPage(); //marks the beginning of the picture loop.
do {
if (device_state != 3) {
break;
}
u8g.setColorIndex(1);
current = millis(); elapsed = String(current - start);
unsigned long durMS = (elapsed.toInt() % 1000); //Milliseconds
unsigned long durSS = (elapsed.toInt() / 1000) % 60; //Seconds
unsigned long durMM = (elapsed.toInt() / (60000)) % 60; //Minutes
unsigned long durHH = (elapsed.toInt() / (3600000)); //Hours
if (device_state != 3) {
break;
}
durHH = durHH % 24;
String timer_string = String(durHH) + ":" + String(durMM) + ":" + String(durSS) + ":" + String(durMS);
timer_string.toCharArray(time, 16);
//Serial.println(elapsed);
u8g.drawStr(0, 50, time);
if (device_state != 3) {
break;
}
u8g.setColorIndex(0); //change the colour back to black
} while ( u8g.nextPage() ); //marks the end of the body of the picture loop
//}
return elapsed;
}
void stopwatch_logo() {
u8g.firstPage();
Serial.println("state=");
Serial.println(device_state);
do {
if (device_state != 2) {
break;
}
u8g.setColorIndex(1);
if (device_state != 2) {
break;
}
u8g.drawStr(0, 50, "Stopwatch");
if (device_state != 2) {
break;
}
u8g.setColorIndex(0); //change the colour back to black
if (device_state != 2) {
break;
}
} while ( u8g.nextPage() ); //marks the end of the body of the picture loop
// u8g.setColorIndex(1);
};
void alarm() {
if (digitalRead(10) == HIGH && time_setting == "hours") {
Serial.println("Incremented hour");
if (HH >= 13) {
HH = 0;
}
HH++;
}
if (digitalRead(10) == HIGH && button_time >= 2000 && time_setting == "hours") {
time_setting = "minutes";
Serial.println("Now adjusting minutes");
}
if (digitalRead(10) == HIGH && button_time <= 2000 && time_setting == "minutes") {
MM++;
Serial.println("Incremented minutes");
if (MM == 61) {
MM = 0;
HH++;
if (HH == 13) {
HH = 0;
}
}
}
alarm_display(HH, MM);
//Serial.println(current-start);
}
void alarm_display( unsigned long durHH, unsigned long durMM ) {
u8g.firstPage();
do {
u8g.setColorIndex(1);
if (device_state != 5) {
break;
}
String timer_string = String(durHH) + ":" + String(durMM) ;
char time[16];
timer_string.toCharArray(time, 16);
u8g.drawStr(0, 50, time);
if (device_state != 5) {
break;
}
u8g.setColorIndex(0); //change the colour back to black
} while ( u8g.nextPage() ); //marks the end of the body of the picture loop
//}
}
void trigger() {
button_up = millis();
button_time = button_up - button_down;
/*Serial.print("Button time=");
Serial.print(button_time);
Serial.println("------");
/*Serial.println("Button time");
Serial.println(button_time);
Serial.println("-----"); */
}