from machine import Pin, ADC, PWM, I2C
from ssd1306 import SSD1306_I2C
from neopixel import NeoPixel
import time
#定义一个具体的adc对象
adc = ADC(Pin(34))
led = PWM(Pin(25))
buzzer = PWM(Pin(16))
LED_NUM=1 #灯珠数量
#板上灯珠连接到引脚10
pin = Pin(25, Pin.OUT)
np = NeoPixel(pin, LED_NUM) # 构建灯珠对象
color1 = (255, 0, 0)
color2 = (0, 255, 0)
color3 = (0, 0, 255)
i2c = I2C(0, scl = Pin(5), sda = Pin(18))
oled = SSD1306_I2C(128, 64, i2c)
#设置ADC的(精度)位数为10(0~1023)
adc.width(ADC.WIDTH_10BIT)
adc.atten(ADC.ATTN_11DB) #测量量程增大到3.3V
while 1:
value = adc.read()
print(value)
led.duty(value)
buzzer.freq(value+1)
oled.fill(0)
oled.text(str(value), 30, 15)
oled.text(str('%.2f'%(value/1023*3.3))+"V",30,55) #('%.2f'%)保留2位小数
oled.show()
if value >0 and value<=300:
np[0] = color1
np.write()
elif value >300 and value<=650:
np[0] = color2
np.write()
else:
np[0] = color3
np.write()