7

I'm currently working with Raspberry Pi. I have implemented push button(switch) program using python. But, When I press the switch, then if condition is true and data stored but when I release the switch button my program is stuck. Why?

My script:

#!/usr/bin/python

import RPi.GPIO as GPIO

switch_1 = 17

GPIO.setmode(GPIO.BCM)

GPIO.setup(switch_1, IN.OUT)

while(True):
    sw1_status = GPIO.input(switch_1)

    print 'Switch_1 : ', sw1_status

    if sw1_status == True:
        print ('Data stored')
        while(sw1_status != 0): // here my program stuck.
            pass
  • This is pretty neat. – wogsland Mar 1 '17 at 20:19
10

Your program is stuck because you have made an infinite loop in it. By the time you get to the while(sw1_status != 0) line, sw1_status is guaranteed to be True, and True != 0 will always evaluate to True.

Perhaps you meant to wait until the button is released, in which case you want to update sw1_status in the loop, by replacing pass with sw1_status = GPIO.input(switch_1).

1

Also, I have used

while((GPIO.input(switch_1)) == 1):
              pass

instead of

while(sw1_status != 0): // here my program stuck.
            pass

It's working fine.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.