from machine import Pin, I2C, ADC
import ssd1306
import time
print("=== LAB 9 DEMO VERSION (NO ASYNC) ===")
# ================= OLED =================
i2c = I2C(1, sda=Pin(6), scl=Pin(7), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# ================= POTENTIOMETER (VOC SIMULATION) =================
adc = ADC(28)
# ================= BUTTON =================
button = Pin(3, Pin.IN, Pin.PULL_UP)
running = True
btn_flag = False
last_press = 0
def button_irq(pin):
global btn_flag, last_press
now = time.ticks_ms()
if time.ticks_diff(now, last_press) > 300:
btn_flag = True
last_press = now
button.irq(trigger=Pin.IRQ_FALLING, handler=button_irq)
# ================= MAIN LOOP =================
while True:
# обробка кнопки
if btn_flag:
btn_flag = False
running = not running
if not running:
oled.fill(0)
oled.text("PROGRAM OFF", 20, 28)
oled.show()
time.sleep(0.3)
continue
raw = adc.read_u16()
# 0..65535 -> 0..500
voc_index = int((raw / 65535) * 500)
oled.fill(0)
oled.text("VOC Index:", 0, 0)
oled.text(str(voc_index), 0, 12)
# 5 секторів
sector_width = 20
start_x = 10
start_y = 30
height = 20
for i in range(5):
x = start_x + i * (sector_width + 2)
oled.rect(x, start_y, sector_width, height, 1)
# визначення активного сектору
sector = voc_index // 100
if sector > 4:
sector = 4
for i in range(sector + 1):
x = start_x + i * (sector_width + 2)
oled.fill_rect(x+1, start_y+1, sector_width-2, height-2, 1)
# текстова інтерпретація
if voc_index <= 100:
quality = "Excellent"
elif voc_index <= 200:
quality = "Good"
elif voc_index <= 300:
quality = "Light"
elif voc_index <= 400:
quality = "Moderate"
else:
quality = "Bad"
oled.text(quality, 0, 55)
oled.show()
time.sleep(0.2)