forked from BostonUniversitySeniorDesign/2024-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise_light.py
More file actions
45 lines (33 loc) · 805 Bytes
/
Copy pathexercise_light.py
File metadata and controls
45 lines (33 loc) · 805 Bytes
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
#!/usr/bin/env python3
"""
Use analog input with photocell
"""
import time
import machine
# GP28 is ADC2
ADC2 = 28
led = machine.Pin("LED", machine.Pin.OUT)
adc = machine.ADC(ADC2)
blink_period = 0.1
max_bright = 20000
min_bright = 10000
def clip(value: float) -> float:
"""clip number to range [0, 1]"""
if value < 0:
return 0
if value > 1:
return 1
return value
while True:
value = adc.read_u16()
print(value)
"""
need to clip duty cycle to range [0, 1]
this equation will give values outside the range [0, 1]
So we use function clip()
"""
duty_cycle = clip((value - min_bright) / (max_bright - min_bright))
led.high()
time.sleep(blink_period * duty_cycle)
led.low()
time.sleep(blink_period * (1 - duty_cycle))