-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightSensor.py
55 lines (48 loc) · 1.32 KB
/
LightSensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import RPi.GPIO as GPIO
import time
# Assign GPIO pin numbers to variables
s2 = 16
s3 = 18
sig = 22 #labeled "out" on your board
cycles = 10
# Setup GPIO and pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(s2, GPIO.OUT)
GPIO.setup(s3, GPIO.OUT)
GPIO.setup(sig, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def DetectColor():
# Detect red values
GPIO.output(s2, GPIO.LOW)
GPIO.output(s3, GPIO.LOW)
time.sleep(0.1)
start_time = time.time()
for count in range(cycles):
GPIO.wait_for_edge(sig, GPIO.FALLING)
duration = time.time() - start_time
red = cycles / duration
print("red value - ", red)
# Detect blue values
GPIO.output(s2, GPIO.LOW)
GPIO.output(s3, GPIO.HIGH)
time.sleep(0.1)
start_time = time.time()
for count in range(cycles):
GPIO.wait_for_edge(sig, GPIO.FALLING)
duration = time.time() - start_time
blue = cycles / duration
print("blue value - ", blue)
# Detect green values
GPIO.output(s2, GPIO.HIGH)
GPIO.output(s3, GPIO.HIGH)
time.sleep(0.1)
start_time = time.time()
for count in range(cycles):
GPIO.wait_for_edge(sig, GPIO.FALLING)
duration = time.time() - start_time
green = cycles / duration
print("green value - ", green)
try:
while True:
DetectColor()
except KeyboardInterrupt:
GPIO.cleanup()