风筝
发表于: 2016-8-8 15:24:01 | 显示全部楼层

播放音调(Playing tones on Multiple outputs using the tone() function)

本示例展示了如何使用tone()命令在多个输出引脚播放不同音符。


tone()工作时占用一个Atmega内部定时器,设置定时器为想要的频率,并使用该定时器在输出引脚输出脉冲。由于仅使用了一个定时器,所以只能同时播放一个音符。但是可以在不用引脚依次播放音符。要实现这一点,需要在移动之前关闭定时器计数。


所需硬件

-    Arduino或者Genuino开发板

-    蜂鸣器或扬声器

-   3个100欧电阻

-    导线

-   面包板


电路连接方式

arduino_3_speakers_bb.png

将扬声器的一端依次连接到Arduino的数字引脚,另一端连接到地。


原理图

arduino_3_speakers_schem.png


代码

下面的程序在每个扬声器依次播放一个音符,播放音符前先关断前一个扬声器。注意每个音符的持续时间和紧跟着的延时时间一样。

以下是主要的程序:

  1. /*
  2.   Multiple tone player

  3. Plays multiple tones on multiple pins in sequence

  4. circuit:
  5. * 3 8-ohm speaker on digital pins 6, 7, and 8

  6. created 8 March 2010
  7. by Tom Igoe
  8. based on a snippet from Greg Borenstein

  9. This example code is in the public domain.

  10. http://www.arduino.cc/en/Tutorial/Tone4

  11. */

  12. void setup() {

  13. }

  14. void loop() {
  15.   // turn off tone function for pin 8:
  16.   noTone(8);
  17.   // play a note on pin 6 for 200 ms:
  18.   tone(6, 440, 200);
  19.   delay(200);

  20.   // turn off tone function for pin 6:
  21.   noTone(6);
  22.   // play a note on pin 7 for 500 ms:
  23.   tone(7, 494, 500);
  24.   delay(500);

  25.   // turn off tone function for pin 7:
  26.   noTone(7);
  27.   // play a note on pin 8 for 500 ms:
  28.   tone(8, 523, 300);
  29.   delay(300);
  30. }
复制代码


跳转到指定楼层
gyg12321
发表于: 2016-8-16 18:15:48 | 显示全部楼层

学习了!!!!!!!!!!!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1479



手机版|

GMT+8, 2024-3-29 19:31 , Processed in 0.111393 second(s), 8 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表