arduino 習作工坊 - lesson 3 電音之夜

39
Arduino劳∽峩╤Plus MakerBar Taipei Workshop 卓櫔ℬ⯽!31261819

Upload: cavedu-education

Post on 21-Apr-2017

2.134 views

Category:

Devices & Hardware


0 download

TRANSCRIPT

Arduino PlusMakerBar Taipei Workshop

聲音波形

模擬波形

聲音頻率頻率,單位為赫茲 (括號內為半⾳音距離,"(0)"為中央C)

⼋八度 0 1 2 3 4 5 6 7 8 9

C 16.352  (−48)

32.703  (−36)

65.406  (−24)

130.81  (−12)

261.63  (0)

523.25  (+12)

1046.5  (+24)

2093.0  (+36)

4186.0  (+48)

8372.0  (+60)

D 18.354  (−46)

36.708  (−34)

73.416  (−22)

146.83  (−10)

293.66  (+2)

587.33  (+14)

1174.7  (+26)

2349.3  (+38)

4698.6  (+50)

9397.3  (+62)

E 20.602  (−44)

41.203  (−32)

82.407  (−20)

164.81  (−8)

329.63  (+4)

659.26  (+16)

1318.5  (+28)

2637.0  (+40)

5274.0  (+52)

10548  (+64)

F 21.827  (−43)

43.654  (−31)

87.307  (−19)

174.61  (−7)

349.23  (+5)

698.46  (+17)

1396.9  (+29)

2793.8  (+41)

5587.7  (+53)

11175  (+65)

G 24.500  (−41)

48.999  (−29)

97.999  (−17)

196.00  (−5)

392.00  (+7)

783.99  (+19)

1568.0  (+31)

3136.0  (+43)

6271.9  (+55)

12544  (+67)

A 27.500  (−39)

55.000  (−27)

110.00  (−15)

220.00  (−3)

440.00  (+9)

880.00  (+21)

1760.0  (+33)

3520.0  (+45)

7040.0  (+57)

14080  (+69)

B 30.868  (−37)

61.735  (−25)

123.47  (−13)

246.94  (−1)

493.88  (+11)

987.77  (+23)

1975.5  (+35)

3951.1  (+47)

7902.1  (+59)

15804  (+71)

MIDI樂器數位介面

音樂Music

MIDI

• 樂器數位介面(Musical Instrument Digital Interface,簡稱MIDI)是一個工業標準的電子通訊協定,為電子樂器等演奏裝置(如合成器)定義各種音符或彈奏碼,容許電子樂器、電腦、手機或其它的舞台演出配備彼此連接,調整和同步,得以即時交換演奏資料。

• MIDI不傳送聲音,只傳送像是音調和音樂強度的資料,音量,顫音和相位等參數的控制訊號,還有設定節奏的時鐘信號。在不同的電腦上,輸出的聲音也因音源器不同而有差異。

揚聲器

揚聲器播放A調,其頻率為440Hz,即每秒振動440次,揚聲器輸出440Hz的交流電,每秒440次電流改變。當電線圈與揚聲器薄膜⼀一起振動,推動周圍的空氣振動,揚聲器由此產⽣生聲⾳音。

揚聲器把電流頻率轉換成聲⾳音。

MIDI範例EX1

電路圖

程式EX1void setup() {

}

void loop() { tone(6, 440, 200); delay(200);

noTone(6); }

Pin6播放「A」midi音, 持續0.2秒

tone(pin,  frequency,  duration)

#include ”pitches.h”

void setup() {

}

新增

tone(6, NOTE_A4, 200); delay(200);

更改

14

• Input A0電位器:0 ~ 1023 • Output 頻率:100 ~ 2000

• int x = map(analogRead(A0), 0, 1023, 100, 2000);

• tone(6, x , 200);

EX2_1

EX2_1

15

void  setup(){    Serial.begin(9600);    }  

void  loop()  {      int  sensor  =  analogRead(A0);      int  x  =  map(analogRead(A0),  0,  1023,  100,  2000);      tone(6,  x  ,  200);        Serial.println(x);  }//end  loop  

