def my_callback(channel): global start global end if GPIO.input(25) == 1: start = time.time() if GPIO.input(25) == 0: end = time.time() elapsed = end - start print(elapsed) GPIO.add_event_detect(25, GPIO.BOTH, callback=my_callback, bouncetime=200)