# Modules
from machine import Pin
from time import sleep, sleep_ms
# Essential pins
dir_pin = Pin(4, Pin.OUT) # Direction Pin
step_pin = Pin(16, Pin.OUT) # Step pin
button = Pin(15, Pin.IN, Pin.PULL_UP)
# Direction variable
direction = 1 # 1 initially
dir_pin.value(direction)
while True:
# Button condition
if button.value() == 0:
direction = 1 - direction
dir_pin.value(direction)
print("Direction changed to", "Forward" if direction else "Backward")
sleep(0.2) # Debounce delay
# Controlling the speed of the stepper motor using timed delays
# These numbers are random
step_pin.on()
sleep_ms(20)
step_pin.off()
sleep_ms(50)
step_pin.on()
sleep_ms(100)
step_pin.off()