Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- from time import sleep
- from datetime import datetime
- from decimal import *
- #-----------------------------------------------------------#
- # General Parameters #
- #-----------------------------------------------------------#
- #display no warnings (on command line)
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(13, GPIO.IN) # GPIO2, BPW85 phototransistor
- GPIO.setup(15, GPIO.IN) # GPIO3, BPW85 phototransistor
- global lg1, lg2, dt1, dt2, distance
- lg1 = 0
- lg2 = 0
- distance = 30000 #um, distance between light gates
- #-----------------------------------------------------------#
- #function LIGHTGATE: function is executed when input pins #
- # are edge triggered #
- #-----------------------------------------------------------#
- def lightgate(channel):
- #Get current date/time stamp
- dt = datetime.now()
- print "Edge triggered on channel ", channel, " at ", dt
- print " "
- global lg1, lg2, dt1, dt2
- #Lightgate 1
- if channel==13: #detect if lightgate 1 is triggered
- lg1 = 1
- dt1 = dt #remember date/time as dt1
- if lg2==1: #check if lightgate 2 was triggered already
- ddt = dt1-dt2 #calculate time difference between lightgate 1 and 2
- s = "Goodbye birdey!" #string mentions that the bird just left the birdhouse
- #Lightgate 2
- if channel==15: #detect if lightgate 2 is triggered
- lg2 = 1
- dt2 = dt #remember date/time as dt2
- if lg1==1: #check if lightgate 1 was triggered already
- ddt = dt2 - dt1 #calculate time difference between lightgate 2 and 1
- s = "Hello birdey!" #string mentions that the bird just entered the birdhouse
- if lg1*lg2 == 1: #if both lightgates were triggered, reset the status
- lg1 = 0
- lg2 = 0
- print s
- print " "
- print "dt2 : ", dt2
- print "dt1 : ", dt1
- print "time diff: ", ddt
- print " "
- #calculate time difference between lightgates in microseconds (us)
- deltat = 1000000*ddt.seconds+ddt.microseconds #us
- #determine decimal precision
- getcontext().prec = 2
- print "distance : ", Decimal(distance), "um"
- print "deltaT : ", Decimal(deltat), "us"
- print " "
- #calculate speed of bird leaving/entering the birdhouse
- print "speed : ", Decimal(distance) / Decimal(deltat), "m/s"
- print " "
- #-----------------------------------------------------------#
- # GPIO edge trigger #
- #-----------------------------------------------------------#
- #Check for falling edge of input pins. Call function LIGHTGATE if triggered
- GPIO.add_event_detect(15,GPIO.FALLING,callback=lightgate,bouncetime=200)
- GPIO.add_event_detect(13,GPIO.FALLING,callback=lightgate,bouncetime=200)
- #-----------------------------------------------------------#
- # main loop... sleeping... #
- #-----------------------------------------------------------#
- try:
- while True:
- sleep(1) #Sleep as long as no edge is triggered
- #-----------------------------------------------------------#
- # Watch for keyboard interupt #
- #-----------------------------------------------------------#
- except KeyboardInterrupt: #CTRL+C
- print "KEYBOARD INTERRUPT (CTRL+C)!"
- finally:
- print "Cleaning up..."
- GPIO.cleanup()
- print "Done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement