-
Notifications
You must be signed in to change notification settings - Fork 1
/
lights.py
142 lines (104 loc) · 3.29 KB
/
lights.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pigpio
import time
import random
# Set GPIO pins for R, G, B
PIN_R = 23
PIN_G = 24
PIN_B = 25
def initialize():
# Connect to the pigpio daemon
pi = pigpio.pi()
# Set initial PWM values to 0
pi.set_PWM_dutycycle(PIN_R, 0)
pi.set_PWM_dutycycle(PIN_G, 0)
pi.set_PWM_dutycycle(PIN_B, 0)
return pi
def set_color(pi, r, g, b):
# Normalize values to the range 0-255
r = max(0, min(255, r))
g = max(0, min(255, g))
b = max(0, min(255, b))
# Set PWM duty cycles for R, G, B
pi.set_PWM_dutycycle(PIN_R, 255 - r)
pi.set_PWM_dutycycle(PIN_G, 255 - g)
pi.set_PWM_dutycycle(PIN_B, 255 - b)
return time.time()
# setting the RGB values for red, yellow, green, and blue
COLORS = [[255, 0, 0], [255, 80, 0], [0, 255, 0], [0, 0, 255]]
COLORNAMES = ["RED", "YELLOW", "GREEN", "BLUE"]
def showLight(pi, results, prevColor):
rand = random.randrange(4)
while rand == prevColor:
rand = random.randrange(4)
color = COLORS[rand]
ts = set_color(pi, color[0], color[1], color[2])
results.append({"ts": ts, "color": COLORNAMES[rand]})
# print("Light", COLORNAMES[rand], "is flashing at time", ts)
return results, rand
def main():
pi = initialize()
results = []
prevColor = -1
# === TIMING ===
time.sleep(1.47)
results, prevColor = showLight(pi, results, prevColor)
for i in range(53):
time.sleep(0.7)
results, prevColor = showLight(pi, results, prevColor)
# === END TIMING ===
# Allow some time for the color to be visible (adjust as needed)
# time.sleep(2)
pi.set_PWM_dutycycle(PIN_R, 100)
pi.set_PWM_dutycycle(PIN_G, 100)
pi.set_PWM_dutycycle(PIN_B, 100)
time.sleep(1)
# Clean up GPIO
pi.stop()
results.append({"ts": results[-1]["ts"] + 0.7, "color": "PURPLE"})
return results
if __name__ == "__main__":
main()
# import pigpio
# import time
# import random
# # Set GPIO pins for R, G, B
# PIN_R = 23
# PIN_G = 24
# PIN_B = 25
# # Connect to the pigpio daemon
# pi = pigpio.pi()
# # Set initial PWM values to 0
# pi.set_PWM_dutycycle(PIN_R, 0)
# pi.set_PWM_dutycycle(PIN_G, 0)
# pi.set_PWM_dutycycle(PIN_B, 0)
# def set_color(r, g, b):
# # Normalize values to the range 0-255
# r = max(0, min(255, r))
# g = max(0, min(255, g))
# b = max(0, min(255, b))
# # Set PWM duty cycles for R, G, B
# pi.set_PWM_dutycycle(PIN_R, 255 - r)
# pi.set_PWM_dutycycle(PIN_G, 255 - g)
# pi.set_PWM_dutycycle(PIN_B, 255 - b)
# return time.time()
# # setting the RGB values for red, yellow, gren, and blu
# colors = [[255, 0, 0], [255, 80, 0], [0, 255, 0], [0, 0, 255]]
# colorNames = ["red", "yellow", "green", "blue"]
# prevColor = -1
# for i in range(0, 15):
# rand = random.randrange(4)
# while rand == prevColor:
# rand = random.randrange(4)
# color = colors[rand]
# ts = set_color(color[0], color[1], color[2])
# print("Light", colorNames[rand], "is flashing at time", ts)
# time.sleep(random.uniform(2, 3))
# prevColor = rand
# # Allow some time for the color to be visible (adjust as needed)
# # time.sleep(2)
# pi.set_PWM_dutycycle(PIN_R, 255)
# pi.set_PWM_dutycycle(PIN_G, 255)
# pi.set_PWM_dutycycle(PIN_B, 255)
# time.sleep(1)
# # Clean up GPIO
# pi.stop()