-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlights.py
More file actions
23 lines (21 loc) · 1.07 KB
/
Copy pathlights.py
File metadata and controls
23 lines (21 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import random
class Lights:
def __init__(self, lights, code):
self.code = code
self.turned_on_lights = []
self.turned_off_lights = lights
def set_light_amount(self, amount):
if len(self.turned_on_lights) < amount:
increase = min(len(self.turned_off_lights), amount - len(self.turned_on_lights))
lights_to_switch = random.sample(self.turned_off_lights, k=increase)
for light in lights_to_switch:
self.code.add_loop_code(f"digitalWrite({light}, HIGH);")
self.turned_off_lights.remove(light)
self.turned_on_lights.append(light)
elif len(self.turned_on_lights) > amount:
decrease = min(len(self.turned_on_lights), len(self.turned_on_lights) - amount)
lights_to_switch = random.sample(self.turned_on_lights, k=decrease)
for light in lights_to_switch:
self.code.add_loop_code(f"digitalWrite({light}, LOW);")
self.turned_off_lights.append(light)
self.turned_on_lights.remove(light)