MIDI範例EX2_2

17

程式EX2_2void  setup(){    Serial.begin(9600);    }  

void  loop()  {      int  sensor  =  analogRead(A0);      if(sensor  >  500  &&  sensor  <  800)        {              tone(6,  440,  200);          delay(200);          noTone(6);          Serial.println(sensor);      }        

else  if  (sensor  >  0  &&  sensor  <  500){          tone(6,  220,  200);          delay(200);          noTone(6);          Serial.println(sensor);      }      else{            tone(6,  880,  200);          delay(200);          noTone(6);        Serial.println(sensor);        }  }//end  loop

practice time-副程式void  setup(){    Serial.begin(9600);    }  void  loop()  {      int  sensor  =  analogRead(A0);      if(sensor  >  500  &&  sensor  <  800)        {              tone(6,  440,  200);          delay(200);          noTone(6);          Serial.println(sensor);      }        

       else  if  (sensor  >  0  &&  sensor  <  500){          tone(6,  220,  200);          delay(200);          noTone(6);          Serial.println(sensor);      }      else{            play();            Serial.println(  sensor);        }  }//end  loop  void  play()  {          tone(6,  880,  200);          delay(200);          noTone(6);  }

19

程式EX2_3 : 加入play(int freq)void  setup(){    Serial.begin(9600);    }  

void  loop()  {      int  sensor  =  analogRead(A0);      if(sensor  >  500  &&  sensor  <  800)        {              play(440);          Serial.println(sensor);      }          else  if  (sensor  >  0  &&  sensor  <  500){          play(220);          Serial.println(sensor);      }      else{        play(880);        Serial.println(sensor);        }  }

void  play(int  freq)  {          tone(6,  freq,  200);          delay(200);          noTone(6);  }

MIDI樂器數位介面

音樂Music

SD Shield

SPI?

串列外設介面(Serial Peripheral Interface Bus,SPI),類似I²C,是一種4線同步序列資料協定,適用於可攜式裝置平台系統,但使用率較I²C少。串列外設介面一般是4線,有時亦可為3線,有別於I²C的2線,以及1-Wire。

新增函式庫

• 將「SimpleSDAudio」移至\arduino\libraries裡

音樂轉檔(WAV→AFM)

27

\SimpleSDAudio\tools\Arduino with 16 MHz\converted

放⼊入SD卡的最外層

撥放音樂EX3

電路圖

注意順序!!!!

#include <SimpleSDAudio.h>

void setup() { SdPlay.setSDCSPin(10); SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_STEREO |SSDA_MODE_AUTOWORKER); if(!SdPlay.setFile(“A16.AFM”)) { Serial.println(F(" not found on card! Error code: ")); Serial.println(SdPlay.getLastError()); while(1); } else { Serial.println(F("found.")); SdPlay.play(); } }

音樂撥放器EX4

電路圖

確認SD卡if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER)) { Serial.println(F("initialization failed. Things to check:")); Serial.println(F("* is a card is inserted?")); Serial.println(F("* Is your wiring correct?")); Serial.println(F("* maybe you need to change the chipSelect pin to match your shield or module?")); Serial.print(F("Error code: ")); Serial.println(SdPlay.getLastError()); while(1); }

讀取SD卡內部資料

Serial.println(F("Files on card:"));

SdPlay.dir(&DirCallback);

選擇檔案ReEnter: count = 0; Serial.println(F("\r\nEnter filename (send newline after input):")); do { while(!Serial.available()) ; c = Serial.read(); ……. if(!SdPlay.setFile(AudioFileName)) { Serial.println(F(" not found on card! Error code: ")); ……

選擇狀態

Serial.println(F("Press s for stop, p for play, h for pause, f to select new file, d for deinit, v to view status.")); flag = 1; while(flag) { SdPlay.worker(); // You can remove this line if you like - worker is not necessary if(Serial.available()) { c = Serial.read();

……

接下來可以做什麼?

• 音頻放大器 • 逛逛電子商場 • 找尋尋有趣的專題

http://www.goodliffe.org.uk/arduino/mp3player.php

Facebook → CAVEDU教育團隊