本帖最后由 弘毅 于 2012-11-19 17:39 编辑
- /*
- keyboard
- 播放声音的音调变化是随着模拟量输入的变化而改变
-
-
- 电路描述:
- *3 个模拟传感器 接+5V从模拟口A0至A5
- *3 个10K电阻从模拟口A0至A5回到GND
- *8欧姆的杨声器接在数字端口8
- created 21 Jan 2010
- modified 9 Apr 2012
- by Tom Igoe
-
- 这个范例代码在公共范围(不受版权制约)
-
- http://arduino.cc/en/Tutorial/Tone3
-
- */
- /*
- 电路原理图
-
- */
- #include "pitches.h"
- const int threshold = 10; // 传感器的最小读数
- //notes数组包含三个传感器,用于播放
- int notes[] = {
- NOTE_A4, NOTE_B4,NOTE_C3 };
- void setup() {
- }
- void loop() {
- for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
- //得到一个传感器的读数
- int sensorReading = analogRead(thisSensor);
- //如果传感器被按下足够了
- if (sensorReading > threshold) {
- // play the note corresponding to this sensor:
- //播放对应的传器器相应的声音
- tone(8, notes[thisSensor], 20);
- }
- }
- }
复制代码
|