这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版 | |||
ocrobot:alpha:kitone:tutorial16 [2019/07/15 02:36] – 董凯萍 | ocrobot:alpha:kitone:tutorial16 [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ~~NOTOC~~ | ||
+ | ====== Analog In, Out Serial(模拟输入输出) ====== | ||
+ | <WRAP left round info 100%> | ||
+ | 这个例子展示了读取模拟脚输入,映射结果到0-255的范围,把读取结果以PWM输出来点亮LED。 | ||
+ | </ | ||
+ | |||
+ | ===== ALPHA MEGA328-U核心 ===== | ||
+ | ==== 硬件 ==== | ||
+ | * [[ocrobot: | ||
+ | * [[ocrobot: | ||
+ | * [[ocrobot: | ||
+ | * [[ocrobot: | ||
+ | ==== 搭建电路 ==== | ||
+ | - ALPHA MEGA328-U模块插入并行扩展板1号槽位。 | ||
+ | - ALPHA 11 LED模块插入并行扩展版1号槽位,堆叠于MEGA328-U上。 | ||
+ | - ALPHA 电位器模块插入并行扩展版2号槽位。 | ||
+ | - USB线连接计算机与ALPHA MEGA328-U。 | ||
+ | {{ocrobot: | ||
+ | ==== 代码 ==== | ||
+ | <code cpp> | ||
+ | /* | ||
+ | 读取模拟输入,映射结果到0-255,用映射值通过PWM点亮LED,显示结果到串口监视器上。 | ||
+ | */ | ||
+ | const int analogInPin = A0; // 设置引脚: | ||
+ | const int analogOutPin = 9; | ||
+ | |||
+ | int sensorValue = 0; // 电位器值 | ||
+ | int outputValue = 0; // PWM模拟输出值 | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | sensorValue = analogRead(analogInPin); | ||
+ | outputValue = map(sensorValue, | ||
+ | analogWrite(analogOutPin, | ||
+ | Serial.print(" | ||
+ | Serial.print(sensorValue); | ||
+ | Serial.print(" | ||
+ | Serial.println(outputValue); | ||
+ | delay(2); | ||
+ | } | ||
+ | </ | ||
+ | [[ocrobot: |