I have a micro stepper connected to a drv8833 and a feather express nrf52840. I have tested the circuitpython code to just control the stepper motor below (inside the if statement) and by itself it works fine. I then added the bluetooth code so I could use a button to initiate the stepper and embedded the stepper motor code into the bluetooth button press if statement for the 1 button. I can hear some kind of response from the stepper when I press the 1 button, but it does not move (it just makes the initial noise before it starts moving but never actually moves.)
I don't get any errors. I know the stepper movement code works and I know the bluetooth code works, but when I combine them, it doesn't work. Could anyone pleases help me understand why this does not work? Thanks.
Code: Select all
import time
import board
import digitalio
from adafruit_motor import stepper
DELAY = 0.01
STEPS = 200
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket
ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)
coils = (
digitalio.DigitalInOut(board.D9), # A1
digitalio.DigitalInOut(board.D10), # A2
digitalio.DigitalInOut(board.D11), # B1
digitalio.DigitalInOut(board.D12), # B2
)
while True:
ble.start_advertising(advertisement)
while not ble.connected:
pass
while ble.connected:
if uart.in_waiting:
packet = Packet.from_stream(uart)
if isinstance(packet, ButtonPacket):
if packet.pressed:
if packet.button == ButtonPacket.BUTTON_1:
# The 1 button was pressed.
for coil in coils: coil.direction = digitalio.Direction.OUTPUT
motor = stepper.StepperMotor(coils[0], coils[1], coils[2], coils[3], microsteps=None)
for step in range(STEPS): motor.onestep()
time.sleep(DELAY)
for step in range(STEPS): motor.onestep(direction=stepper.BACKWARD)
time.sleep(DELAY)
for step in range(STEPS): motor.onestep(style=stepper.DOUBLE)
time.sleep(DELAY)
for step in range(STEPS): motor.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
time.sleep(DELAY)
for step in range(STEPS): motor.onestep(style=stepper.INTERLEAVE)
time.sleep(DELAY)
for step in range(STEPS): motor.onestep(direction=stepper.BACKWARD, style=stepper.INTERLEAVE)
time.sleep(DELAY)
motor.release()
elif packet.button == ButtonPacket.UP:
# The UP button was pressed.
print("UP button pressed!")