It seems to me that the official manual is not fully clear, especially for new users.
To all: Could you publish here parts of code from real projects where ticks_add and/or ticks_diff are used?
Thanks.
A correct example of using utime.ticks_add and utime.ticks_diff
-
- Posts: 35
- Joined: Sat Oct 26, 2019 8:38 pm
-
- Posts: 67
- Joined: Fri Sep 04, 2020 9:27 am
- Location: Hanko, Finland
- Contact:
Re: A correct example of using utime.ticks_add and utime.ticks_diff
Example:
prints:
1108
2210
3312
4413
5515
6616
7718
8820
9921
0 (this when mqtt-message is seen)
Code is now here too: https://github.com/divergentti/kotiauto ... in/main.py
Code: Select all
def check_uptime(topic, message):
global mqtt_last_seen
mqtt_last_seen = utime.ticks_ms()
in main loop:
client.set_callback(check_uptime)
client.subscribe("$SYS/broker/bytes/#")
...
print(utime.ticks_diff(utime.ticks_ms(), mqtt_last_seen))
time.sleep(1.1)
1108
2210
3312
4413
5515
6616
7718
8820
9921
0 (this when mqtt-message is seen)
Code is now here too: https://github.com/divergentti/kotiauto ... in/main.py
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: A correct example of using utime.ticks_add and utime.ticks_diff
Or at the REPL issue:
Then wait five seconds and issue
and you'll see the number of ms you waited (about 5000).
Code: Select all
import time
t = time.ticks_ms()
Code: Select all
print(time.ticks_diff(time.ticks_ms(), t))
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: A correct example of using utime.ticks_add and utime.ticks_diff
I agree it's not the most obvious explanation of what's going on. Before I raise a bug, can you explain what you think was specifically unclear or how you'd improve it?ihornehrutsa wrote: ↑Sat Oct 10, 2020 6:49 pmIt seems to me that the official manual is not fully clear, especially for new users.
(i.e. is the issue that it's confusing why you need ticks_add and ticks_diff in the first place, or how to actually use it)
Ref: http://docs.micropython.org/en/latest/l ... ticks_diff
-
- Posts: 35
- Joined: Sat Oct 26, 2019 8:38 pm
Re: A correct example of using utime.ticks_add and utime.ticks_diff
Thanks, Divergentti
But if your MQTT client lasts ~ 12 days(not rarely for IoT), you will get negative values unexpected for newbies.
Thanks, Peter pythoncoder
But if your code
lasts ~ 12 days, you also may get negative values unexpected for newbies.
If time.ticks_us() used
we may get negative values every 18 minutes.
Counter overflow happens.
Only one example from the http://docs.micropython.org/en/latest/l ... ticks_diff
is strictly correct, others are not fully.
The question is not closed...
Thanks to all.
But if your MQTT client lasts ~ 12 days(not rarely for IoT), you will get negative values unexpected for newbies.
Thanks, Peter pythoncoder
But if your code
Code: Select all
import time
t = time.ticks_ms()
time.sleep(5) # or do something
print(time.ticks_diff(time.ticks_ms(), t))
If time.ticks_us() used
Code: Select all
import time
t = time.ticks_us()
time.sleep(5) # or do something
print(time.ticks_diff(time.ticks_us(), t))
Counter overflow happens.
Only one example from the http://docs.micropython.org/en/latest/l ... ticks_diff
Code: Select all
# This code snippet is not optimized
now = time.ticks_ms()
scheduled_time = task.scheduled_time()
if ticks_diff(scheduled_time, now) > 0:
print("Too early, let's nap")
sleep_ms(ticks_diff(scheduled_time, now))
task.run()
elif ticks_diff(scheduled_time, now) == 0:
print("Right at time!")
task.run()
elif ticks_diff(scheduled_time, now) < 0:
print("Oops, running late, tell task to run faster!")
task.run(run_faster=true)
The question is not closed...
Thanks to all.