资料介绍
描述
问题
最初,我的任务是为学校项目创建一个 Arduino UNO 闹钟。闹钟包括一个显示时间和菜单的触摸显示屏、一个在闹钟响起时播放所选铃声的蜂鸣器、一个电源和一个外壳。让闹钟正确播放铃声是该项目的主要挑战之一。
起初,我很容易在互联网上找到一些播放旋律的示例。它主要是互联网上流传的相同代码的略有不同的版本,使用延迟来播放每个音符的时间。但是,我很快注意到将该代码应用于我的项目时存在两个问题:
- 由于代码使用了延迟,因此闹钟在播放旋律时无法轻易接受用户输入。但是,例如,需要输入来阻止闹钟响起。
- 闹钟提供各种铃声,存储在数组中。这些会占用大量可用的 SRAM(可变内存)。如果没有足够的可用 SRAM,Arduino 将产生随机行为。
所以我最终编写了自己的代码来解决这些问题。
解决方案
我让我的 Arduino UNO 在运行其他代码的同时播放音频并减少内存消耗。对于第一部分,我使用线程,对于第二部分,我将旋律存储在程序内存 ( PROGMEM
) 而不是变量内存中。查看下一章以获得进一步的解释。
生成的源代码可以在以下 Github 存储库中找到:https ://github.com/jschneibel/tiny-tune 。在那里,如果你想在你的项目中使用它,你可以仔细查看源代码并下载它。为了运行该程序,编译以下文件并将其上传到您的 Arduino:
-
tiny-tune.ino
-
tunes.ino
-
pitches.h
-
libraries/ArduinoThread
(Ivan Seidel 的图书馆)
用于playTune()
开始循环播放实施的样本曲调。用于cancelTune()
阻止它播放。编辑getTuneData()
以tunes.ino
更改样本曲调或添加您自己的曲调。
该代码已经在 Arduino UNO 上进行了测试。
怎么运行的
本章仅显示代码中最重要的部分。检查上一章以获得完整的源代码并正确设置它。
音频在 Ivan Seidel 的 ArduinoThread 库的线程上播放。线程在文件中设置tiny-tune.ino
,其代码很简单:
// tiny-tune.ino
// ... additional setup code here (see Github repository for full code).
ThreadController threadController = ThreadController();
Thread tuneThread = Thread(); // Our thread playing audio.
// Callback function for tuneThread.
void tuneCallback() {
playCurrentNote(); // See file tunes.ino.
}
void setup() {
// Configure threads.
tuneThread.onRun(tuneCallback);
tuneThread.setInterval(100);
tuneThread.enabled = false;
threadController.add(&tuneThread);
// Start playing the tune (see file tunes.ino).
playTune();
// Additional code can be run here.
}
void loop() {
noInterrupts();
// Run threads in threadController.
threadController.run();
interrupts();
// Additional code can be run here.
}
playTune()
是实际开始播放旋律的命令。它通过运行线程来做到这一点,而线程又调用playCurrentNote()
. playCurrentNote()
定义于tunes.ino
并一次播放一个音符或暂停:
// tunes.ino
// ... additional code here (see Github repository for full code).
// Play a single note or pause of the tune.
void playCurrentNote() {
if(playPauseNext == false) { // if a note should be played
// Read note and note duration from program memory and
// store them in global variables currentNote and
// currentNoteDuration.
getTuneData(currentNoteIndex);
// There has to be a short pause between notes, otherwise
// the tune will not play smoothly.
// Feel free to experiment with this.
pauseBetweenNotes = currentNoteDuration * 0.30;
// Play note (the code will keep executing without delay).
tone(BUZZER_PIN, currentNote, currentNoteDuration);
// Repeat tune from the beginning after maxNoteIndex
// (end of tune) has been reached.
if (currentNoteIndex == maxNoteIndex) currentNoteIndex = 0;
else currentNoteIndex++;
// Call tuneThread again when the current note has
// finished playing.
tuneThread.setInterval(currentNoteDuration);
// After the current note, a pause will be played.
playPauseNext = true;
}
else { // if a pause should be played
noTone(BUZZER_PIN);
// Call tuneThread again when the current pause
// has finished playing.
tuneThread.setInterval(pauseBetweenNotes);
// After this pause, a note will be played.
playPauseNext = false;
}
}
您可以在这里看到该函数一次只播放一个音符。这是必要的,以减少可变存储器或 SRAM 的消耗。全局变量currentNote
和currentNoteDuration
实际上是缓冲区变量,在变量内存中只保存整个旋律中的一个音符。
完整的旋律本身存储在程序存储器 ( PROGMEM
) 中,稍后您将看到。顾名思义,程序存储器是存储程序的地方,它与变量存储器是分开的。程序存储器的内容无法在运行时更改。这意味着旋律必须是恒定的,在 Arduino 运行时不能合成或计算它们。当您将程序上传到您的 Arduino 时,必须定义它们。
该函数getMelodyData(byte index)
从程序存储器中读取旋律数组中指定索引处的音符,并将其存储在全局缓冲区变量currentNote
中currentNoteDuration
:
// tunes.ino
// ... additional code here (see Github repository for full code).
// Edit getTuneData() to change the sample tune or add your own tunes.
void getTuneData(byte index)
{
// The notes and their durations are stored in PROGMEM
// (program memory aka flash memory):
const static uint16_t sample_tune[] PROGMEM = {
NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5,
NOTE_B5, NOTE_C6, NOTE_B5, NOTE_A5, NOTE_G5, NOTE_F5,
NOTE_E5, NOTE_D5, NOTE_C5};
const static byte sample_tempo[] PROGMEM = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8};
// The currentNote and currentNoteDuration are global
// buffer variables so that loading the notes and their
// durations of the tune won't use up all our SRAM
// (in case the tune is very long):
// Read current note from program memory.
currentNote = pgm_read_word_near(sample_tune + index);
// Read current note duration from program memory and
// convert to milliseconds.
currentNoteDuration = 1500/pgm_read_byte_near(sample_tempo + index);
// Read max index of array in program memory.
maxNoteIndex = sizeof(sample_tune) / sizeof(sample_tune[0]) - 1;
}
然后,缓冲的音符将被播放,下一个音符进入缓冲区。这已经是它了!如果你想改变旋律或添加更多(你可以在互联网上找到几个),你可以修改功能。getTuneData(byte index)
如果您可以在您的项目中使用我的代码,如果您有任何问题或发现错误,请告诉我!谢谢阅读。
- 使用Arduino Uno和基本组件创建自动泡泡皂机 0次下载
- 基于Arduino UNO的闹钟 0次下载
- 基于Arduino UNO的细丝计数器 0次下载
- 基于Arduino Uno的RC车 1次下载
- Arduino DIY闹钟 0次下载
- Arduino闹钟项目 0次下载
- Arduino UNO的扩展板创建的电子键屏蔽 0次下载
- Arduino Uno国内改版电路原理图下载 0次下载
- Arduino Uno Rev.3开发板意大利原版电路图 0次下载
- Arduino UNO数据手册 49次下载
- Arduino Files的Arduino Uno R3电路原理图免费下载 86次下载
- Arduino UNO的电路原理图免费下载 211次下载
- arduino uno电路设计原理图 395次下载
- arduino_Uno_Rev3-02-TH 0次下载
- Arduino_Uno_Rev3-schematic 59次下载
- 小安派-UNO-ET485 Arduino开发板简介 428次阅读
- 这么漂亮的Arduino开发板你见过吗? 1015次阅读
- 如何创建基于DCO的音频合成器 588次阅读
- 如何创建一个由时间开关电池供电的太阳能充电电路 1962次阅读
- 如何利用Arduino UNO和SD卡制作音乐播放器 6302次阅读
- 如何使用Arduino Uno和HC-06实现智能手机对电灯的控制 3917次阅读
- 如何利用Arduino创建一个电机滑动门 1334次阅读
- 基于Arduino UNO和HC-05蓝牙模块控制伺服电机 4330次阅读
- 如何使用Arduino创建停车门禁控制系统? 4689次阅读
- 微雪电子Arduino开发板UNO PLUS简介 4881次阅读
- 微雪电子BB Black扩展板|Arduino功能扩展简介 1268次阅读
- 源创通信BPI-UNO32 arduino 开发板简介 1345次阅读
- dfrobotArduino UNO R3 介绍 5291次阅读
- 详解Arduino Uno控制直流电机之应用 2w次阅读
- 用Arduino Uno开发板和一块面包板就能轻松搞定这一切 2.3w次阅读
下载排行
本周
- 1储能电源市场分析
- 7.99 MB | 8次下载 | 免费
- 2储能电源市场分析报告
- 2.61 MB | 6次下载 | 免费
- 3磁环电感定制时应该注意什么
- 0.32 MB | 2次下载 | 免费
- 4labview文档教程资料(一)
- 24.29 MB | 2次下载 | 免费
- 5轻触三功能+常按 SOS 功能手筒LED驱动ICSD3302数据手册
- 0.60 MB | 1次下载 | 2 积分
- 6英集芯IP5353 QFN32 规格书pdf
- 2.70 MB | 1次下载 | 免费
- 7NCV78763R1DAGEVB子板数据手册
- 533.41 KB | 次下载 | 免费
- 8500mA,高效 MicroSiP™ 降压转换器TPS8269xSIP数据表
- 918.92KB | 次下载 | 免费
本月
- 1ES9038PRO解码芯片的电路原理图介绍
- 0.25 MB | 35次下载 | 5 积分
- 2STM32国内外发展现状
- 1.15 MB | 13次下载 | 免费
- 3传感芯片选型指南
- 3.60 MB | 11次下载 | 免费
- 4ATmega8芯片中文手册
- 2.45 MB | 8次下载 | 1 积分
- 5储能电源市场分析
- 7.99 MB | 8次下载 | 免费
- 6TDK电容器产品指南
- 11.88 MB | 7次下载 | 1 积分
- 72A多电池高效开关充电器AN_SY6912A中文资料规格书
- 1.43 MB | 7次下载 | 免费
- 8储能电源市场分析报告
- 2.61 MB | 6次下载 | 免费
总榜
- 1matlab软件下载入口
- 未知 | 935086次下载 | 免费
- 2开源硬件-PMP21529.1-4 开关降压/升压双向直流/直流转换器 PCB layout 设计
- 1.48MB | 420050次下载 | 免费
- 3Altium DXP2002下载入口
- 未知 | 233068次下载 | 免费
- 4电路仿真软件multisim 10.0免费下载
- 340992 | 191317次下载 | 免费
- 5十天学会AVR单片机与C语言视频教程 下载
- 158M | 183316次下载 | 免费
- 6labview8.5下载
- 未知 | 81567次下载 | 免费
- 7Keil工具MDK-Arm免费下载
- 0.02 MB | 73788次下载 | 免费
- 8NI LabVIEW中实现3D视觉的工具和技术
- 未知 | 70088次下载 | 免费
评论
查看更多