聽Arduino在唱歌~手工電子DIY教程
- 電子DIY
- 關注:2.62W次
製作時間: 2小時 製作難度: ★★★☆☆ GEEK指數: ★★★★☆Arduino,是一塊基於開放原始碼的USB介面Simple I/O介面板,或者可以詮釋為一種開源硬體和開源軟體相結合的創新,它能實現的功能相當豐富。在此之前的文章中,已經有不少利用Arduino實現的DIY作品,為使童鞋們能循序漸進的加深瞭解,我將介紹一些Arduino的基本應用。首先要介紹的是如何利用Arduino板發出簡單的聲音,即如何把Arduino板做成一個迷你的音訊發生器。
1 工具和材料○ 1個8歐姆的小揚聲器;○ 1塊Arduino實驗板;○ 1個按鈕;○ 1個阻值10k的電阻○ 以及導線若干。
這裡我用一塊Adafruit ProtoShield原型擴充套件板來搭建測試電路
2 如何產生音樂● 在開始動手製作之前,我們最好先提前執行一下音樂程式,這樣對搭建出的Arduino電路應該發出什麼音樂就能做到心中有數。你可以從Arduino整合開發環境中獲得相應的音樂程式。● 為方便起見我在這裡直接給出了一段音樂程式程式碼(都是基礎的C語言,別有壓力!)。 #include "pitches.h"// notes in the melody:int melody[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};// note durations: 4 = quarter note, 8 = eighth note, etc.:int noteDurations[] = {4, 8, 8, 4,4,4,4,4 };void setup() {// iterate over the notes of the melody:for (int thisNote = 0; thisNote// to calculate the note duration, take one second// divided by the note type.//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.int noteDuration = 1000/noteDurations[thisNote];tone(8, melody[thisNote],noteDuration);// to distinguish the notes, set a minimum time between them.// the note's duration + 30% seems to work well:int pauseBetweenNotes = noteDuration * 1.30;delay(pauseBetweenNotes);// stop the tone playing:noTone(8);}}void loop() {// no need to repeat the melody.}} ● 另外還需要一個檔案來定義基本音調,這個檔案也能在整合開發環境中找到,在此我也給出了這段程式碼,你只需將其儲存為.h格式,並放到程式目錄下即可: /***************** Public Constants ********************/#define NOTE_B0 31#define NOTE_C1 33#define NOTE_CS1 35#define NOTE_D1 37#define NOTE_DS1 39#define NOTE_E1 41#define NOTE_F1 44#define NOTE_FS1 46#define NOTE_G1 49#define NOTE_GS1 52#define NOTE_A1 55#define NOTE_AS1 58#define NOTE_B1 62#define NOTE_C2 65#define NOTE_CS2 69#define NOTE_D2 73#define NOTE_DS2 78#define NOTE_E2 82#define NOTE_F2 87#define NOTE_FS2 93#define NOTE_G2 98#define NOTE_GS2 104#define NOTE_A2 110#define NOTE_AS2 117#define NOTE_B2 123#define NOTE_C3 131#define NOTE_CS3 139#define NOTE_D3 147#define NOTE_DS3 156#define NOTE_E3 165#define NOTE_F3 175#define NOTE_FS3 185#define NOTE_G3 196#define NOTE_GS3 208#define NOTE_A3 220#define NOTE_AS3 233#define NOTE_B3 247#define NOTE_C4 262#define NOTE_CS4 277#define NOTE_D4 294#define NOTE_DS4 311#define NOTE_E4 330#define NOTE_F4 349#define NOTE_FS4 370#define NOTE_G4 392#define NOTE_GS4 415#define NOTE_A4 440#define NOTE_AS4 466#define NOTE_B4 494#define NOTE_C5 523#define NOTE_CS5 554#define NOTE_D5 587#define NOTE_DS5 622#define NOTE_E5 659#define NOTE_F5 698#define NOTE_FS5 740#define NOTE_G5 784#define NOTE_GS5 831#define NOTE_A5 880#define NOTE_AS5 932#define NOTE_B5 988#define NOTE_C6 1047#define NOTE_CS6 1109#define NOTE_D6 1175#define NOTE_DS6 1245#define NOTE_E6 1319#define NOTE_F6 1397#define NOTE_FS6 1480#define NOTE_G6 1568#define NOTE_GS6 1661#define NOTE_A6 1760#define NOTE_AS6 1865#define NOTE_B6 1976#define NOTE_C7 2093#define NOTE_CS7 2217#define NOTE_D7 2349#define NOTE_DS7 2489#define NOTE_E7 2637#define NOTE_F7 2794#define NOTE_FS7 2960#define NOTE_G7 3136#define NOTE_GS7 3322#define NOTE_A7 3520#define NOTE_AS7 3729#define NOTE_B7 3951#define NOTE_C8 4186#define NOTE_CS8 4435#define NOTE_D8 4699#define NOTE_DS8 4978 ● 按照上圖所示連線揚聲器並燒入程式,電源正極連線8腳,負極連線接地腳,不出意外的話,這樣就能聽到一小段曲子了。● 如果想嘗試換成其他音樂,還可以從 這裡 下載相應程式碼。 3 Tone函式● Arduino板上產生的音樂由tone()函式控制,改動該函式即可改變音樂旋律,程式碼有兩種形式分別為:tone(pin, frequency, duration)或tone(pin, frequency)● 第一個函式中,pin代表連線揚聲器的管腳,frequency代表發聲頻率,duration代表持續的時間,單位是毫秒。● 如果用第二個函式,則還需另外的noTone()函式來控制音樂的停止noTone(pin)。● 在上一步音樂程式中,還需包括一個pitches.h檔案。該檔案的作用是根據聲音訊率改變自動得出相應變數,這樣我們就無需自己計算基本音調,有了這個功能,我們這些菜鳥級的作曲家也有機會推出自己的作品了。 4 製作簡易鍵盤 ● 接下來我們要製作一個簡易的單鍵鍵盤,這一步工作主要是把電路各個部分組合起來,並使其在按鍵控制下正常發聲。● 下面兩張圖片分別是實際接線圖和接線示意圖。實際接線圖
接線示意圖
● 下面是單個音調的程式程式碼,將其新增到tone函式中,另外千萬不要忘記pitches.h檔案哦(除非哪位蛋疼哥想親自動手把赫茲換算成音調):#include "pitches.h"const int buttonPin = 2; // the number of the pushbutton pinint note1 = NOTE_C4; // define note sound// variables will change:int buttonState = 0; // variable for reading the pushbutton status void setup() {// initialize the pushbutton pin as an input:pinMode(buttonPin, INPUT);}void loop(){// read the state of the pushbutton value:buttonState = digitalRead(buttonPin);// check if the pushbutton is pressed.// if it is, the buttonState is HIGH:if (buttonState == HIGH) {// sound tonetone(8, note1);}else {//turn off soundnoTone(8);}} ● 連線好電路並載入上面這段程式碼,按下按鍵後聽到的聲音應該是C調。下面我們再來看段視訊以進一步加深理解: 5 震撼出籠!● 作品揭曉的時刻到啦!看愛出鏡的作者自HIGH的摸樣是不是很有感染力?! ● 如果你覺得音調不夠豐富,想新增更多音調,那麼只需繼續增加按鍵和相應程式碼。 ● 讓我們來期待更多的arduino基本應用吧!- 文章版權屬於文章作者所有,轉載請註明 https://zhizuowo.com/shenghuo/dianzidiy/e0kve.html