电子发烧友App

硬声App

0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示
电子发烧友网>电子资料下载>电子资料>创建一个Arduino UNO闹钟

创建一个Arduino UNO闹钟

2022-12-27 | zip | 0.39 MB | 次下载 | 免费

资料介绍

描述

问题

最初,我的任务是为学校项目创建一个 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 的消耗。全局变量currentNotecurrentNoteDuration实际上是缓冲区变量,在变量内存中只保存整个旋律中的一个音符。

完整的旋律本身存储在程序存储器 ( PROGMEM) 中,稍后您将看到。顾名思义,程序存储器是存储程序的地方,它与变量存储器是分开的。程序存储器的内容无法在运行时更改。这意味着旋律必须是恒定的,在 Arduino 运行时不能合成或计算它们。当您将程序上传到您的 Arduino 时,必须定义它们。

该函数getMelodyData(byte index)从程序存储器中读取旋律数组中指定索引处的音符,并将其存储在全局缓冲区变量currentNotecurrentNoteDuration

// 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)

如果您可以在您的项目中使用我的代码,如果您有任何问题或发现错误,请告诉我!谢谢阅读。


下载该资料的人也在下载 下载该资料的人还在阅读
更多 >

评论

查看更多

下载排行

本周

  1. 1储能电源市场分析
  2. 7.99 MB  |  8次下载  |  免费
  3. 2储能电源市场分析报告
  4. 2.61 MB   |  6次下载  |  免费
  5. 3磁环电感定制时应该注意什么
  6. 0.32 MB   |  2次下载  |  免费
  7. 4labview文档教程资料(一)
  8. 24.29 MB   |  2次下载  |  免费
  9. 5轻触三功能+常按 SOS 功能手筒LED驱动ICSD3302数据手册
  10. 0.60 MB   |  1次下载  |  2 积分
  11. 6英集芯IP5353 QFN32 规格书pdf
  12. 2.70 MB  |  1次下载  |  免费
  13. 7NCV78763R1DAGEVB子板数据手册
  14. 533.41 KB   |  次下载  |  免费
  15. 8500mA,高效 MicroSiP™ 降压转换器TPS8269xSIP数据表
  16. 918.92KB   |  次下载  |  免费

本月

  1. 1ES9038PRO解码芯片的电路原理图介绍
  2. 0.25 MB   |  35次下载  |  5 积分
  3. 2STM32国内外发展现状
  4. 1.15 MB   |  13次下载  |  免费
  5. 3传感芯片选型指南
  6. 3.60 MB   |  11次下载  |  免费
  7. 4ATmega8芯片中文手册
  8. 2.45 MB   |  8次下载  |  1 积分
  9. 5储能电源市场分析
  10. 7.99 MB  |  8次下载  |  免费
  11. 6TDK电容器产品指南
  12. 11.88 MB   |  7次下载  |  1 积分
  13. 72A多电池高效开关充电器AN_SY6912A中文资料规格书
  14. 1.43 MB   |  7次下载  |  免费
  15. 8储能电源市场分析报告
  16. 2.61 MB   |  6次下载  |  免费

总榜

  1. 1matlab软件下载入口
  2. 未知  |  935086次下载  |  免费
  3. 2开源硬件-PMP21529.1-4 开关降压/升压双向直流/直流转换器 PCB layout 设计
  4. 1.48MB  |  420050次下载  |  免费
  5. 3Altium DXP2002下载入口
  6. 未知  |  233068次下载  |  免费
  7. 4电路仿真软件multisim 10.0免费下载
  8. 340992  |  191317次下载  |  免费
  9. 5十天学会AVR单片机与C语言视频教程 下载
  10. 158M  |  183316次下载  |  免费
  11. 6labview8.5下载
  12. 未知  |  81567次下载  |  免费
  13. 7Keil工具MDK-Arm免费下载
  14. 0.02 MB  |  73788次下载  |  免费
  15. 8NI LabVIEW中实现3D视觉的工具和技术
  16. 未知  |  70088次下载  |  免费