rattionline
rattionline

Reputation: 539

stop for-loop when GPIO pin goes high

i have a stepper motor wired up to my Raspberry Pi with a DRV8825 Driver Chip. My goal is that the Motor stops turning when a sensor detects metal, like an endstop.

With the code I´ve got the Motor turns 4 rounds and then stops. When the sensor detects metal and I start "turnforward" the Motor don´t turn. So the Sensor works fine..

What I want is that the loop checks the sensor after every Step the motor turns.

Hope anyone can help me :)

GPIO.input(26) #Gets a signal from the sensor when metal is detected


def turnforward():
    while True:

        if (GPIO.input(26) == False): #False= Sensor sends LOW
            for i in range(800): # 800 steps = 4 rounds
                        GPIO.output(pinStep, True)
                        time.sleep(0.0018)
                        GPIO.output(pinStep, False)
            break # without the break the for loop in infinite
        else:
            break

Upvotes: 1

Views: 531

Answers (1)

Isaac Reed
Isaac Reed

Reputation: 116

try putting your sensor check inside the loop

def turnforward():
    for i in range(800): # 800 steps = 4 rounds
        if (GPIO.input(26) == True): #False= Sensor sends LOW::My failure!
             break
        GPIO.output(pinStep, True)
        time.sleep(0.0018)
        GPIO.output(pinStep, False)

Upvotes: 2

Related Questions