from __future__ import print_function import NPi.GPIO as GPIO import time PIN_NUM = 12 GPIO.setmode(GPIO.BOARD) GPIO.setup(PIN_NUM, GPIO.IN, GPIO.PUD_DOWN) print("The value of Pin %d is %d" % (PIN_NUM, GPIO.input(PIN_NUM))) def my_callback(channel): print("Callback trigger %d" % channel) print("Now value of the Pin is %d" % (GPIO.input(PIN_NUM))) print("Click Ctr + C to exit") GPIO.add_event_detect(PIN_NUM, GPIO.RISING, callback=my_callback, bouncetime=300) try: while True: time.sleep(0.1) except KeyboardInterrupt: pass GPIO.cleanup()
#!/usr/bin/env python from __future__ import print_function import NPi.GPIO as GPIO import time from threading import Timer SWITCH_PIN = 10 GPIO.setmode(GPIO.BOARD) GPIO.setup(SWITCH_PIN, GPIO.IN, GPIO.PUD_DOWN) print("\n value_%d = %d\n" % (SWITCH_PIN, GPIO.input(SWITCH_PIN))) GPIO.add_event_detect(SWITCH_PIN, GPIO.RISING, bouncetime=200) # add rising edge detection on a channel switchcount = 0 while switchcount < 2: if GPIO.event_detected(SWITCH_PIN): switchcount += 1 print('Button pressed', switchcount) print("\n value_%d = %d\n" % (SWITCH_PIN, GPIO.input(SWITCH_PIN))) GPIO.remove_event_detect(SWITCH_PIN)