I’m testing the time functions and I’m seeing what I think is strange behavior where the time.ticks_diff() value is going backwards.
Here’s my code:
import time
startTime = time.ticks_ms()
for _ in range(20):
deltaTime = time.ticks_diff(time.ticks_ms(), startTime)
print(deltaTime/1000)
time.sleep_ms(100);
And here’s the result:
MicroPython v1.25.0-preview.beta06 on 2025-02-17; SparkFun XRP Controller with RP2350
Type “help()” for more information.
0.0
0.1
0.2
0.3
0.4
0.6
0.7
0.8
0.5 <<<<
0.9
1.2
1.3
1.4
1.5
1.0 <<<<<
1.8
1.9
1.1 <<<<<
1.6
1.7
I’m a complete beginner with Python and microPython, so if I’m misunderstanding something here, I’m ready to learn.
-Alan
I just tested on a Raspberry PI Pico and it works as I expected it to work:
MicroPython v1.25.0 on 2025-04-15; Raspberry Pi Pico with RP2040
Type "help()" for more information.
%Run -c $EDITOR_CONTENT
0.0
0.1
0.201
0.302
0.403
0.504
0.605
0.7059999
0.807
0.908
1.009
1.11
1.211
1.312
1.413
1.514
1.615
1.716
1.817
1.918
I think what you are seeing is some out of order prints. I’m not sure if you were running attached with a cable or Bluetooth. But, with fast turn around prints there are a few spots in the system that can get overloaded and cause the order to get out of sequence.
I would try keeping the numbers in an array and then print out the array at the end.
Oh, that’s very interesting and correct, it’s a buffering issue.
If I do just a loop:
for i in range(100) :
print(i)
everything prints fine.
If I add a sleep:
for i in range(100) :
print(i)
time.sleep_ms(100)
things get printed out of order!
I’m using bluetooth for communications.
I’ll keep an eye out for this so I don’t get confused in the future.
Thank you for the information.
-Alan