#include <GyverTM1637.h>
#include <EncButton.h>
#include <microDS18B20.h>
#define BUZZ_PIN 10
#define RELE_PIN 2
#define RELAY_LEVEL_ON LOW
#define RELAY_LEVEL_OFF HIGH
#define CLK_TM_PIN 12
#define DIO_TM_PIN 11
#define CLK_ENC_PIN 5
#define DT_ENC_PIN 4
#define SW_ENC_PIN 3
#define DS18B20_PIN 9
GyverTM1637 disp(CLK_TM_PIN, DIO_TM_PIN);
EncButton eb(CLK_ENC_PIN, DT_ENC_PIN, SW_ENC_PIN); // CLK, DT, SW
MicroDS18B20<DS18B20_PIN> sensor;
uint32_t timerMsec = 60000;
uint32_t maxTimerValue = 5999000;
bool startTimer = 0;
bool timerActiveStatus = 0;
bool movePoint = 0;
bool soundPlay = 0;
void setup() {
Serial.begin(115200); // Any baud rate should work
Serial.println("Hello Arduino\n");
disp.clear();
disp.brightness(7);
eb.setBtnLevel(LOW);
eb.setClickTimeout(1000);
eb.setDebTimeout(50);
eb.setHoldTimeout(600);
eb.setStepTimeout(200);
eb.setEncReverse(0);
eb.setEncType(EB_STEP4_LOW);
eb.setFastTimeout(30);
eb.counter = 0;
// put your setup code here, to run once:
tone(BUZZ_PIN, 2500, 1000);
pinMode(RELE_PIN, OUTPUT);
pinMode(DS18B20_PIN, INPUT_PULLUP);
digitalWrite(RELE_PIN, RELAY_LEVEL_OFF);
}
void loop() {
eb.tick();
encHandler();
pointTick();
dspTick();
timerHendler();
timerActive();
tempTick();
}
void dspTick() {
static uint32_t tm;
uint32_t cm = millis();
if (cm - tm >= 1000) {
tm = cm;
showTimeInDsp(timerMsec);
}
}
void encHandler() {
int step = 1000;
if (eb.fast()) step = 15000;
if (eb.click()) Serial.println("click");
if (eb.left()) Serial.println("left");
if (eb.right()) Serial.println("right");
if (eb.left() && timerMsec <= maxTimerValue) {
timerMsec += step;
Serial.println(timerMsec);
}
if (eb.right() && timerMsec >= 0) {
timerMsec -= step;
if ( timerMsec < 0) timerMsec = 0;
Serial.println(timerMsec);
}
if (eb.click() && !startTimer ) {
startTimer = 1;
digitalWrite(RELE_PIN, RELAY_LEVEL_OFF);
}
if (eb.click() && startTimer && timerActiveStatus) {
startTimer = 0;
soundPlay = 0;
movePoint = 0;
timerActiveStatus = 0;
digitalWrite(RELE_PIN, RELAY_LEVEL_OFF);
Serial.println(eb.click() && !startTimer);
}
}
void showTimeInDsp(int timeMs) {
byte h = timerMsec / 1000 / 60 ;
byte m = timerMsec / 1000 % 60;
// disp.point(1);
disp.displayClockTwist(h, m, 35);
}
void pointTick() {
static uint32_t tm;
static bool flag = 0;
uint32_t cm = millis();
if (movePoint) {
if (cm - tm >= 1000) {
tm = cm;
flag = !flag;
}
disp.point(flag);
} else {
disp.point(1);
}
}
void timerHendler() {
static uint32_t tm;
uint32_t cm = millis();
if (startTimer) {
movePoint = 1;
if (cm -tm >= 1000) {
tm = cm;
timerActiveStatus = 1;
if (timerMsec > 0) timerMsec -= 1000;
if (timerMsec < 0) timerMsec = 0;
}
if (timerMsec == 0) {
startTimer = 0;
movePoint = 0;
soundPlay = 1;
}
}
}
void timerActive() {
static uint32_t tm;
uint32_t cm = millis();
bool s = 0;
if (cm -tm >= 1000) {
tm = cm;
s = ! s;
}
digitalWrite(RELE_PIN, RELAY_LEVEL_ON);
if (s && soundPlay) tone(BUZZ_PIN, 2500, 500);
}
float getTempDS18B20() {
float t = 0.0;
if (sensor.online()) {
sensor.requestTemp();
if (sensor.readTemp()) t = sensor.getTemp();
} else {
Serial.println("DS18B20 Offline!");
}
return t;
}
void tempTick() {
static uint32_t tm;
uint32_t cm = millis();
if (cm -tm >= 1000) {
tm = cm;
Serial.println(getTempDS18B20());
}
}