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
1
  • This is pretty neat.
    – wogsland
    Mar 1, 2017 at 20:19

2 Answers 2

9

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).

0
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 agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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