#include "fDigitsSegtPin2.h"
class StopWatch : public fDigitsSegtPin
{
public:
StopWatch(u8 vPf1, u8 vPf2, u8 vPf3, u8 vPf4, u8 vPf5, u8 vPf6, u8 vPf7, u8 vPf8, u8 vPf9, u8 vPf10, u8 vPf11, u8 vPf12) :
fDigitsSegtPin(vPf1, vPf2, vPf3, vPf4, vPf5, vPf6, vPf7, vPf8, vPf9, vPf10, vPf11, vPf12)
{
_working = false;
_curTime = 0;
_startTime = 0;
_deltaStopTime = 0;
}
void print()
{
if(_working)
{
int addTime = millis() - _startTime;
if(addTime >= 1000)
{
_startTime = _startTime + 1000;//в миллисекундах
_curTime = _curTime + 0.01;
if(_curTime - (int)(_curTime) > 0.59)
{
_curTime = _curTime + 1 - 0.60;
}
if(_curTime > 59)
{
_curTime -= 60;
}
}
fDigitsSegtPin::print(_curTime + 0.001);//1 секунда = 0.01 на циферблате
//поправка 0.001 введена из-за погрешности float
}
else
{
fDigitsSegtPin::print(_curTime + 0.001);//1 секунда = 0.01 на циферблате
//поправка 0.001 введена из-за погрешности float
}
}
void startStop()
{
if(_working)
{
_deltaStopTime = millis() - _startTime;
_working = false;
}
else
{
_startTime = millis() - _deltaStopTime;
_working = true;
}
}
void reset()
{
if(!_working)
{
_curTime = 0;
_deltaStopTime = 0;
}
}
private:
bool _working;
float _curTime;
unsigned long _startTime;
int _deltaStopTime;
};
template<typename T>
class Button
{
public:
Button(T* indicator, void (T::*action)(void), int pinB)
{
_indicator = indicator;
_action =action;
_lastButton = LOW;
_curButton = LOW;
_pinButton = pinB;
pinMode(pinB, INPUT_PULLUP);
}
~Button() = default;
void process()
{
_curButton = debounce();
if (_lastButton == HIGH && _curButton == LOW)
{
(_indicator->*_action)();
}
_lastButton = _curButton;
}
private:
T* _indicator;
void (T::*_action)(void);
int _lastButton;
int _curButton;
int _pinButton;
int debounce ()
{
int current = digitalRead(_pinButton);
if(_lastButton != current)
{
delay(5);
current = digitalRead(_pinButton);
}
return current;
}
};
StopWatch stopWatch(4, 3, 7, 2, 6, 11, 7, 10, 9, 5, 0, 8);
Button<StopWatch> startStopButton(&stopWatch, &StopWatch::startStop, 12);
Button<StopWatch> resetButton(&stopWatch, &StopWatch::reset, 13);
void setup() {
stopWatch.doPrint_firstZero = 1;
stopWatch.begin();
}
void loop()
{
startStopButton.process();
resetButton.process();
stopWatch.print();
}