How to do a simple subtraction #14380
hanslammerts
started this conversation in
General
Replies: 2 comments
-
Use |
Beta Was this translation helpful? Give feedback.
0 replies
-
Also, you can't reliably use simple subtraction on ticks counters. These roll over at around 2**30. Use the time.ticks_diff(ticks1, ticks2) function instead. Here's what I think your code should look like: from machine import Pin
import time
def heartbeat(currentms):
setup = False
print(currentms)
def loop():
loopTimer = time.ticks_ms
maxLoopTime = 0
maxLoopTime = max(
maxLoopTime, (time.ticks_diff(time.ticks_ms(), loopTimer))
)
loopTimer = time.ticks_ms()
currentMillis = time.ticks_ms()
heartbeat(currentMillis)
if __name__ == "__main__":
loop() (though impossible to tell since indentation was lost. Please read the Welcome (Please read before posting) entry on how to post code so it's readable.) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm sorry.
Just started out with micropython (for the Py Pico) and one of the simplest things fail.
Please have a look at the following snippet:
`
from machine import Pin
import utime
def heartbeat(currentms):
setup = False
print(currentms)
def loop():
loopTimer = utime.ticks_ms
maxLoopTime = 0
maxLoopTime = max(maxLoopTime, (utime.ticks_ms - loopTimer))
loopTimer = utime.ticks_ms
currentMillis = utime.ticks_ms
heartbeat(currentMillis)
if __name__ == '__main__':
loop()
`
The error I get:
Operator "-" not supported for types "() -> int" and "() -> int"
How do I subtract loopTimer from utime.ticks_ms ?
Also, the code backticks don't seem to work....
Indentation disappears....
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions