from machine import Pin, ADC, PWM
import time
import math
rs = Pin(32, Pin.OUT)
e = Pin (22, Pin.OUT)
data = [
Pin(21, Pin.OUT),
Pin(19, Pin.OUT),
Pin(18, Pin.OUT),
Pin(5, Pin.OUT)
]
LCD_Clear = 0x01
LCD_Home = 0x02
LCD_Entery = 0x04
Entery_Left = 0x02
LCD_Display = 0x08
Display_ON = 0x04
LCD_FUNCTION = 0x20
FUNC_4BIT_2LINE = 0x08
LCD_SET_DDRAM = 0x80
ROW_ADDR = (0x00, 0x40)
def _pulse():
e.value(1)
time.sleep_us(1)
e.value(0)
time.sleep_us(50)
def _write4(nibble):
for i in range 4:
data[i].value((nibble >> i) & 1)
_pulse()
def _send(value, is_data):
rs.value(1 if is_data else 0)
_write4(value >> 4)
_write4(value & 0x0F)
def command(cmd):
_send(cmd, False)
if cmd in (LCD_Clear, LCD_Home):
time.sleep_ms(2)
def write_char(ch):
_send(ord(ch), True)
def write_str(text):
for ch in text:
write_char(ch)
def set_cursor(col1, row):
command(0x80 | (ROW_ADDR[row] + col1))
def lcd_init()
ldr = ADC(Pin(33))
ldr.atten(ADC.ATTN_11DB)
pir = Pin(35, Pin.IN)
ntc = ADC(Pin(34))
ntc.atten(ADC.ATTN_11DB)
def read_celicus():
row = ntc.read()
if row <= 0 or row >= 4095:
return -273
r = 4095.0 / row - 1.0
return 1.0 / (math.log(1.0 / r) / 3950.0 + 1.0 / 298.15) - 273.15
red = Pin(2, freq = 1000, duty=0)
green = Pin(4, freq = 1000, duty=0)
blue = Pin(16, freq = 1000, duty=0)
def set_color(red, gree, blue):
red.duty(red)
green.duty(green)
blue.duty(blue)
btn = Pin(17, Pin.INPUT, Pin.PULL_UP)
armed = False
last_press = 0
def on_press(pin):
global armed, last_press
now = time.ticks_ms()
if time.ticks_diff(now, last_press):
armed = not armed
last_press = now
btn.irq(trigger=Pin.IRQ_FALLING, handler=on_press)
Dark_LIMIT = 1500
HEAT_LIMIT = 45
def decide(light, temp_c ,motion):
if temp_c > HEAT_LIMIT:
return "ALERT"
if armed and light < Dark_LIMIT and motion == 1:
return "ALERT"
if armed:
return "ARMED"
return "OK"
buzzer = PWM(Pin(32), freq=1000, duty=0)
lcd_init()
light = 0
temp_c = 0.0
state = "OK"
last_sense = 0
last_lcd = 0
while