用户工具

站点工具


learing:examples:analoginoutserial

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
learing:examples:analoginoutserial [2017/10/05 03:36] 弘毅learing:examples:analoginoutserial [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
 +====== Analog In, Out Serial(模拟输入输出) ======
 +
 +<WRAP left round info 65%>
 +这个例子展示了读取模拟脚输入,映射结果到0-255的范围,把读取结果以PWM输出来点亮LED。
 +</WRAP>
 +
 +<WRAP left round box 100%>
 +===== 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。
 +==== 代码 ====
 +<code cpp>/*
 + 读取模拟输入,映射结果到0-255,用映射值通过PWM点亮LED,显示结果到串口监视器上。
 + */
 +
 +
 +// 设置引脚:
 +const int analogInPin = A0;  
 +const int analogOutPin = 9; 
 +
 +int sensorValue = 0;        // 电位器值
 +int outputValue = 0;        // PWM模拟输出值
 +
 +void setup() {
 +  // 初始化串口通讯为9600波特率
 +  Serial.begin(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>
 +
 +
 +
 +
 +</WRAP>
 +
 +<WRAP left round box 100%>
 +===== ALPHA 8F328D-U核心 =====
 +==== 硬件 ====
 +
 +==== 搭建电路 ====
 +
 +==== 代码 ====
 +</WRAP>
 +
 +<WRAP left round box 100%>
 +===== MangoII =====
 +==== 硬件要求 ====
 +|OCROBOT控制器|
 +|电位器|
 +|LED|
 +|220欧姆电阻|
 +
 +连接电位器到控制器的5V,电位器中间脚接到A0口,电位器剩下的一个引脚连接到GND。连接220欧姆限流电阻到数字脚9,串联一个LED。LED的长脚应该从电阻连接到输出引脚,短的那一个引脚连接到GND。
 +
 +==== 原理图 ====
 +
 +
 +
 +==== 代码 ====
 +
 +
 +下面的程序中,声明引脚号的分配,变量,模拟值,输出值,然后就是串口通信了。
 +
 +接下来,在主循环代码中,sensorvalue用来存储电位器的模拟值,因为模拟值是0-1023之间,analogWrite值只能为0-255,电位器出来的数据要来点亮LED就必须按比例缩小。
 +
 +为了缩小这个值,我们用到map()函数来映射
 +
 +<code cpp>outputValue = map(sensorValue, 0, 1023, 0, 255);</code>
 +
 +outputValue是分配来存储映射后的电位器出来的值。map()函数有五个量,需要映射的值,数据上限和下限,映射之后的最大值和最小值。这里,传感器的值被从原始的0-1023映射到0-255
 +
 +映射出来的值就被用来输出成PWM来点亮LED,这个值随电位器的旋钮而改变。最后,原始值和映射值都以稳定的数据流被发到串口监视器。
 +
 +<code cpp>/*
 + 读取模拟输入,映射结果到0-255,用映射值通过PWM点亮LED,显示结果到串口监视器上。
 + */
 +
 +
 +// 设置引脚:
 +const int analogInPin = A0;  
 +const int analogOutPin = 9; 
 +
 +int sensorValue = 0;        // 电位器值
 +int outputValue = 0;        // PWM模拟输出值
 +
 +void setup() {
 +  // 初始化串口通讯为9600波特率
 +  Serial.begin(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>
 +</WRAP>