tone()

描述

在指定的 pin 腳上產生一個指定頻率的方波 (工作週期是 50% 的 PWM 信號),可以指定持續時間或者持續直到呼叫 noTone(),在這個指定的 pin 腳上可以接蜂鳴器或者喇叭來發出一段音頻。

同一時間只能產生一段音頻,如果這段音頻已經在別的 pin 腳上撥放,則呼叫 tone() 是無效的;如果是在同一個 pin 腳上撥放時,呼叫 tone() 則是設定它的頻率。

注意: 如果你想要在多個 pin 腳上撥放不同的音頻,在呼叫下一次 tone() 前你需要先呼叫 noTone()

語法


tone(pin, frequency)
tone(pin, frequency, duration)

參數

pin: 指定要產生音頻的 pin 腳
frequency: 聲音的頻率 (Hz),unsigned int 型別
duration: 音頻的持續時間,單位為毫秒,unsigned long 型別 (此參數為非必需)

回傳

無回傳值

使用限制

tone() 使用了 pin 32 腳的 PWM 計時器,因此不可與 analogWrite(32) 同時被呼叫,以避免衝突;此外,tone() 亦不可與共用相同計時器的 MsTimer2IRremote 函式庫同時使用。

See also

noTone()
Tutorial: Tone
Tutorial: Pitch follower
Tutorial: Simple Keyboard
Tutorial: multiple tones


語法參考主頁面

本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.