0

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 ()
2
  • 10
    You don't break out of any of your while loops Commented Mar 28, 2019 at 11:48
  • I tried with break after else, after that constantly request to input variant. But when i press switches it don't work. It's working only if I frist press requested switches and after that input variant for that switches (but that is not wahat I need) Commented Mar 28, 2019 at 12:48

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

@VizslaLover Can you elaborate more? Can you print anything after getting the input from the user? maybe the problem is not with the loops.
Sorry, when I tried with IF, it's same as i tried wit break after else. When I tried with IF it constantly request to input variant. But when i press switches it don't work. It's working only if I frist press requested switches and after that input variant for that switches (but that is not wahat I need)
@VizslaLover Are you using Raspberry Pi's built in push buttons or are you using different buttons? I suspect your problem might be with the hardware and how you read it. From what I remember, since buttons are mechanical, they are a bit laggy and are not as fast as digital buttons. You have to wait a bit after reading your ports data.
I use push buttons, and it works with code on beginning. But with that code it ask me only first time for variant. And for insertion other variant i need to run code again. And I need to have possibility to insert variant all the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.