import machine
import time
# Pin definitions for the stepper motor
IN1 = machine.Pin(0, machine.Pin.OUT)
IN2 = machine.Pin(1, machine.Pin.OUT)
IN3 = machine.Pin(2, machine.Pin.OUT)
IN4 = machine.Pin(3, machine.Pin.OUT)
# Button setup
BUTTON_PIN = 14
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
# Timing control
last_press_time = 0
motor_on_time = 5
debounce_time = 3
# Stepper motor sequence
step_sequence = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1],
]
def step_motor(step):
IN1.value(step[0])
IN2.value(step[1])
IN3.value(step[2])
IN4.value(step[3])
def run_motor(duration):
start_time = time.time()
step_index = 0
while time.time() - start_time < duration:
step_motor(step_sequence[step_index])
step_index = (step_index + 1) % len(step_sequence)
time.sleep(0.002)
step_motor([0, 0, 0, 0])
while True:
if button.value() == 0:
current_time = time.time()
if current_time - last_press_time >= debounce_time:
last_press_time = current_time
print("Button pressed: running motor")
run_motor(motor_on_time)
print("Motor stopped")
time.sleep(0.1)