#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
int state = 0;
long timestamp_button_pressed;
unsigned long countdownStart;
int focus_time = 0;
int rest_time = 0;
int potentiometer_value_s = 0;
int potentiometer_value_r = 0;
int FocusMinute = 0;
int RestMinute = 0;
int RemainFocusTime = 0;
int RemainRestTime = 0;
int MidMinute = 0;
char buffer[20];
int string_width;
const int buzzerPin = 4;
//===============================================
void ButtonMode()
{
if (millis() - timestamp_button_pressed > 200)
{
state = 1 - state; // Toggle between state 0 and state 1
timestamp_button_pressed = millis();
}
}
void ButtonStart()
{
if (millis() - timestamp_button_pressed > 200)
{
if (state == 2)
{
state = 0;
countdownStart = 0; // Reset the countdown start time(is it necessary? idk)
}
else
{
state = 2;
countdownStart = millis();
}
timestamp_button_pressed = millis();
}
}
void displayStateMessage() //Display current status
{
switch (state)
{
case 0:
string_width = u8g.getStrWidth("set FOCUS_TIME");
u8g.drawStr(64-string_width/2, 60, "set FOCUS_TIME");
break;
case 1:
string_width = u8g.getStrWidth("set REST_TIME");
u8g.drawStr(64-string_width/2, 60, "set REST_TIME");
break;
case 2:
if (0<=RemainFocusTime)
{
string_width = u8g.getStrWidth("REST_TIME");
u8g.drawStr(64-string_width/2, 60, "REST_TIME");
}
else
{
string_width = u8g.getStrWidth("FOCUS_TIME"); //change this to switch blabla
u8g.drawStr(64-string_width/2, 60, "FOCUS_TIME");
break;
}
}
}
void displayMain() //Display time set and countdown
{
switch(state)
{
case 0:
u8g.firstPage();
do
{
string_width = u8g.getStrWidth(buffer);
sprintf(buffer, "%d:00", focus_time);
u8g.drawStr(64 - string_width / 2, 10, buffer);
displayStateMessage();
}
while (u8g.nextPage());
potentiometer_value_s = map(analogRead(A0), 0, 1023, 5, 12);
focus_time = 5 * potentiometer_value_s;
break;
case 1:
u8g.firstPage();
do
{
string_width = u8g.getStrWidth(buffer);
sprintf(buffer, "%d:00", rest_time);
u8g.drawStr(64 - string_width / 2, 10, buffer);
displayStateMessage();
}
while (u8g.nextPage());
potentiometer_value_r = map(analogRead(A0), 0, 1023, 1, potentiometer_value_s);
rest_time = 5 * potentiometer_value_r;
break;
case 2:
{
FocusMinute=60000*focus_time, RemainFocusTime=countdownStart+FocusMinute-millis();
RestMinute=60000*rest_time, RemainRestTime=countdownStart+FocusMinute+MidMinute+RestMinute-millis();
MidMinute=5000; //Delay between Focus time and Rest time
//countdownSession=FocusMinute+MidMinute+RestMinute; //One Session: Focus time + Delay + Rest time
}
if (0<=RemainFocusTime)
{
u8g.firstPage();
do
{
string_width = u8g.getStrWidth(buffer);
sprintf(buffer, "%02d:%02d", (int)(RemainFocusTime / 60000), (int)((RemainFocusTime % 60000) / 1000));
u8g.drawStr(64 - string_width / 2, 10, buffer);
}
while (u8g.nextPage());
}
else if (0<=RemainFocusTime+MidMinute)
{
tone(buzzerPin, 1000);
delay(5000);
noTone(buzzerPin);
}
else if (0<=RemainRestTime)
{
u8g.firstPage();
do
{
string_width = u8g.getStrWidth(buffer);
sprintf(buffer, "%02d:%02d", (int)(RemainRestTime / 60000), (int)((RemainRestTime % 60000) / 1000));
u8g.drawStr(64 - string_width / 2, 10, buffer);
}
while (u8g.nextPage());
}
}
}
//===============================================
void setup()
{
u8g.setFont(u8g_font_8x13B);
u8g.setColorIndex(1);
pinMode(buzzerPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), ButtonMode, RISING);
attachInterrupt(digitalPinToInterrupt(3), ButtonStart, RISING);
timestamp_button_pressed = millis();
}
void loop()
{
displayMain();
}