[Tutor] (no subject)

Megan Ani Mirkhanian mmirkhan at uci.edu
Mon Apr 4 18:02:45 EDT 2022


Hi,

I am writing a code to make my stepper motor back and forward.
I wrote the code and then at the bottom I created a for loop which commands
the stepper motor to go backwards and then forwards. I am trying to find a
way to make the two for loops variables so that when I want to make the
stepper motor go backwards I click on for instance x and enter and when I
want the stepper motor to go forward I click on for instance y and enter.
Is there a way to do this?

# Stepper1.py

import RPi.GPIO as GPIO
import time

IN_1 = 16  # adapt to your wiring IN1 is grey which is 16
IN_2 = 18 # IN2 is red which is 18
IN_3 = 7 # IN3 is yellow which is 7
IN_4 = 11 # IN4 is white which is 11
delay = 0.002 # time to settle

def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(IN_1, GPIO.OUT)
    GPIO.setup(IN_2, GPIO.OUT)
    GPIO.setup(IN_3, GPIO.OUT)
    GPIO.setup(IN_4, GPIO.OUT)

def forwardStep():
    setStepper(1, 0, 1, 0)
    setStepper(0, 1, 1, 0)
    setStepper(0, 1, 0, 1)
    setStepper(1, 0, 0, 1)
    setStepper(1, 0, 1, 0)
    setStepper(0, 1, 1, 0)
    setStepper(0, 1, 0, 1)
    setStepper(1, 0, 0, 1)


def backwardStep():
    setStepper(1, 0, 0, 1)
    setStepper(0, 1, 0, 1)
    setStepper(0, 1, 1, 0)
    setStepper(1, 0, 1, 0)
    setStepper(1, 0, 0, 1)
    setStepper(0, 1, 0, 1)
    setStepper(0, 1, 1, 0)
    setStepper(1, 0, 1, 0)

def setStepper(in1, in2, in3, in4):
    GPIO.output(IN_1, in1)
    GPIO.output(IN_2, in2)
    GPIO.output(IN_3, in3)
    GPIO.output(IN_4, in4)
    time.sleep(delay)

setup()

for i in range(256):
     backwardStep()

for i in range(256):
     forwardStep()


More information about the Tutor mailing list