Photogate
Posted: Wed May 03, 2017 11:56 pm
I have a photogate code that its already working, it measures how long the signal gets cut, this is the code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN)
start = time.time()
stop = time.time()
gateState = False
mid = 0
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.1:
print "Time: ", stop - start, "s"
except KeyboardInterrupt:
GPIO.cleanup()
Now I have a code to control two servo motor at the same time to open at a certain angle, this is the code:
import time
import pigpio
servos = [17,27] #GPIO number
gpio = pigpio.pi()
#pulsewidth can only set between 500-2500
try:
while True:
gpio.set_servo_pulsewidth(servos[0], 500) #servo 1 to 0 degree
print("Servo {} {} micro pulses".format(servos, 500))
time.sleep(2.5)
gpio.set_servo_pulsewidth(servos[1], 1500) #servo 2 to 90 degree
print("Servo {} {} micro pulses".format(servos, 1500))
time.sleep(2.5)
gpio.set_servo_pulsewidth(servos[0], 1500)
print("Servo {} {} micro pulses".format(servos, 1500))
time.sleep(2.5)
gpio.set_servo_pulsewidth(servos[1], 500)
print("Servo {} {} micro pulses".format(servos, 500))
time.sleep(2.5)
# switch all servos off
except KeyboardInterrupt:
for s in servos:
gpio.set_servo_pulsewidth(s, 0);
gpio.stop()
Now I want to combine both codes together so depending on the time of the signal being cut it would activate one of the servo motors, our project its a ramp that we would slide three different sizes of boxes measuring it with the time delay on the photogate and then it will send the box to the corresponding gate. I included a picture of the ramp that would have the photogate circuit in the top, thanks for your help.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN)
start = time.time()
stop = time.time()
gateState = False
mid = 0
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.1:
print "Time: ", stop - start, "s"
except KeyboardInterrupt:
GPIO.cleanup()
Now I have a code to control two servo motor at the same time to open at a certain angle, this is the code:
import time
import pigpio
servos = [17,27] #GPIO number
gpio = pigpio.pi()
#pulsewidth can only set between 500-2500
try:
while True:
gpio.set_servo_pulsewidth(servos[0], 500) #servo 1 to 0 degree
print("Servo {} {} micro pulses".format(servos, 500))
time.sleep(2.5)
gpio.set_servo_pulsewidth(servos[1], 1500) #servo 2 to 90 degree
print("Servo {} {} micro pulses".format(servos, 1500))
time.sleep(2.5)
gpio.set_servo_pulsewidth(servos[0], 1500)
print("Servo {} {} micro pulses".format(servos, 1500))
time.sleep(2.5)
gpio.set_servo_pulsewidth(servos[1], 500)
print("Servo {} {} micro pulses".format(servos, 500))
time.sleep(2.5)
# switch all servos off
except KeyboardInterrupt:
for s in servos:
gpio.set_servo_pulsewidth(s, 0);
gpio.stop()
Now I want to combine both codes together so depending on the time of the signal being cut it would activate one of the servo motors, our project its a ramp that we would slide three different sizes of boxes measuring it with the time delay on the photogate and then it will send the box to the corresponding gate. I included a picture of the ramp that would have the photogate circuit in the top, thanks for your help.