luiskrlos84
Posts: 9
Joined: Mon Mar 27, 2017 10:27 pm

Help with time cycle on photogate code

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

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



pcmanbob
Posts: 9465
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help with time cycle on photogate code

Sun May 07, 2017 12:59 pm

Hi.

Think this is doing what you want.

pi@TestPi3:~ $ python fgate.py
gpio state 1
gpio state 0
program start time 1494161593.31
gate start 1494161609.29
gate stop 1494161617.65
Time: 8.36131310463 s
gpio state 1
gpio state 0
program start time 1494161593.31
gate start 1494161643.23
gate stop 1494161643.23
Time: 0.00548505783081 s
gpio state 1
gpio state 0
program start time 1494161593.31
gate start 1494161643.24
gate stop 1494161647.22
Time: 3.98290681839 s


and this the code I used.

Code: Select all

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11,GPIO.IN)

pstart = time.time()


try:
    while True:
        
        while GPIO.input(11) == 0:
            time.sleep(0.001)
			
        print "gpio state", GPIO.input(11)
        start = time.time()	
			
        while GPIO.input(11) == 1:
            time.sleep(0.001)
                
        else:
            print "gpio state", GPIO.input(11)
            stop = time.time()
            print "program start time", pstart
            print "gate start", start
            print "gate stop", stop
            print "Time:   ", stop - start, "s"
            
             

except KeyboardInterrupt:
    #gpio.stop()
    GPIO.cleanup()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”