SHARE
TWEET

Fancier frequent sampling

ke1g Jul 12th, 2016 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import time
  3. import random
  4. import RPi.GPIO as GPIO
  5.  
  6. # Define "tuning" constants early in the file
  7. SAMPLE_INTERVAL = 0.001
  8. STATE_INTERVAL = 0.2
  9.  
  10. LEDRed = 18
  11. LEDYellow = 23
  12. LEDGreen = 24
  13. LEDs = (LEDRed, LEDYellow, LEDGreen)
  14. ButtonPin = 25
  15.  
  16. # Put actual work after base constants are defined
  17. GPIO.setmode(GPIO.BCM)
  18. GPIO.setwarnings(False)
  19.  
  20. GPIO.setup(LEDRed, GPIO.OUT)
  21. GPIO.setup(LEDYellow, GPIO.OUT)
  22. GPIO.setup(LEDGreen, GPIO.OUT)
  23. GPIO.setup(ButtonPin, GPIO.IN)
  24.  
  25. # Derived constants can come later.
  26. SAMPLE_PER_STATE = STATE_INTERVAL / SAMPLE_INTERVAL
  27.  
  28. correct_button = 0
  29.  
  30. start = time.time()
  31.  
  32. while correct_button <= 10:
  33.     led = LEDs[random.randint(0, 2)]
  34.     GPIO.output(led, GPIO.HIGH)
  35.     button_seen = False
  36.     if led == LEDYellow:
  37.         for i in range(SAMPLE_PER_STATE):
  38.             if not GPIO.input(ButtonPin):
  39.                 button_seen = True
  40.             time.sleep(SAMPLE_INTERVAL)
  41.         if button_seen:
  42.             correct_button = correct_button + 1
  43.     else:
  44.         time.sleep(STATE_INTERVAL)
  45.     GPIO.output(led, GPIO.LOW)
  46.  
  47. end = time.time()
  48.  
  49. time_taken = end - start
  50. time_taken = str(time_taken)
  51.  
  52. print("Time taken in seconds is " + time_taken)
  53.  
  54. GPIO.cleanup()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top