This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathproject02.py
94 lines (62 loc) · 2.15 KB
/
project02.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
"""
use two simultaneously running threads to:
* read Photocell periodically and save to JSON file
* code similar to your project01.py in a second thread simultaneously
"""
import machine
import time
import _thread
import project01
# project01.py also needs to be copied to the Pico
def photocell_logger(N: int, sample_interval_s: float) -> None:
"""
get raw uint16 values from photocell N times and save to JSON file
Parameters
----------
N: int
number of samples to take
"""
print("start light measurement thread")
adc = machine.ADC(28)
values: list[int] = []
start_time: tuple[int] = time.localtime()
for _ in range(N):
values.append(adc.read_u16())
time.sleep(sample_interval_s)
end_time: tuple[int] = time.localtime()
# please also log the end_time and sample interval in the JSON file
# i.e. two additional key, value in the dict
data = {
"light_uint16": values,
"start_time": start_time,
}
now: tuple[int] = time.localtime()
now_str = "-".join(map(str, now[:3])) + "T" + "_".join(map(str, now[3:6]))
filename = f"proj2-light-{now_str}.json"
print("light measurement done: write", filename)
project01.write_json(filename, data)
def blinker_response_game(N: int) -> None:
# %% setup input and output pins
led = machine.Pin("LED", machine.Pin.OUT)
button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP)
# %% please read these parameters from JSON file like project 01 instead of hard-coding
sample_ms = 10.0
on_ms = 500
t: list[float | None] = []
project01.blinker(3, led)
for i in range(N):
time.sleep(project01.random_time_interval(0.5, 5.0))
led.high()
tic = time.ticks_ms()
t0 = None
while time.ticks_diff(time.ticks_ms(), tic) < on_ms:
if button.value() == 0:
t0 = time.ticks_diff(time.ticks_ms(), tic)
led.low()
break
t.append(t0)
led.low()
project01.blinker(5, led)
project01.scorer(t)
_thread.start_new_thread(photocell_logger, (10, 0.5))
blinker_response_game(5)