Micropython如何让蜂鸣器声音播放的更久?
Micropython how to play buzzer sound longer?
我几乎完成了一个项目,但是当我给出一个很高的值时。我几乎听不到蜂鸣器的声音,因为它只播放了一秒钟。有谁知道怎么把蜂鸣器的声音放长一点?
import pycom
import machine
import time
from machine import Pin
import socket
from network import LoRa
import binascii
import array
from machine import PWM
############### Weightsensor ###############
def main():
while True:
adc = machine.ADC() # create an ADC object
apin = adc.channel(pin='P16') # create an analog pin on P16
val = apin() # read an analog value
if val < 20:
print(val)
print("Weight is good")
binaryString = bin(val)
print(binaryString)
time.sleep(5)
if val > 20:
print(val)
print("Weight is to high")
binaryString = bin(val)
print(binaryString)
# 50% duty cycle at 38kHz.
pwm = PWM(3, frequency=78000) # use PWM timer 0, with a frequency of 5KHz
# create pwm channel on pin P12 with a duty cycle of 50%
pwm_c = pwm.channel(0, pin='P20', duty_cycle=1.0)
pwm_c.duty_cycle(0.3) # change the duty cycle to 30%
time.sleep(5)
if __name__ == "__main__":
main()
蜂鸣器在第二个“if”语句中播放。
有人可以帮帮我吗?
亲切的问候
我在您的代码中发现了多个问题:为什么您在 while 循环中一次又一次地重新定义 adc
和 apin
?将它移到 while True
之前
Duty
在你的 PWN 中不应该影响你的蜂鸣器的音高,所以我的建议是省略所有的职责设置,或者至少让它像 250(试验一下)
蜂鸣器的音调是您 PWM 声明中的 frequency
值。您可以稍后在某处使用 pwm.frequency(hertz)
更改它。或者通过手动输入命令在控制台中找到最佳工作设置。
会响多久根据time.sleep()
在我看来代码应该是这样的(没有在船上测试过):
import pycom
import machine
import time
from machine import Pin
import socket
from network import LoRa
import binascii
import array
from machine import PWM
############### Weightsensor ###############
def main():
adc = machine.ADC() # create an ADC object
apin = adc.channel(pin='P16') # create an analog pin on P16
# 50% duty cycle at 38kHz.
pwm = PWM(3, frequency=78000) # use PWM timer 0, with a frequency of 5KHz
# create pwm channel on pin P12 with a duty cycle of 50%
pwm_c = pwm.channel(0, pin='P20')
while True:
val = apin() # read an analog value
if val < 20:
print(val)
print("Weight is good")
binaryString = bin(val)
print(binaryString)
time.sleep(5)
if val > 20:
print(val)
print("Weight is to high")
binaryString = bin(val)
print(binaryString)
pwm_c.freq(500)
time.sleep(5)
pwm_c.freq(0)
if __name__ == "__main__":
main()
我几乎完成了一个项目,但是当我给出一个很高的值时。我几乎听不到蜂鸣器的声音,因为它只播放了一秒钟。有谁知道怎么把蜂鸣器的声音放长一点?
import pycom
import machine
import time
from machine import Pin
import socket
from network import LoRa
import binascii
import array
from machine import PWM
############### Weightsensor ###############
def main():
while True:
adc = machine.ADC() # create an ADC object
apin = adc.channel(pin='P16') # create an analog pin on P16
val = apin() # read an analog value
if val < 20:
print(val)
print("Weight is good")
binaryString = bin(val)
print(binaryString)
time.sleep(5)
if val > 20:
print(val)
print("Weight is to high")
binaryString = bin(val)
print(binaryString)
# 50% duty cycle at 38kHz.
pwm = PWM(3, frequency=78000) # use PWM timer 0, with a frequency of 5KHz
# create pwm channel on pin P12 with a duty cycle of 50%
pwm_c = pwm.channel(0, pin='P20', duty_cycle=1.0)
pwm_c.duty_cycle(0.3) # change the duty cycle to 30%
time.sleep(5)
if __name__ == "__main__":
main()
蜂鸣器在第二个“if”语句中播放。
有人可以帮帮我吗?
亲切的问候
我在您的代码中发现了多个问题:为什么您在 while 循环中一次又一次地重新定义 adc
和 apin
?将它移到 while True
Duty
在你的 PWN 中不应该影响你的蜂鸣器的音高,所以我的建议是省略所有的职责设置,或者至少让它像 250(试验一下)
蜂鸣器的音调是您 PWM 声明中的 frequency
值。您可以稍后在某处使用 pwm.frequency(hertz)
更改它。或者通过手动输入命令在控制台中找到最佳工作设置。
会响多久根据time.sleep()
在我看来代码应该是这样的(没有在船上测试过):
import pycom
import machine
import time
from machine import Pin
import socket
from network import LoRa
import binascii
import array
from machine import PWM
############### Weightsensor ###############
def main():
adc = machine.ADC() # create an ADC object
apin = adc.channel(pin='P16') # create an analog pin on P16
# 50% duty cycle at 38kHz.
pwm = PWM(3, frequency=78000) # use PWM timer 0, with a frequency of 5KHz
# create pwm channel on pin P12 with a duty cycle of 50%
pwm_c = pwm.channel(0, pin='P20')
while True:
val = apin() # read an analog value
if val < 20:
print(val)
print("Weight is good")
binaryString = bin(val)
print(binaryString)
time.sleep(5)
if val > 20:
print(val)
print("Weight is to high")
binaryString = bin(val)
print(binaryString)
pwm_c.freq(500)
time.sleep(5)
pwm_c.freq(0)
if __name__ == "__main__":
main()