用户工具

站点工具


ocrobot:alpha:kitone:tutorial16

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
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。
 +</WRAP>
 +
 +===== ALPHA MEGA328-U核心 =====
 +==== 硬件 ====
 +  * [[ocrobot:alpha:parallelexpansion:index|ALPHA 并行扩展板]]
 +  * [[ocrobot:alpha:mega328-u:main|ALPHA MEGA328-U]]
 +  * [[ocrobot:alpha:11led:index|ALPHA 11 LED模块]]
 +  * [[ocrobot:alpha:potentiometer:main|ALPHA 电位器模块]]
 +==== 搭建电路 ====
 +  - ALPHA MEGA328-U模块插入并行扩展板1号槽位。
 +  - ALPHA 11 LED模块插入并行扩展版1号槽位,堆叠于MEGA328-U上。
 +  - ALPHA 电位器模块插入并行扩展版2号槽位。
 +  - USB线连接计算机与ALPHA MEGA328-U。
 +{{ocrobot:alpha:kitone:led_电位器435.png?nolink|}}
 +==== 代码 ====
 +<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);   // 初始化串口通讯为9600波特率
 +}
 +
 +void loop() {
 +  sensorValue = analogRead(analogInPin);         // 读取模拟输入
 +  outputValue = map(sensorValue, 0, 1023, 0, 255);  // 映射
 +  analogWrite(analogOutPin, outputValue);          // 输出:
 +  Serial.print("sensor = " );                // 显示到串口
 +  Serial.print(sensorValue);
 +  Serial.print("\t output = ");
 +  Serial.println(outputValue);
 +  delay(2);              // 等待两毫秒秒让模数转换器稳定
 +}
 +</code>
 +[[ocrobot:alpha:kitone:main|返回上一级]]
ocrobot/alpha/kitone/tutorial16.txt · 最后更改: 2023/06/07 04:23 由 127.0.0.1