from machine import Pin, Timer
import time
# Pin definitions
microswitch_pin = Pin(1, Pin.IN, Pin.PULL_UP) # Adjust the pin number as needed
solenoid_pin = Pin(2, Pin.OUT) # Adjust the pin number as needed
# Variables
dwell_time = 1000 # Dwell time in milliseconds (adjustable)
# Timer for dwell time
dwell_timer = Timer(-1)
# Function to turn off the solenoid
def turn_off_solenoid(timer):
solenoid_pin.off()
# Main loop
while True:
if not microswitch_pin.value(): # Microswitch pressed
solenoid_pin.on()
dwell_timer.init(period=dwell_time, mode=Timer.ONE_SHOT, callback=turn_off_solenoid)
# Wait until the microswitch is released to avoid re-triggering
while not microswitch_pin.value():
time.sleep_ms(10)
time.sleep_ms(200) # Debounce delay
time.sleep_ms(50) # Short delay to avoid button bounce