Photo-gate timer with CircuitPython: Difference between revisions
From ShawnReevesWiki
Jump to navigationJump to search
No edit summary |
|||
Line 10: | Line 10: | ||
The same text output to the Python REPL on the computer is output to the screen on the module, so outputting text requires only the built-in print() function. | The same text output to the Python REPL on the computer is output to the screen on the module, so outputting text requires only the built-in print() function. | ||
== | == Codes == | ||
===Timer=== | |||
<syntaxhighlight lang="python" line copy> | <syntaxhighlight lang="python" line copy> | ||
import time | import time | ||
Line 43: | Line 44: | ||
passed.append(event.key_number) | passed.append(event.key_number) | ||
print(times) | print(times) | ||
</syntaxhighlight> | |||
===Speeds=== | |||
<syntaxhighlight lang="python" line copy> | |||
import time | |||
import board | |||
import keypad | |||
print("Photogate interval meter") | |||
#Dialog to enter length of device, through matrix keyboard, in mm? | |||
gates = (board.D1, board.D2) | |||
#create empty dictionaries, keys will be key_number: | |||
startTimes = {} | |||
endTimes = {} | |||
speeds = {} | |||
#interval is time in seconds to debounce button. | |||
keys = keypad.Keys(gates, value_when_pressed=True, pull=True, interval=0.002) | |||
while True: | |||
event=keys.events.get() | |||
if event: | |||
#print(event) #debug | |||
#which gate was passed? event.key_number | |||
#Enter gate: event.pressed==True | |||
#Exit gate: event.pressed==False | |||
#if object is entering a gate: | |||
if event.pressed==True: | |||
startTimes[event.key_number] = time.monotonic() | |||
elif event.pressed==False: | |||
endTimes[event.key_number] = time.monotonic() | |||
if startTimes[event.key_number]>0.00: | |||
print(event.key_number, endTimes[event.key_number]-startTimes[event.key_number]) | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 06:36, 21 April 2025

See Photo-gate timer for a full discussion.
CircuitPython
CircuitPython is a flavor or port of MicroPython, by Adafruit Industries, to program any of hundreds of micro-controllers or single-board-computers with Python code. Changing the code on a board is simple: You copy a Python program file, and optionally libraries, to board that can be set to present itself to a computer as a USB drive. When you reset the board, it runs the file.
ESP32-S2 Reverse TFT Feather
The ESP32-S2 Reverse TFT Feather from Adafruit, https://www.adafruit.com/product/5345 , is an inexpensive development board with a WiFi capable radio.
The same text output to the Python REPL on the computer is output to the screen on the module, so outputting text requires only the built-in print() function.
Codes
Timer
import time
import board
import keypad
print("Photogate timer")
gates = (board.D1, board.D2)
#create empty list:
times = []
#track which gates have already been interrupted
passed = []
#pull is whether you use internal pullup/pulldown. False if you use an external switch.
#interval is time in seconds to debounce button.
keys = keypad.Keys(gates, value_when_pressed=True, pull=True, interval=0.002)
while True:
event=keys.events.get()
if event:
#print(event) #uncomment this to debug events
#which gate was passed? event.key_number
#if this is the first gate:
if 0 == len(times):
times.append(0.0)
startTime = time.monotonic()
#mark which gate has been passed
passed.append(event.key_number)
print(times)
#else check if a gate not yet passed:
elif 0 == passed.count(event.key_number):
#Add the current time to the list
times.append(time.monotonic() - startTime)
passed.append(event.key_number)
print(times)
Speeds
import time
import board
import keypad
print("Photogate interval meter")
#Dialog to enter length of device, through matrix keyboard, in mm?
gates = (board.D1, board.D2)
#create empty dictionaries, keys will be key_number:
startTimes = {}
endTimes = {}
speeds = {}
#interval is time in seconds to debounce button.
keys = keypad.Keys(gates, value_when_pressed=True, pull=True, interval=0.002)
while True:
event=keys.events.get()
if event:
#print(event) #debug
#which gate was passed? event.key_number
#Enter gate: event.pressed==True
#Exit gate: event.pressed==False
#if object is entering a gate:
if event.pressed==True:
startTimes[event.key_number] = time.monotonic()
elif event.pressed==False:
endTimes[event.key_number] = time.monotonic()
if startTimes[event.key_number]>0.00:
print(event.key_number, endTimes[event.key_number]-startTimes[event.key_number])