電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>創(chuàng)建一個Arduino UNO鬧鐘

創(chuàng)建一個Arduino UNO鬧鐘

2022-12-27 | zip | 0.39 MB | 次下載 | 免費(fèi)

資料介紹

描述

問題

最初,我的任務(wù)是為學(xué)校項目創(chuàng)建一個 Arduino UNO 鬧鐘。鬧鐘包括一個顯示時間和菜單的觸摸顯示屏、一個在鬧鐘響起時播放所選鈴聲的蜂鳴器、一個電源和一個外殼。讓鬧鐘正確播放鈴聲是該項目的主要挑戰(zhàn)之一。

起初,我很容易在互聯(lián)網(wǎng)上找到一些播放旋律的示例。它主要是互聯(lián)網(wǎng)上流傳的相同代碼的略有不同的版本,使用延遲來播放每個音符的時間。但是,我很快注意到將該代碼應(yīng)用于我的項目時存在兩個問題:

  • 由于代碼使用了延遲,因此鬧鐘在播放旋律時無法輕易接受用戶輸入。但是,例如,需要輸入來阻止鬧鐘響起。
  • 鬧鐘提供各種鈴聲,存儲在數(shù)組中。這些會占用大量可用的 SRAM(可變內(nèi)存)。如果沒有足夠的可用 SRAM,Arduino 將產(chǎn)生隨機(jī)行為。

所以我最終編寫了自己的代碼來解決這些問題。

解決方案

我讓我的 Arduino UNO 在運(yùn)行其他代碼的同時播放音頻并減少內(nèi)存消耗。對于第一部分,我使用線程,對于第二部分,我將旋律存儲在程序內(nèi)存 ( PROGMEM) 而不是變量內(nèi)存中。查看下一章以獲得進(jìn)一步的解釋。

生成的源代碼可以在以下 Github 存儲庫中找到:https ://github.com/jschneibel/tiny-tune 。在那里,如果你想在你的項目中使用它,你可以仔細(xì)查看源代碼并下載它。為了運(yùn)行該程序,編譯以下文件并將其上傳到您的 Arduino:

  • tiny-tune.ino
  • tunes.ino
  • pitches.h
  • libraries/ArduinoThread(Ivan Seidel 的圖書館)

用于playTune()開始循環(huán)播放實施的樣本曲調(diào)。用于cancelTune()阻止它播放。編輯getTuneData()tunes.ino更改樣本曲調(diào)或添加您自己的曲調(diào)。

該代碼已經(jīng)在 Arduino UNO 上進(jìn)行了測試。

怎么運(yùn)行的

本章僅顯示代碼中重要的部分。檢查上一章以獲得完整的源代碼并正確設(shè)置它。

音頻在 Ivan Seidel 的 ArduinoThread 庫的線程上播放。線程在文件中設(shè)置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()是實際開始播放旋律的命令。它通過運(yùn)行線程來做到這一點,而線程又調(diào)用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;
    }
}

您可以在這里看到該函數(shù)一次只播放一個音符。這是必要的,以減少可變存儲器或 SRAM 的消耗。全局變量currentNotecurrentNoteDuration實際上是緩沖區(qū)變量,在變量內(nèi)存中只保存整個旋律中的一個音符。

完整的旋律本身存儲在程序存儲器 ( PROGMEM) 中,稍后您將看到。顧名思義,程序存儲器是存儲程序的地方,它與變量存儲器是分開的。程序存儲器的內(nèi)容無法在運(yùn)行時更改。這意味著旋律必須是恒定的,在 Arduino 運(yùn)行時不能合成或計算它們。當(dāng)您將程序上傳到您的 Arduino 時,必須定義它們。

該函數(shù)getMelodyData(byte index)從程序存儲器中讀取旋律數(shù)組中指定索引處的音符,并將其存儲在全局緩沖區(qū)變量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;
}

然后,緩沖的音符將被播放,下一個音符進(jìn)入緩沖區(qū)。這已經(jīng)是它了!如果你想改變旋律或添加更多(你可以在互聯(lián)網(wǎng)上找到幾個),你可以修改功能。getTuneData(byte index)

如果您可以在您的項目中使用我的代碼,如果您有任何問題或發(fā)現(xiàn)錯誤,請告訴我!謝謝閱讀。


下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1山景DSP芯片AP8248A2數(shù)據(jù)手冊
  2. 1.06 MB  |  532次下載  |  免費(fèi)
  3. 2RK3399完整板原理圖(支持平板,盒子VR)
  4. 3.28 MB  |  339次下載  |  免費(fèi)
  5. 3TC358743XBG評估板參考手冊
  6. 1.36 MB  |  330次下載  |  免費(fèi)
  7. 4DFM軟件使用教程
  8. 0.84 MB  |  295次下載  |  免費(fèi)
  9. 5元宇宙深度解析—未來的未來-風(fēng)口還是泡沫
  10. 6.40 MB  |  227次下載  |  免費(fèi)
  11. 6迪文DGUS開發(fā)指南
  12. 31.67 MB  |  194次下載  |  免費(fèi)
  13. 7元宇宙底層硬件系列報告
  14. 13.42 MB  |  182次下載  |  免費(fèi)
  15. 8FP5207XR-G1中文應(yīng)用手冊
  16. 1.09 MB  |  178次下載  |  免費(fèi)

本月

  1. 1OrCAD10.5下載OrCAD10.5中文版軟件
  2. 0.00 MB  |  234315次下載  |  免費(fèi)
  3. 2555集成電路應(yīng)用800例(新編版)
  4. 0.00 MB  |  33566次下載  |  免費(fèi)
  5. 3接口電路圖大全
  6. 未知  |  30323次下載  |  免費(fèi)
  7. 4開關(guān)電源設(shè)計實例指南
  8. 未知  |  21549次下載  |  免費(fèi)
  9. 5電氣工程師手冊免費(fèi)下載(新編第二版pdf電子書)
  10. 0.00 MB  |  15349次下載  |  免費(fèi)
  11. 6數(shù)字電路基礎(chǔ)pdf(下載)
  12. 未知  |  13750次下載  |  免費(fèi)
  13. 7電子制作實例集錦 下載
  14. 未知  |  8113次下載  |  免費(fèi)
  15. 8《LED驅(qū)動電路設(shè)計》 溫德爾著
  16. 0.00 MB  |  6656次下載  |  免費(fèi)

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費(fèi)
  3. 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
  4. 78.1 MB  |  537798次下載  |  免費(fèi)
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費(fèi)
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費(fèi)
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費(fèi)
  11. 6電路仿真軟件multisim 10.0免費(fèi)下載
  12. 340992  |  191187次下載  |  免費(fèi)
  13. 7十天學(xué)會AVR單片機(jī)與C語言視頻教程 下載
  14. 158M  |  183279次下載  |  免費(fèi)
  15. 8proe5.0野火版下載(中文版免費(fèi)下載)
  16. 未知  |  138040次下載  |  免費(fèi)