Correct use of "time.ticks_diff(ticks1, ticks2)"..?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Correct use of "time.ticks_diff(ticks1, ticks2)"..?

Post by FeK9 » Wed Sep 21, 2022 9:36 am

Good Day All...

I've been trying to substitute time.sleep(2) with the code shown below... For the life of me
I can not get it to work, what am I doing wrong or not seeing?

Code: Select all

import time

start = time.ticks_ms()

while time.ticks_diff(time.ticks_ms(), start) < 2000:
    print('Loop')

print('End Loop')
I'm using the firmware by 'russhughes' with fast MicroPython driver for ST7789 display, for the Pi Pico W... I also
tried stock (stable) firmware... The same...

Code: Select all

MicroPython v1.19.1-292-g59e3348c1-dirty on 2022-09-04; Raspberry Pi Pico W with RP2040
Type "help()" for more information.

MicroPython v1.19.1 on 2022-09-08; Raspberry Pi Pico W with RP2040
Type "help()" for more information.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Correct use of "time.ticks_diff(ticks1, ticks2)"..?

Post by scruss » Wed Sep 21, 2022 3:16 pm

You're creating so much output that the terminal (Thonny?) takes time to catch up. Your loop is running for two seconds, but takes nearly a minute to output all of the data. Don't print so much. Use a small sleep, too:

Code: Select all

import time

start = time.ticks_ms()

while time.ticks_diff(time.ticks_ms(), start) < 2000:
    print('Loop', time.ticks_diff(time.ticks_ms(), start))
    time.sleep_ms(50)

print('End Loop')
This runs in much better time, and shows you the remaining time

FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Re: Correct use of "time.ticks_diff(ticks1, ticks2)"..?

Post by FeK9 » Wed Sep 21, 2022 4:56 pm

Thanks for the reply

Using print('Loop') in the while loop might have not been the best idea, I've tried to simplify the question.

What I've been seeing in another piece of code I'm working on is what I might call time creep...

If you use time.sleep(2) you will get a two second pause a that point, the code above it in the main loop also
take time to process... In all you get a 2 second pause plus time it takes to process the other code...

My aim is to have the 'other code processing time' be part of the 2 second
pause... E.g. 1.8 + 200 ms = 2 seconds not 2 Sec + 200 ms...

Hope that make sense, the explanation..? :|
scruss wrote:
Wed Sep 21, 2022 3:16 pm
You're creating so much output that the terminal (Thonny?) takes time to catch up. Your loop is running for two seconds, but takes nearly a minute to output all of the data. Don't print so much. Use a small sleep, too:

Code: Select all

import time

start = time.ticks_ms()

while time.ticks_diff(time.ticks_ms(), start) < 2000:
    print('Loop', time.ticks_diff(time.ticks_ms(), start))
    time.sleep_ms(50)

print('End Loop')
This runs in much better time, and shows you the remaining time

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Correct use of "time.ticks_diff(ticks1, ticks2)"..?

Post by scruss » Wed Sep 21, 2022 11:02 pm

FeK9 wrote:
Wed Sep 21, 2022 4:56 pm
My aim is to have the 'other code processing time' be part of the 2 second
pause... E.g. 1.8 + 200 ms = 2 seconds not 2 Sec + 200 ms...
And that's exactly what you're getting. Put this at the end of your code:

Code: Select all

print('Elapsed / ms:', time.ticks_diff(time.ticks_ms(), start))
and you'll see this result:

Code: Select all

Loop
Loop
...
Loop
End Loop
Elapsed / ms: 2000
If you're running this in Thonny, it may take several minutes. If you're running it in minicom, say, it might take 20 seconds. Serial I/O takes time, and it's beyond the Raspberry Pi Pico's control how long it takes for these characters to be received. Serial output is asynchronous.

Your process is I/O bound. As I said before, the way to fix it is produce less output.

FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Re: Correct use of "time.ticks_diff(ticks1, ticks2)"..?

Post by FeK9 » Thu Sep 22, 2022 7:01 am

@scruss

Thank's

U've point me in the right direction, penny finally dropped...

Below is the while loop one liner that I'll use as a substitute for time.sleep(2), I have tinker experiment with it a bit already and I get my 2 seconds regardless of the code above it...

Will test some more, but it's looking ok so far...

;)

Code: Select all

import time

start = time.ticks_ms()
#
#
# Other code that hopefully take less then 2 Seconds
#
#
while time.ticks_diff(time.ticks_ms(), start) < 2000: pass # pass = nop()
    #print('Loop', time.ticks_diff(time.ticks_ms(), start))
    #time.sleep_ms(50)

print('End Loop', time.ticks_diff(time.ticks_ms(), start))

Post Reply