Adafruit stepper motor HAT python help?


#1

Not sure if it’s the right place for this but I’ve had my stepper hat delivered and all is working fine and have it turning by a bipolar stepper motor, but need help getting more from the demo script.

I’ve used the instructions from the link on the Pimoroni shop to here: Adafruit Motor Hat

I’ve copied it all and taken out what I think I don’t need and changed it to get a continuous turn but I’m trying to get it to do a full 360 rotate then stop. I’ve increased the steps to get (about) a full 360 but the demo script just keeps looping through??

My idea is to have a button press then 360 rotate and stop, until the next button press. I’m still trying to learn python but can’t even figure out a loop :-/

This is what I have so far:

#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor

import time
import atexit

# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT(addr = 0x60)

# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
    mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
 
atexit.register(turnOffMotors)

myStepper = mh.getStepper(200, 1)       # 200 steps/rev, motor port #1
myStepper.setSpeed(800)                  # 800 RPM

while (True):
	print("Interleaved coil steps")
	myStepper.step(400, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.INTERLEAVE)

Thanks


#2

Just glancing at that code:

while (True):

will run the code while True = True (which is all the time, so the code inside the while loop will run continuously).

So if you comment out the while loop (put a # in front of every line) and then put the myStepper.step(400, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.INTERLEAVE) line at the bottom of the code, not indented, you should see it run once, I think.