forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Open
Labels
Milestone
Description
Firmware
Adafruit CircuitPython 6.2.0 on 2021-04-05; Raspberry Pi Pico with rp2040
code.py
Note: GP16
and GP17
are directly connected with a wire.
from board import GP16, GP17
from digitalio import DigitalInOut, Direction
from pulseio import PulseIn
from time import sleep
trigger = DigitalInOut(GP16)
trigger.direction = Direction.OUTPUT
trigger.value = False
echo = PulseIn(GP17, maxlen=3, idle_state=False)
def trigger_pulse(seconds: float):
print(f"Triggering ~{seconds:.6}s pulse…")
trigger.value = True
sleep(seconds)
trigger.value = False
print("Done:", [echo[i] for i in range(len(echo))])
sleep(1.0) # giving myself enough time to connect to the serial
for i in range(10):
trigger_pulse(0.001000)
Output
The documentation says:
When there is no more room (len() ==
maxlen
) the oldest pulse length is removed to make room.
But the list keeps growing despite the maxlen=3
:
code.py output:
Triggering ~0.001s pulse…
Done: [816]
Triggering ~0.001s pulse…
Done: [816, 183]
Triggering ~0.001s pulse…
Done: [816, 183, 237]
Triggering ~0.001s pulse…
Done: [816, 183, 237, 816]
Triggering ~0.001s pulse…
Done: [816, 183, 237, 816, 183]
Triggering ~0.001s pulse…
Done: [816, 183, 237, 816, 183, 237]
Triggering ~0.001s pulse…
Done: [816, 183, 237, 816, 183, 237, 816]
Triggering ~0.001s pulse…
Done: [816, 183, 237, 816, 183, 237, 816, 183]
Triggering ~0.001s pulse…
Done: [816, 183, 237, 816, 183, 237, 816, 183, 237]
Triggering ~0.001s pulse…
Done: [816, 183, 237, 816, 183, 237, 816, 183, 237, 816]
Code done running.