#define button_ok 5
#define button_up 6
#define button_down 4
#define CLK 2
#define DIO 3
#define value_timer 100
#define tonePin 10
#include <EncButton.h>
#include <TimerMs.h>
#include "GyverTM1637.h"
// -
//#include <EEPROM.h>
Button ok(button_ok, INPUT, HIGH);
Button up(button_up, INPUT, HIGH);
Button down(button_down, INPUT, HIGH);
TimerMs tmr(value_timer, 0, 0);
GyverTM1637 disp(CLK, DIO);
int clicks = 0;
int8_t min = 0, sec = 0, msec = 0, t_min = 0, t_sec = 0, t_msec, ts_min, ts_sec, mode = 1;
bool start = false, displ = false;
void setup() {
disp.clear();
disp.brightness(5);
tmr.setPeriodMode();
disp.displayByte(_S, _E, _C, _empty);
delay(1000);
disp.point(true);
pinMode(tonePin, OUTPUT);
Serial.begin(9600);
}
void loop() {
tick();
if (up.hold() && start == false) {
mode += 1;
displ = true;
//EEPROM.update(0, ts_min);
//EEPROM.update(1, ts_sec);
}
if (down.hold() && start == false) {
mode -= 1;
displ = true;
//EEPROM.update(0, ts_min);
//EEPROM.update(1, ts_sec);
}
switch (mode) {
case 0:
mode = 3;
break;
case 1:
stopwatch();
break;
case 2:
timer();
break;
case 3:
clicker();
break;
case 4:
mode = 1;
break;
}
}
void tick() {
ok.tick();
up.tick();
down.tick();
}
void stopwatch() {
disp.displayClock(min, sec);
if (displ == true) {
min = 0;
sec = 0;
msec = 0;
disp.point(false);
disp.displayByte(_S, _E, _C, _empty);
delay(1000);
disp.point(true);
displ = false;
}
if (start == true) {
if (tmr.tick()) {
msec += 1;
}
if (msec == 10) {
msec = 0;
sec += 1;
}
if (sec == 60) {
sec = 0;
min += 1;
}
}
if (ok.hold() && start == false) {
min = 0;
sec = 0;
msec = 0;
}
if (ok.click() || (ok.releaseStep() && start == true)) {
if (start == true) {
start = false;
tmr.stop();
} else {
start = true;
//if (tmr.active() == "resume") {
// tmr.resume();
//} else tmr.start();
tmr.start();
}
}
}
void timer() {
disp.displayClock(t_min, t_sec);
if (displ == true) {
start = false;
t_msec = 0;
// -
//EEPROM.get(0, ts_min);
//EEPROM.get(1, ts_sec);
if ((ts_min > 0 || ts_min < 99) && (ts_sec > 0 || ts_sec < 59)) {
t_min = 2;
t_sec = 0;
} else {
t_min = ts_min;
t_sec = ts_sec;
}
disp.point(false);
disp.displayByte(_t, _i, _n, _e);
delay(1000);
disp.point(true);
displ = false;
}
if (start == true) {
if (tmr.tick()) {
t_msec -= 1;
//Serial.print(t_min);
//Serial.print(":");
//Serial.print(t_sec);
//Serial.print(":");
//Serial.println(t_msec);
//Serial.print("tmr");
//Serial.println(tmr.active());
}
if (t_msec < 0) {
t_msec = 9;
t_sec -= 1;
}
if (t_sec < 0) {
t_sec = 59;
t_min -= 1;
}
if (t_min == 0 && t_sec == 0 && t_msec == 0) {
start = false;
byte a = 5; // количество повторений
while (a--) {
ok.tick();
if (ok.holding() || ok.click()) {
noTone(tonePin);
break;
}
disp.point(false);
tone(tonePin, 147);
disp.displayByte(_E, _N, _D, _empty);
delay(1000);
ok.tick();
if (ok.holding() || ok.click()) {
noTone(tonePin);
break;
}
noTone(tonePin);
delay(1000);
ok.tick();
if (ok.holding() || ok.click()) {
noTone(tonePin);
break;
}
tone(tonePin, 147);
disp.point(true);
disp.displayByte(_empty, _empty, _empty, _empty);
delay(1000);
ok.tick();
if (ok.holding() || ok.click()) {
noTone(tonePin);
break;
}
noTone(tonePin);
delay(1000);
}
}
}
if (up.click() && start == false) {
tmr.force();
t_msec = 0;
t_sec += 1;
if (t_sec > 59) {
t_sec = 0;
t_min += 1;
}
if (t_min > 99) t_min = 99;
}
if (up.hasClicks(2) && start == false) { //ster()
tmr.force();
t_msec = 0;
t_sec += 10;
if (t_sec > 59) {
t_sec = 0;
t_min += 1;
}
if (t_min > 99) t_min = 99;
}
if (up.hasClicks(3) && start == false) {
tmr.force();
t_msec = 0;
t_min += 1;
if (t_min > 99) t_min = 99;
}
if (down.click() && start == false) {
tmr.force();
t_msec = 0;
t_sec -= 1;
if (t_sec < 0) {
t_sec = 59;
t_min -= 1;
}
if (t_min < 0) t_min = 0;
}
if (down.hasClicks(2) && start == false) { //step
tmr.force();
t_msec = 0;
t_sec -= 10;
if (t_sec < 0) {
t_sec = 59;
t_min -= 1;
}
if (t_min < 0) t_min = 0;
}
if (down.hasClicks(3) && start == false) {
tmr.force();
t_msec = 0;
t_min -= 1;
if (t_min < 0) t_min = 0;
}
if (ok.click() || ok.hold()) {
if (t_min == 0 && t_sec == 0 && t_msec == 0) {
disp.point(false);
disp.displayByte(_dash, _dash, _dash, _dash);
delay(100);
disp.point(true);
} else {
if (start == true) {
start = false;
tmr.stop();
//Serial.print("tmr");
//Serial.println(tmr.active());
} else {
start = true;
//if (tmr.active() == false) { // "resume"
// tmr.resume();
//} else {
//ts_min = t_min;
//ts_sec = t_sec;
//EEPROM.update(0, ts_min);
//EEPROM.update(1, ts_sec);
// -
//EEPROM.update(0, t_min);
//EEPROM.update(1, t_sec);
tmr.start();
//}
}
}
}
}
void clicker() {
disp.displayInt(clicks);
if (displ == true) {
disp.point(false);
disp.displayByte(_C, _L, _i, _C);
delay(1000);
displ = false;
}
if (ok.click() && clicks < 9999) {
clicks += 1;
}
if (ok.hold()) {
clicks = 0;
}
}