## **曲調2**
> tone()函數通過控制**Atmega**處理器的內部定時器來工作。它會根據你設定的頻率在輸出引腳上輸出脈沖信號。由于tone()只使用了一個定時器,因此你只能同時在一個揚聲器上播放。不過,你可以按照一定順序的在不同引腳之間快速切換來實現多個揚聲器“同時”播放。為了做到這一點,你需要將在一個引腳發聲后,快速的“轉向”下一個引腳。
### **所需硬件**
* Arduino板或Genuino板
* 3個8Ω揚聲器
* 3個100Ω電阻
* 跳線
* 面包板
### **電路**

### **原理圖**

### **代碼**
下列工程按照順序在不同揚聲器上播放聲音(在后一個播放之前將前一個關閉)。注意,每次播放音符的時間應和之后delay的時間一樣。
~~~
/*
曲調2
按照順序在不同揚聲器上播放音符。
電路連接:
* 3個8Ω揚聲器分別連接到數字引腳6、7和8
代碼是公開的
*/
void setup() {
}
void loop() {
// 關閉8號引腳上的tone:
noTone(8);
// 在6號引腳上播放200毫秒:
tone(6, 440, 200);
delay(200);
// 關閉6號引腳上的tone:
noTone(6);
//在7號引腳上播放500毫秒:
tone(7, 494, 500);
delay(500);
// 關閉7號引腳上的tone:
noTone(7);
//在8號引腳上播放500毫秒:
tone(8, 523, 300);
delay(300);
}
~~~
[tone()](http://www.arduino.cc/en/Reference/Tone)?
- 說明
- 系統示例文件目錄結構及說明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (與特定硬件相關,暫無)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暫無)
- ArduinoISP