Help with time cycle on photogate code
Posted: Sat May 06, 2017 8:50 pm
My project objective is to use a ramp that we will scan three different sizes(small, meduim, large) boxs. Measure the time delay from start and stop using our photogate circuit and depending on the size, it will activate the corresponding servo to guide the box in the correct direction terminal. The photogate circuit will be located in the top of the ramp to scan the box as it slide down and the servo depending on the measurement will sort the box...
but now
i need some help with my photogate code, the code work good but it has one defect, the time start once the program starts running but i need time to start when the photogate signal gets cut off, i would appreciate it if someone could help me out
but now
i need some help with my photogate code, the code work good but it has one defect, the time start once the program starts running but i need time to start when the photogate signal gets cut off, i would appreciate it if someone could help me out
Code: Select all
import RPi.GPIO as GPIO
import pigpio
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN)
servos = [27,22]
gpio = pigpio.pi()
start = time.time()
stop = time.time()
gateState = False
try:
while True:
if (GPIO.input(11) != gateState):
gateState = not gateState
if (gateState == True):
start = time.time()
else:
stop = time.time()
if stop - start > 0.01:
print "Time: ", stop - start, "s"
if stop - start < 2 :
gpio.set_servo_pulsewidth(servos[0], 1600)
time.sleep(1)
gpio.set_servo_pulsewidth(servos[0], 2250)
time.sleep(1)
if 2 < stop - start < 5:
gpio.set_servo_pulsewidth(servos[1], 1650)
time.sleep(1)
gpio.set_servo_pulsewidth(servos[1], 2250)
time.sleep(1)
if stop - start > 5 :
time.sleep(1)
except KeyboardInterrupt:
gpio.stop()
GPIO.cleanup()