Prevent REPL from freezing

This commit is contained in:
2025-12-25 02:29:37 +01:00
parent 3702c7ef7b
commit 5f5de3ccc6

View File

@@ -1,11 +1,11 @@
import time
import _thread
from machine import Pin, ADC, I2C
from screen import Screen
# ----- ADC (Grove Light Sensor) -----
light_adc = ADC(Pin(1))
light_adc.atten(ADC.ATTN_11DB) # 03.3V range
light_adc.width(ADC.WIDTH_12BIT) # 04095
light_adc.atten(ADC.ATTN_11DB)
light_adc.width(ADC.WIDTH_12BIT)
screen = Screen()
@@ -16,12 +16,18 @@ def read_light(samples=8):
time.sleep_ms(5)
return total // samples
while True:
raw = read_light()
percent = int((raw / 4095) * 100)
def sensor_task():
while True:
raw = read_light()
percent = int((raw / 4095) * 100)
screen.display("Light Sensor", 15, 0)
screen.display("Raw: {}".format(raw), 15, 20)
screen.display("Level: {}%".format(percent), 15, 40)
screen.clear()
screen.display("Light Sensor", 15, 0)
screen.display("Raw: {}".format(raw), 15, 20)
screen.display("Level: {}%".format(percent), 15, 40)
time.sleep(0.5)
time.sleep(1)
_thread.start_new_thread(sensor_task, ())
print("Main thread running")