diff --git a/AutoPID.cpp b/AutoPID.cpp index 64a5f49..cc819ee 100644 --- a/AutoPID.cpp +++ b/AutoPID.cpp @@ -92,7 +92,9 @@ void AutoPID::setIntegral(double integral){ void AutoPIDRelay::run() { AutoPID::run(); - while ((millis() - _lastPulseTime) > _pulseWidth) _lastPulseTime += _pulseWidth; + const unsigned long currentTime = millis(); + while (currentTime > (_pulseWidth + _lastPulseTime)) + _lastPulseTime += _pulseWidth; *_relayState = ((millis() - _lastPulseTime) < (_pulseValue * _pulseWidth)); } diff --git a/AutoPID.h b/AutoPID.h index efe85f2..1262406 100644 --- a/AutoPID.h +++ b/AutoPID.h @@ -46,7 +46,7 @@ class AutoPIDRelay : public AutoPID { public: AutoPIDRelay(double *input, double *setpoint, bool *relayState, double pulseWidth, double Kp, double Ki, double Kd) - : AutoPID(input, setpoint, &_pulseValue, 0, 1.0, Kp, Ki, Kd) { + : AutoPID(input, setpoint, &_pulseValue, 0, 1.0, Kp, Ki, Kd), _lastPulseTime{millis()} { _relayState = relayState; _pulseWidth = pulseWidth; };