#import libaries
import machine
import ssd1306
from machine import Pin,ADC,Timer
from utime import sleep
from i2c_lcd import I2cLcd
# pin configuration
pin_pb=Pin(34,Pin.IN)
pin_led=Pin(16,Pin.OUT)
pin_pm=ADC(Pin(33))
pin_ms=ADC(Pin(32))
pin_red=Pin(4,Pin.OUT)
pin_green=Pin(0,Pin.OUT)
pin_blue=Pin(2,Pin.OUT)
pin_dp1= Pin(25, Pin.IN)
pin_slsw=Pin(35,Pin.IN)
sda = machine.Pin(21)
scl = machine.Pin(22)
oled_width= 128
oled_height= 64
i2c = machine.SoftI2C(sda=sda, scl=scl, freq=10000)
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
myTimer = Timer(1)
# main routine
def main():
while True:
# Subroutine here
sub_pb()
sub_ms()
oled_i2c()
sub_dp1()
# subroutine
def sub_pb():
val_pb=pin_pb.value()
print("pb:",val_pb)
if val_pb ==1:
pin_led.on()
else:
pin_led.off()
def sub_ms():
val_ms=pin_ms.read()
print("ms:",val_ms)
if val_ms==0:
pin_red.off()
pin_green.off()
pin_blue.off()
else:
pin_red.on()
pin_green.off()
pin_blue.off()
def oled_i2c():
val_pm=pin_pm.read()
print("pm:",val_pm)
oled.fill(0)
oled.text("value:"+str(val_pm),0,15)
oled.show()
def toggle_led(myTimer):
pin_blue.on()
sleep(0.5)
pin_blue.off()
sleep(0.5)
myTimer.init(period=1000, mode=Timer.PERIODIC, callback=toggle_led)
def sub_dp1():
val_dp1=pin_dp1.value()
print("dp1",val_dp1)
if val_dp1 ==1:
pin_green.on()
else:
pin_green.off()
pin_dp1.irq(trigger=Pin.IRQ_FALLING,handler=sub_dp1)
# Main function
if __name__ == '__main__':
main()