blackcheng 发表于 2015-7-23 22:44:20

关于tone函数

   我是个小白,刚刚接触Arduino,在练习用蜂鸣器的时候遇到了一个怪事,先上电路图。
然后程序void setup(){
pinMode(10,OUTPUT);
}
void loop(){
tone(10,400);
delay(1000);
tone(10,1000);
delay(1000);
tone(10,600);
delay(1000);
tone(10,300);
delay(1000);
}
蜂鸣器正常发出四个频率声音间隔一秒
可是如果将tone函数写成三个参数的话
void setup(){
pinMode(10,OUTPUT);
}
void loop(){
tone(10,400,1000);
tone(10,1000,1000);
tone(10,600,1000);
tone(10,300,1000);
}

应该结果是一样的。可是蜂鸣器发出的声音变成了只一个频率,而且我改了程序里的频率参数后还是只发这个声音,难道是板子坏了?(UNO的板子,网上买的)

我又逗了啊 发表于 2015-7-23 23:26:24

可能时间太长,频率变化不大,难以分辨吧

shihaipeng04 发表于 2015-7-23 23:37:30

依稀记得好像 tone(3个参数的时候,响了一个,就继续执行下面的程序了。 实际上你的程序,只运行了 tone(10.300,1000)的一句,前面3个都瞬间被覆盖了。好像是这样。你换下最后的频率,听听是不是变了。

血阳 发表于 2015-7-24 00:35:43

同样不解,感觉要么就是只响了第一个代码的,要么就是只响了最后一个的,你分别改改试试。感觉楼上说的有理。
你可以试试一个小程序,
tone(10,400,1000);
noTone(10)
看看他是响了1s。还是直接没响,如果没响的话,那就是楼上说的原因咯。

blackcheng 发表于 2015-7-24 17:31:55

又进行试验,发现如果只写一个tone函数(三个参数的形式,下同),是正常的。
但是如果写了两个及以上tone函数,表示持续时间的参数就不起作用了,只能发出一个频率,而且听着比正常的刺耳。

进阶的熊猫 发表于 2015-7-24 21:44:56

这两种写法肯定是不同的,我摘录了官网对tone函数的说明,你自己理解下:
1,The tone() command works by taking over one of the Atmega's internal timers, setting it to the frequency you want, and using the timer to pulse an output pin. Since it's only using one timer, you can only play one note at a time.
2,Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.:lol
页: [1]
查看完整版本: 关于tone函数