Guest User

Frequent button polling

a guest
Jul 11th, 2016
126
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. GPIO.setmode(GPIO.BCM)
  7. GPIO.setwarnings(False)
  8.  
  9. LEDRed = 18
  10. LEDYellow = 23
  11. LEDGreen = 24
  12. ButtonPin = 25
  13.  
  14. GPIO.setup(LEDRed, GPIO.OUT)
  15. GPIO.setup(LEDYellow, GPIO.OUT)
  16. GPIO.setup(LEDGreen, GPIO.OUT)
  17. GPIO.setup(ButtonPin, GPIO.IN)
  18.  
  19. correct_button = 0
  20.  
  21. start = time.time()
  22.  
  23. while correct_button <= 10:
  24.     led_choice = random.randint(1,3)
  25.     button_seen = False
  26.     for i in xrange(200):
  27.         if led_choice == 1:
  28.             led = LEDRed
  29.         if led_choice == 2:
  30.             led = LEDYellow
  31.             if GPIO.input(ButtonPin) == False:
  32.                 button_seen = True
  33.         if led_choice == 3:
  34.             led = LEDGreen
  35.     if button_seen:
  36.         correct_button = correct_button + 1
  37.     GPIO.output(led, GPIO.HIGH)
  38.     time.sleep(0.001)
  39.     GPIO.output(led, GPIO.LOW)
  40.  
  41. end = time.time()
  42.  
  43. time_taken = end - start
  44. time_taken = str(time_taken)
  45.  
  46. print("Time taken in seconds is " + time_taken)
  47.  
  48. GPIO.cleanup()
RAW Paste Data