两侧同时换到之前的修订记录前一修订版 | |
ocrobot:alpha:kitone:tutorial14 [2019/07/15 02:35] – 董凯萍 | ocrobot:alpha:kitone:tutorial14 [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 |
---|
| ~~NOTOC~~ |
| ====== Pitch follower using the tone() function (使用tone功能随输入量改变音高)====== |
| |
| <WRAP left round info 100%> |
| 此例程演示了使用tone功能来产生一个随输入值变化的音高。 |
| </WRAP> |
| |
| |
| ===== ALPHA MEGA328-U核心 ===== |
| ==== 硬件 ==== |
| * [[ocrobot:alpha:parallelexpansion:index|ALPHA 并行扩展板]] |
| * [[ocrobot:alpha:mega328-u:main|ALPHA MEGA328-U]] |
| * [[ocrobot:alpha:potentiometer:main|ALPHA 电位器模块]] |
| * [[ocrobot:alpha:buzzer:main|ALPHA 蜂鸣器模块]] |
| ==== 搭建电路 ==== |
| |
| - ALPHA MEGA328-U模块插入并行扩展板1号槽位。 |
| - ALPHA 蜂鸣器模块插入并行扩展版1号槽位堆叠于MEGA328-U上。 |
| - ALPHA 电位器模块插入并行扩展版2号槽位。 |
| - USB线连接计算机与ALPHA MEGA328-U。 |
| {{ocrobot:alpha:kitone:蜂_电位器435.png?nolink|}} |
| ==== 代码 ==== |
| |
| <code cpp> |
| /* |
| Pitch follower |
| 播放基于模拟输入变化的音符 |
| */ |
| void setup() { |
| Serial.begin(9600); // 初始化通信速率(调试用): |
| } |
| |
| void loop() { |
| int sensorReading = analogRead(A0); // 读取传感器值: |
| Serial.println(sensorReading); // 显示到串口 |
| // 模拟输入变化范围 (这里,从光敏电阻得到的大约400 - 1000 ) |
| // 输出范围(120 - 1500Hz) |
| // 基于传感器值变化范围改变输入最大最小输入值 |
| int thisPitch = map(sensorReading, 400, 1000, 120, 1500); |
| tone(9, thisPitch, 10); // 播放音符: |
| delay(1); // 延时 |
| } |
| </code> |
| <WRAP left round tip 100%> |
| 人类可以听到20-20000hz频率的声音,但是这里我们用120-1500hz更合适。 |
| 你需要得到真实的模拟输入变化范围。电路图中可以知道模拟输入值大约在400-1000变化。改变map值来对比你的传感器的变化范围。 |
| </WRAP> |
| |
| [[ocrobot:alpha:kitone:main|返回上一级]] |