#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(13, 11, 8, 9, 10); //SPI Display Connection (add in arduino ide)
//state 0: focus_time set, state 1: rest_time set, state 2: count down start in order
int state=0;
long timestamp_button_pressed;
unsigned long countdown_start;
int focus_time=0;
int rest_time=0;
char buffer[20];
int string_width;
void button_mode()
{
if (millis()-timestamp_button_pressed>700)
{
if (state==0)
{
state=1;
timestamp_button_pressed=millis();
}
else if (state==1)
{
state=0;
timestamp_button_pressed=millis();
}
}
}
void button_start()
{
if (millis()-timestamp_button_pressed>700)
{
(state==2)? state=0:state=2;
}
}
void setup()
{
u8g.setFont(u8g_font_8x13B);
u8g.setColorIndex(1);
attachInterrupt(digitalPinToInterrupt(2), button_mode, RISING);
attachInterrupt(digitalPinToInterrupt(3), button_start, RISING);
timestamp_button_pressed=millis();
}
void loop()
{
if (state==0)
{
u8g.firstPage();
do
{
string_width=u8g.getStrWidth(buffer);
dtostrf(5*focus_time, 1, 0, buffer);
sprintf(buffer, "%s%s", buffer, ":00");
u8g.drawStr(64-string_width/2, 10, buffer); //focus_time display
}
while ( u8g.nextPage() );
focus_time=map(analogRead(A0), 0, 1023, 5, 12);
}
if (state==1)
{
u8g.firstPage();
do
{
string_width=u8g.getStrWidth(buffer);
dtostrf(5*rest_time, 1, 0, buffer);
sprintf(buffer, "%s%s", buffer, ":00");
u8g.drawStr(64-string_width/2, 10, buffer);
}
while ( u8g.nextPage() );
rest_time=map(analogRead(A0), 0, 1023, 1, focus_time);
}
}