Toude

Morcup Rasp Code

May 29th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1.  
  2. #!/usr/local/bin/python
  3.  
  4. import RPi.GPIO as GPIO
  5. import time
  6.  
  7. __author__ = 'Gus (Adapted from Adafruit)'
  8. __license__ = "GPL"
  9. __maintainer__ = "pimylifeup.com"
  10.  
  11. GPIO.setmode(GPIO.BOARD)
  12.  
  13. #define the pin that goes to the circuit
  14. pin_to_circuit = 7
  15.  
  16.  
  17. def rc_time (pin):
  18.  
  19.     #Output on the pin for
  20.     GPIO.setup(pin_to_circuit, GPIO.OUT)
  21.     GPIO.output(pin_to_circuit, GPIO.LOW)
  22.     time.sleep(.1)
  23.  
  24.     #Change the pin back to input
  25.     GPIO.setup(pin_to_circuit, GPIO.IN)
  26.  
  27.  
  28.  
  29.     compare = [0,0]        # The list "compare" will store successive values of count, with the most recent value in the first entry.
  30.     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.
  31.     count = 0            
  32.     differenceThreshold = 1500 # IMPORTANT: Put whatever value you need for the difference to be for the trigger condition here.
  33.  
  34.  
  35.     # The outer WHILE loop will run until the following three conditions have been met:
  36.     #
  37.     #     (1) The second entry of "compare" has a nonzero value (i.e. we have obtained at least one count).
  38.     #    (2) The first entry of "compare" has a nonzero value (i.e. we have obtained at least two counts).
  39.     #    (3) The previous two conditions are met, and the difference between the last two values of count is at least "differenceThreshold."
  40.     while abs(compare[0] - compare[1]) < differenceThreshold or (compare[0] == 0 or compare[1] == 0):
  41.             #Output on the pin for
  42.         GPIO.setup(pin_to_circuit, GPIO.OUT)
  43.         GPIO.output(pin_to_circuit, GPIO.LOW)
  44.         time.sleep(.1)
  45.  
  46.         #Change the pin back to input
  47.         GPIO.setup(pin_to_circuit, GPIO.IN)
  48.         if bigCount == 0:    # If bigCount is not at least 2, then we haven't obtained at least two values of count to compare yet.
  49.             count = 0    # Reset the variable "count" to 0.
  50.             while (GPIO.input(pin_to_circuit) == GPIO.LOW):        # Obtain a value of "count.
  51.                 count += 1
  52.             compare[0] = count     # Assign the (1 - bigCount)^th entry of compare to the value of "count" obtained by the preceding while loop.
  53.             bigCount = 1    # Set bigCount to 1.
  54.  
  55.         elif bigCount == 1:   # If bigCount is equal to 2, then "compare" has two nonzero entries, and we can begin comparing consecutive values of "count."
  56.             count = 0        # Reset count to 0.
  57.             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.
  58.             while (GPIO.input(pin_to_circuit) == GPIO.LOW):     # Generate a new value of count.
  59.                 count += 1
  60.             compare[0] = count         # Assign the new value of count to the first entry of compare.
  61.    
  62.     # 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.
  63.     return compare[0]
  64.  
  65. while True:  
  66.     print (rc_time(pin_to_circuit))
  67.     GPIO.setup(16, GPIO.OUT)
  68.     GPIO.setup(22, GPIO.OUT)
  69.     GPIO.output(16, True)
  70.     time.sleep(0.5)
  71.     GPIO.output(22, True)
  72.     time.sleep(0.5)
  73.     GPIO.output(22, False)
  74.     GPIO.output(16, False)
  75.     time.sleep(1)   #this is to make 1 second before can fire again
  76.  
  77.            
  78. except KeyboardInterrupt:
  79.     print("Goodbye")
  80.     quit()
  81.  
  82.  
  83. except Exception as e:   # is this the right place to put the exception raising?
  84.     print(e)
  85.     raise e
  86. finally:
  87.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment