Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an Overflow in PID_RelayOutput.ino? #130

Open
rtek1000 opened this issue Jan 12, 2023 · 2 comments
Open

Is there an Overflow in PID_RelayOutput.ino? #130

rtek1000 opened this issue Jan 12, 2023 · 2 comments

Comments

@rtek1000
Copy link

rtek1000 commented Jan 12, 2023

Hello,

I noticed that the sum of the value in the windowStartTime variable occurs, but it seems to me that it is not restarted, so in a moment it seems to overflow.

PID_RelayOutput.ino:

  if (millis() - windowStartTime > WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }

Based on the Blink code, the variable can be set using the value of millis(), this way it must always have a value within the operating range:

  if (millis() - windowStartTime > WindowSize)
  { //time to shift the Relay Window
    windowStartTime = millis();
  }

Ref.:

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
   [...]
  }

https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

@rtek1000

This comment was marked as resolved.

rtek1000 added a commit to rtek1000/Arduino-PID-Library that referenced this issue Jan 12, 2023
@drf5n
Copy link

drf5n commented Mar 2, 2023

windowStartTime is the same size as the millis() result, so it will roll over at about the same time as millis() itself rolls over

The math for differencing unsigned longs works well for intervals.

Using the windowStartTime += windowSize; form makes it so any jitter in the window getting triggered is absorbed by the following window. The triggers will stay synchronized with every 5000ms for as long as the code runs. If you use the windowStartTime = millis(); form, if a window starts a little late, it pushes all the following windows later too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants