March 17, 2024, 17:32
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) ControlPin = [7, 11, 13, 15] for pin in ControlPin: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, 0) StepCount = 8 # this two-dimensional array holds the eight-step sequence for half stepping Seq = [ [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] ] # GPIO.output(enable_pin, 1) def setStep(w1, w2, w3, w4): GPIO.output(ControlPin[0], w1) GPIO.output(ControlPin[1], w2) GPIO.output(ControlPin[2], w3) GPIO.output(ControlPin[3], w4) def forward(delay, steps): for i in range(steps): for j in range(StepCount): setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3]) time.sleep(delay) def backwards(delay, steps): for i in range(steps): for j in reversed(range(StepCount)): setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3]) time.sleep(delay) if __name__ == '__main__': while True: delay = input("Delay (ms)?") steps = input("How many steps forwards? ") forward(int(delay) / 1000.0, int(steps)) steps = input("How many steps backwards? ") backwards(int(delay) / 1000.0, int(steps)) # this cleanup command is needed so that all the pins are reset once the program ends GPIO.cleanup()