Micropython板学习笔记
MicroPython板学习笔记之LED_RGB,按键控制:
本实验使用的硬件分别是D2、D3、D4、D5四个LED灯与K2按键,设备的硬件原理图如下。
查询pyb包的引脚对应关系:使用
import pyb
help(pyb.Pin.board)
可以查看引脚对应关系。
代码如下:
#LED_RGB,按键控制,by:ssj/2021.4.2
from pyb import Pin,LED,ADC,ExtInt,OLED,delay #导入pyb包,应用Pin,OLED,delay
led1 = LED(1)
led2 = LED(2)
led3 = LED(3)
led4 = LED(4)
adc = ADC(Pin('TVOC'))
oled = OLED()
oled.fill(0) #开启OLED
oled.show() #关闭OLED
m = 0
def sw():
global m
if m == 0:
led4.on()
m = 1
print(m)
elif m == 1:
led4.off()
m = 0
SW = lambda e : sw()
ext = ExtInt(Pin('K1'),ExtInt.IRQ_FALLING,Pin.PULL_UP,SW)
while True: #主循环
tvoc = (adc.read()/4096)*3.3*2 #读取ADC的值,12-bits精度,4096个值
if tvoc < 1.25:
tvoc = 0
led1.off() #关闭LED1
led2.off() #关闭LED2
led3.on() #开启LED3
oled.fill(0)
oled.text("G",0,14,1)
oled.show()
elif tvoc < 3.25:
tvoc = (tvoc - 1.25)/2*50
led1.off() #关闭LED1
led3.off() #关闭LED3
led2.on() #开启LED2
oled.fill(0)
oled.text("B",0,14,1)
oled.show()
else:
tvoc = (tvoc - 3.25)/0.4*50+50 #大于3.25的时候计算
led2.off() #关闭LED2
led3.off() #关闭LED3
led1.on() #开启LED1
oled.fill(0)
oled.text("R",0,14,1)
oled.show()
tvoc1 = "%.10f"%tvoc #%.10f表示显示tvoc后10位小数
oled.fill(0) #开启OLED
oled.text(tvoc1,0,24,1) #在OLED上面显示tvoc的值
oled.show() #关闭OLED