Prevent REPL from freezing
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
import time
|
import time
|
||||||
|
import _thread
|
||||||
from machine import Pin, ADC, I2C
|
from machine import Pin, ADC, I2C
|
||||||
from screen import Screen
|
from screen import Screen
|
||||||
|
|
||||||
# ----- ADC (Grove Light Sensor) -----
|
|
||||||
light_adc = ADC(Pin(1))
|
light_adc = ADC(Pin(1))
|
||||||
light_adc.atten(ADC.ATTN_11DB) # 0–3.3V range
|
light_adc.atten(ADC.ATTN_11DB)
|
||||||
light_adc.width(ADC.WIDTH_12BIT) # 0–4095
|
light_adc.width(ADC.WIDTH_12BIT)
|
||||||
|
|
||||||
screen = Screen()
|
screen = Screen()
|
||||||
|
|
||||||
@@ -16,12 +16,18 @@ def read_light(samples=8):
|
|||||||
time.sleep_ms(5)
|
time.sleep_ms(5)
|
||||||
return total // samples
|
return total // samples
|
||||||
|
|
||||||
while True:
|
def sensor_task():
|
||||||
|
while True:
|
||||||
raw = read_light()
|
raw = read_light()
|
||||||
percent = int((raw / 4095) * 100)
|
percent = int((raw / 4095) * 100)
|
||||||
|
|
||||||
|
screen.clear()
|
||||||
screen.display("Light Sensor", 15, 0)
|
screen.display("Light Sensor", 15, 0)
|
||||||
screen.display("Raw: {}".format(raw), 15, 20)
|
screen.display("Raw: {}".format(raw), 15, 20)
|
||||||
screen.display("Level: {}%".format(percent), 15, 40)
|
screen.display("Level: {}%".format(percent), 15, 40)
|
||||||
|
|
||||||
time.sleep(0.5)
|
time.sleep(1)
|
||||||
|
|
||||||
|
_thread.start_new_thread(sensor_task, ())
|
||||||
|
|
||||||
|
print("Main thread running")
|
||||||
Reference in New Issue
Block a user