Python Forum
single input infinite output problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
single input infinite output problem
#1
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)
Reply
#2
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.
Reply
#3
Thanks! Its amazing how an oversight can be so frustrating.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to input & output parameters from command line argument shantanu97 1 532 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Problem restricting user input in my rock paper scissors game ashergreen 6 889 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Code giving same output no matter the input. Yort 2 537 Dec-20-2020, 05:59 AM
Last Post: buran
  Infinite loop problem Zirconyl 5 657 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Handling multi-input/output audio in python bor1904 4 915 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  using 'while loop' output going into infinite loop... amitkb 2 698 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Print output in single file using pramika loop deepakkhw 1 704 Jul-11-2020, 11:57 AM
Last Post: j.crater
  My code is giving my an output of zero, no matter what value I input PiyushBanarjee 1 676 Jul-01-2020, 04:34 AM
Last Post: bowlofred
  Problem with the input marios 4 726 May-03-2020, 01:01 PM
Last Post: marios
  Help me input the Rawdata file so that it can output DataErrorFile and ValidData.txt Halozeus 6 1,029 May-01-2020, 07:11 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020