Arduino扬声器发出长时间连续的噪音

时间:2019-03-31 00:55:52

标签: python arduino arduino-uno speaker

根据我的代码,Arduino扬声器似乎无法正常工作。它并没有关闭声音。

我正在尝试运行以下代码。但是,当我按下按钮时,我得到的只是一种长而连续的声音。当我改变频率时,音调也会改变,因此似乎可以正确读取频率,但是当我调节电位器时,它不会改变频率。

我还尝试运行一段较小的代码,该代码只应播放声音1秒钟。当我运行代码时,扬声器具有相同的结果:扬声器发出一种声音,直到我拔下Arduino的插头才停止。

为什么会这样,我该如何解决?

board = PyMata3(com_port="com5")
pot = Pot(board, PIN_POT, 10) 
spkr = Speaker(board, PIN_SPEAKER)
led = Led(board, PIN_LED0)
button = Button(board, PIN_SW0, activeLevel=0, internalPull=1)

while True:
    if button.isPressed():
        if pot.fraction() < 0.3:
            spkr.play(50)
        if 0.3 <= pot.fraction() < 0.65:
            spkr.play(100)
        if 0.65 <= pot.fraction() <= 1.0:
            spkr.play(200)

我希望扬声器随着电位计的调整而改变频率。

1 个答案:

答案 0 :(得分:0)

您刚刚调用了play方法。停止/暂停在哪里?