from machine import ADC, Pin, I2C, time_pulse_us
from utime import sleep, sleep_us
from i2c_lcd import I2cLcd
from picozero import Speaker
import neopixel
colors = [
(255,0,0),
(255,255,0),
(255,255,255),
(0,255,0),
(0,255,255),
(0,0,255),
(0,0,0),
]
pins = [
Pin(3, Pin.OUT),
Pin(4, Pin.OUT),
Pin(5, Pin.OUT),
Pin(6, Pin.OUT),
Pin(7, Pin.OUT),
Pin(10, Pin.OUT),
Pin(11, Pin.OUT),
Pin(15, Pin.OUT)
]
digits = [
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,1],
[0,0,0,0,0,1,1,1],
[0,0,0,0,1,1,1,1],
[0,0,0,1,1,1,1,1],
[0,0,1,1,1,1,1,1],
[0,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1],
]
switch = Pin(21, Pin.IN)
pixels = neopixel.NeoPixel(Pin(2), 16)
gas_sensor = ADC(26)
VREF = 3.3
ADC_RES = 65536 #2^16
DOLLAR_RATE_PER_VOLT = 2.5
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
trig = Pin(28, Pin.OUT)
echo = Pin(27, Pin.IN)
buzzer = Speaker(8)
def measure_distance():
trig.low()
sleep_us(4)
trig.high()
sleep_us(4)
trig.low()
duration = time_pulse_us(echo, 1)
return duration/58.82
color_index = 0
while True:
if switch.value() == 1:
buzzer.play(500, duration=0.05)
sleep(0.05)
continue
lcd.move_to(0,0)
raw_gas = gas_sensor.read_u16()
voltage = (raw_gas / ADC_RES) * VREF
lcd.putstr(str(voltage))
distance = str(measure_distance())
lcd.move_to(0,1)
lcd.putstr(distance)
sleep(1)
lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Getting new info...")
sleep(1)
lcd.clear()
color_index = (color_index + 1) % len(colors)
for i in range(len(pixels)):
pixels[i] = colors[color_index]
pixels.write()
sleep(0.1)
for j in range(len(colors)):
pins[j].value(digits[color_index][j])