1

I am currently building a project in which I am using a Flask web app to control various different components with the pi's GPIO. The issue i am currently having is stopping the execution of code that controls the components. When a button is clicked on the web page it starts a for loop that controls a stepper motor for a certain number of iterations. Here is that code

for x in range(1000):
    GPIO.output(pins['Right Motor']['rightInOne'], GPIO.HIGH)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInTwo'], GPIO.HIGH)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInThree'], GPIO.LOW)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInFour'], GPIO.LOW)
    time.sleep(.001)

    GPIO.output(pins['Right Motor']['rightInOne'], GPIO.LOW)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInTwo'], GPIO.HIGH)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInThree'], GPIO.HIGH)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInFour'], GPIO.LOW)
    time.sleep(.001)

    GPIO.output(pins['Right Motor']['rightInOne'], GPIO.LOW)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInTwo'], GPIO.LOW)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInThree'], GPIO.HIGH)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInFour'], GPIO.HIGH)
    time.sleep(.001)

    GPIO.output(pins['Right Motor']['rightInOne'], GPIO.HIGH)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInTwo'], GPIO.LOW)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInThree'], GPIO.LOW)
    time.sleep(.001)
    GPIO.output(pins['Right Motor']['rightInFour'], GPIO.HIGH)
    time.sleep(.001)

Where the problem lies is that when the button is clicked, the server continues execution of that for loop until it ends. I need a way to stop the execution as soon as the stop button is clicked.

Any help and/or suggestions are greatly appreciated. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.