Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import random
- import RPi.GPIO as GPIO
- # Define "tuning" constants early in the file
- SAMPLE_INTERVAL = 0.001
- STATE_INTERVAL = 0.2
- LEDRed = 18
- LEDYellow = 23
- LEDGreen = 24
- LEDs = (LEDRed, LEDYellow, LEDGreen)
- ButtonPin = 25
- # Put actual work after base constants are defined
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(LEDRed, GPIO.OUT)
- GPIO.setup(LEDYellow, GPIO.OUT)
- GPIO.setup(LEDGreen, GPIO.OUT)
- GPIO.setup(ButtonPin, GPIO.IN)
- # Derived constants can come later.
- SAMPLE_PER_STATE = STATE_INTERVAL / SAMPLE_INTERVAL
- correct_button = 0
- start = time.time()
- while correct_button <= 10:
- led = LEDs[random.randint(0, 2)]
- GPIO.output(led, GPIO.HIGH)
- button_seen = False
- if led == LEDYellow:
- for i in range(SAMPLE_PER_STATE):
- if not GPIO.input(ButtonPin):
- button_seen = True
- time.sleep(SAMPLE_INTERVAL)
- if button_seen:
- correct_button = correct_button + 1
- else:
- time.sleep(STATE_INTERVAL)
- GPIO.output(led, GPIO.LOW)
- end = time.time()
- time_taken = end - start
- time_taken = str(time_taken)
- print("Time taken in seconds is " + time_taken)
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement