# John Kristofer A. Go
# BSCPE-4A
from rp2 import PIO, StateMachine, asm_pio
from machine import Pin
import time
@asm_pio(set_init=(PIO.OUT_LOW,))
def stepper_motor_program():
wrap_target()
set(pins, 1) [31] # High step pulse
set(pins, 0) [31] # Low step pulse
wrap()
DIR_PIN = Pin(18, Pin.OUT)
STEP_PIN = Pin(19, Pin.OUT)
BUTTON_PIN = Pin(1, Pin.IN, Pin.PULL_UP)
LED_PIN = Pin(2, Pin.OUT)
# StateMachine to control stepper motor
sm = StateMachine(0, stepper_motor_program, freq=10000, set_base=STEP_PIN)
def blink_led():
LED_PIN.toggle()
time.sleep(0.25) # Delay for 0.25 seconds (2 Hz blinking)
# Main loop
while True:
if BUTTON_PIN.value() == 0: # Button pressed
DIR_PIN.on() # Set motor to turn clockwise
LED_PIN.on()
if not sm.active():
sm.active(1)
time.sleep(0.1) # Wait for 0.1 seconds (10 Hz step rate)
else:
if sm.active(): # Stop motor if it is running
sm.active(0)
blink_led() # Blink LED at 2 Hz