Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- import RPi.GPIO as GPIO
- import time
- __author__ = 'Gus (Adapted from Adafruit)'
- __license__ = "GPL"
- __maintainer__ = "pimylifeup.com"
- GPIO.setmode(GPIO.BOARD)
- #define the pin that goes to the circuit
- pin_to_circuit = 7
- def rc_time (pin):
- #Output on the pin for
- GPIO.setup(pin_to_circuit, GPIO.OUT)
- GPIO.output(pin_to_circuit, GPIO.LOW)
- time.sleep(.1)
- #Change the pin back to input
- GPIO.setup(pin_to_circuit, GPIO.IN)
- compare = [0,0] # The list "compare" will store successive values of count, with the most recent value in the first entry.
- bigCount = 0 # Initialize a variable "bigCount" which will be used to detect whether we have obtained the first two values of count needed to do our first comparison.
- count = 0
- differenceThreshold = 1500 # IMPORTANT: Put whatever value you need for the difference to be for the trigger condition here.
- # The outer WHILE loop will run until the following three conditions have been met:
- #
- # (1) The second entry of "compare" has a nonzero value (i.e. we have obtained at least one count).
- # (2) The first entry of "compare" has a nonzero value (i.e. we have obtained at least two counts).
- # (3) The previous two conditions are met, and the difference between the last two values of count is at least "differenceThreshold."
- while abs(compare[0] - compare[1]) < differenceThreshold or (compare[0] == 0 or compare[1] == 0):
- #Output on the pin for
- GPIO.setup(pin_to_circuit, GPIO.OUT)
- GPIO.output(pin_to_circuit, GPIO.LOW)
- time.sleep(.1)
- #Change the pin back to input
- GPIO.setup(pin_to_circuit, GPIO.IN)
- if bigCount == 0: # If bigCount is not at least 2, then we haven't obtained at least two values of count to compare yet.
- count = 0 # Reset the variable "count" to 0.
- while (GPIO.input(pin_to_circuit) == GPIO.LOW): # Obtain a value of "count.
- count += 1
- compare[0] = count # Assign the (1 - bigCount)^th entry of compare to the value of "count" obtained by the preceding while loop.
- bigCount = 1 # Set bigCount to 1.
- elif bigCount == 1: # If bigCount is equal to 2, then "compare" has two nonzero entries, and we can begin comparing consecutive values of "count."
- count = 0 # Reset count to 0.
- compare[1] = compare[0] # Since the outer WHILE loop was not triggered, we need to sample a new value of count; hence we need to send the first entry of "compare" to the second in preparation for assigning a new value to the first entry.
- while (GPIO.input(pin_to_circuit) == GPIO.LOW): # Generate a new value of count.
- count += 1
- compare[0] = count # Assign the new value of count to the first entry of compare.
- # Since we have exited the outer WHILE loop above, our two values of count were sufficiently far apart. Return the last value of count computed.
- return compare[0]
- while True:
- print (rc_time(pin_to_circuit))
- GPIO.setup(16, GPIO.OUT)
- GPIO.setup(22, GPIO.OUT)
- GPIO.output(16, True)
- time.sleep(0.5)
- GPIO.output(22, True)
- time.sleep(0.5)
- GPIO.output(22, False)
- GPIO.output(16, False)
- time.sleep(1) #this is to make 1 second before can fire again
- except KeyboardInterrupt:
- print("Goodbye")
- quit()
- except Exception as e: # is this the right place to put the exception raising?
- print(e)
- raise e
- finally:
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment