九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 4998|回复: 0
打印 上一主题 下一主题

arduino ibox项目实战17-tonemelody实验

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-28 09:56:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,喇叭或蜂鸣器一个
软件平台:WIN7操作系统,android4.0android4.4系统
实验目标:通过iboxPWM口控制喇叭或蜂鸣器实现播放音乐的功能。
打开arduino的IDE开发工具,依次点击文件->示例->02.Digital->toneMelody,toneMelody的示例程序将会被打开,其源码如下:
  1. #include "pitches.h"

  2. // notes in the melody:
  3. int melody[] = {
  4.   NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
  5. };

  6. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  7. int noteDurations[] = {
  8.   4, 8, 8, 4, 4, 4, 4, 4
  9. };

  10. void setup() {
  11. }

  12. void loop() {
  13.   // no need to repeat the melody.

  14.   // iterate over the notes of the melody:
  15.   for (int thisNote = 0; thisNote < 8; thisNote++) {

  16.     // to calculate the note duration, take one second
  17.     // divided by the note type.
  18.     //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  19.     int noteDuration = 1000 / noteDurations[thisNote];
  20.     tone(PWM0, melody[thisNote], noteDuration);

  21.     // to distinguish the notes, set a minimum time between them.
  22.     // the note's duration + 30% seems to work well:
  23.     int pauseBetweenNotes = noteDuration * 1.30;
  24.     delay(pauseBetweenNotes);
  25.     // stop the tone playing:
  26.     noTone(PWM0);
  27.   }
  28. }
复制代码
将喇叭或蜂鸣器通过杜绑线接到ibox扩展板的PWM口,点击上传按钮运行程序,喇叭或蜂鸣器将会有音乐播出。
回复

使用道具 举报

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

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-4-26 18:14 , Processed in 0.018851 second(s), 18 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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