36 lines
707 B
Python
36 lines
707 B
Python
import time
|
|
import _thread
|
|
from machine import Pin, ADC, I2C
|
|
from screen import Screen
|
|
|
|
light_adc = ADC(Pin(1))
|
|
light_adc.atten(ADC.ATTN_11DB)
|
|
light_adc.width(ADC.WIDTH_12BIT)
|
|
|
|
screen = Screen()
|
|
|
|
def read_light(samples=8):
|
|
total = 0
|
|
for _ in range(samples):
|
|
total += light_adc.read()
|
|
time.sleep_ms(5)
|
|
return total // samples
|
|
|
|
def sensor_task():
|
|
while True:
|
|
raw = read_light()
|
|
percent = int((raw / 4095) * 100)
|
|
|
|
lines = [
|
|
"Light Sensor",
|
|
f"Raw: {raw}",
|
|
f"Level: {percent}%"
|
|
]
|
|
|
|
screen.display_lines(lines)
|
|
|
|
time.sleep(1)
|
|
|
|
_thread.start_new_thread(sensor_task, ())
|
|
|
|
print("Main thread running") |