I've Raspberry and SHARP 2Y0A02 distance sensor. I wish to make a counter of moved object, but I've a problem that my code is counting every time it detects object and until it's finished it counting like million times. What I need to do is to count only +1 until signal lost and wait for signal once again detected and count +1 next object. My code is:
Code: Select all
import RPi.GPIO as GPIO import time GPIN = 4 GPIO.setmode(GPIO.BCM) GPIO.setup(GPIN, GPIO.IN) counting = 0 try: while True: if GPIO.input(GPIN) == 1 : counting = counting + 1 print(counting) time.sleep(1) else: counting = counting print (counting) time.sleep(1) except KeyboardInterrupt: GPIO.cleanup()
As you see when signal lost everything seems ok it keeps last digit, but when signal is detected it counts endlessly +1 until signal lost. Is there a solution for making this sensor work as counter (button like style)? Thank you.