VizslaLover
VizslaLover

Reputation: 11

How to fix infinite loop in python

I just started with python and RPi. But i stuck :D I have 4 microswitches, and few posibilities (BC1, BC2, BC3... etc). For example, if we choose posibility BC1, then microswitch 1 (btn1) and 2 (btn2) must be active, if that case is true, then ledG1 and vazduh are active. Also I need to have a posibiliti to insert another posibiliti (BC1, BC2, BC3), and untill we insert another posibility first must be active (if requested switches are active).

With this code it's working, but ask me only once to insert possibility.

BC1 = '1'
BC2 = '2'
BC3 = '3'
BC4 = '4'
BC5 = '5'

def compare ():
    while True:
        Barcode = input("Insert barcode: ")
        while Barcode == BC1:
            if GPIO.input(btn1)==0 and GPIO.input(btn2)==0:
                GPIO.output(vazduh, GPIO.HIGH)
                GPIO.output(ledG1, GPIO.HIGH)
                continue
            else:
                GPIO.output(vazduh, GPIO.LOW)
                GPIO.output(ledG1, GPIO.LOW)
                continue                
        while Barcode == BC2:
            if GPIO.input(btn2)==0 and GPIO.input(btn4)==0:
                GPIO.output(vazduh, GPIO.HIGH)
                GPIO.output(ledG3, GPIO.HIGH)
                continue
            else:
                GPIO.output(vazduh, GPIO.LOW)
                GPIO.output(ledG3, GPIO.LOW)
                continue


compare ()

Upvotes: 0

Views: 499

Answers (1)

Farhood ET
Farhood ET

Reputation: 1541

I think you probably wanted to use if instead of while in your first while's scope.

while True:
   Barcode = input("Insert barcode: ")
   if Barcode == BC1:
      #Do something #1
   if Barcode == BC2:
      #Do something #2

Upvotes: 2

Related Questions