Python Forum
single input infinite output problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: single input infinite output problem (/thread-29868.html)



single input infinite output problem - Chase91 - Sep-23-2020

This is my first time using a forum so sorry if my educate is not great, but i could use some help. I'm trying to have my program give a single output for a single input within a set of loops. If manual button 1 (button_state_man1) is pressed then as long as manual button 3 (button_state_man3) is not pressed anytime you press button 1 again, "moving up" is printed once for every press or "moving down" is printed for every press of button 2 (button_state_man2). The problem I have is that in the current code if button 1 is pressed at all "moving up" prints infinitely. If I add a break/continue it returns to the first if statement without button 3 being pressed. Can anyone help me in this matter?
while True:
    button_state_man1 = GPIO.input(BUTTON_MAN1)
    button_state_man2 = GPIO.input(BUTTON_MAN2)
    button_state_man3 = GPIO.input(BUTTON_MAN3)

    if button_state_man1 == 0:
        print('1')
        sleep(delay2)
        while not button_state_man3 == 0:
            if button_state_man1 == 0:
                print('moving up')
                for x in range(step_count):
                    GPIO.output(DIR, 1)
                    GPIO.output(STEP, GPIO.HIGH)
                    sleep(delay)
                    GPIO.output(STEP, GPIO.LOW)
                    sleep(delay)
                  
            if button_state_man2 == 0:
                print('moving down')
                for x in range(step_count):
                    GPIO.output(DIR, 0)
                    GPIO.output(STEP, GPIO.HIGH)
                    sleep(delay)
                    GPIO.output(STEP, GPIO.LOW)
                    sleep(delay)



RE: single input infinite output problem - Yoriz - Sep-23-2020

When the code gets to this line
while not button_state_man3 == 0:
it will be an infinite loop because the button states
button_state_man1 = GPIO.input(BUTTON_MAN1)
button_state_man2 = GPIO.input(BUTTON_MAN2)
button_state_man3 = GPIO.input(BUTTON_MAN3)
are only updated before it enters this while loop, once insidethe for loop all button states will stay the same.


RE: single input infinite output problem - Chase91 - Sep-23-2020

Thanks! Its amazing how an oversight can be so frustrating.