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
  problem in output of a snippet code akbarza 2 302 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 610 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  problem in using input command akbarza 4 998 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 619 Oct-18-2023, 08:16 AM
Last Post: akbarza
  problem in output of a function akbarza 9 1,095 Sep-29-2023, 11:13 AM
Last Post: snippsat
  output provide the filename along with the input file processed. arjunaram 1 903 Apr-13-2023, 08:15 PM
Last Post: menator01
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 883 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 819 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  How to split the input taken from user into a single character? mHosseinDS86 3 1,137 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  serial input/output barryjo 3 2,374 Dec-27-2021, 11:57 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

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