第14/31-4课:Arduino编程map()示例1
本教程是Arduino编程系列教程的一部分,重点介绍map()函数的实际应用。在本课中,您将学习如何将百分比值(0–100)转换为PWM(脉冲宽度调制)值(0–255),以控制LED的亮度。无需手动计算60%等于153,map()函数会为您处理线性转换,使您的代码更简洁、更直观。
这项技术在许多实际应用中至关重要,因为您需要将传感器读数或用户输入转换为不同的范围。例如,您可以使用它来:
- 根据电位器读数控制直流电机的速度。
- 将温度传感器的原始值显示在0-100%的刻度上。
- 将操纵杆坐标映射到舵机角度。
- 根据环境光传感器调节LED亮度。
- 将0-5V模拟读数转换为0-1023数字值,反之亦然。
硬件/组件
要跟随本项目进行操作,您需要以下组件:
- Arduino开发板(Uno、Nano等)
- 一个LED
- 一个220欧姆电阻
- 跳线
- 面包板(可选,但推荐使用)
接线指南
在视频中,讲师使用之前关于LED渐亮课程中的接线设置来演示该项目。电路非常简单:将LED的阳极(长脚)连接到220欧姆电阻,然后将电阻的另一端连接到Arduino的数字引脚3。将LED的阴极(短脚)直接连接到Arduino的接地(GND)引脚。
代码解释
此示例的代码非常简洁,核心逻辑位于setup()函数中。让我们分解用户可配置的部分。
int LEDpin = 3; //定义LED的引脚
此行定义了LED所连接的Arduino引脚。在此示例中,设置为引脚3,这是一个支持PWM的引脚,对于模拟输出是必需的。如果您将LED连接到其他PWM引脚,可以更改此编号。
int b=8;//60%亮度
这是您将修改以测试不同亮度级别的关键变量。它表示所需的亮度百分比。代码中的注释写着“60%”,但视频中为了演示目的,该值设置为8。您可以将其更改为0到100之间的任何整数。例如,设置b = 60将表示60%的亮度级别。
int brightness = map(b, 0, 100, 0,255);
这是本课的核心。map()函数将您的百分比值(b)从其原始范围(0到100)转换为新的目标范围(0到255)。结果存储在brightness变量中,然后用于向LED引脚写入模拟值。
analogWrite(LEDpin, brightness);// 以60%亮度点亮LED
最后,此行使用analogWrite()将映射后的值发送到LED引脚。这设置了LED的占空比,从而有效控制其亮度。这种方法的美妙之处在于,您只需更改b变量即可控制LED,而map()函数会处理所有必要的计算。
实际项目/演示
在视频中,讲师上传代码并演示更改b变量的效果。当b设置为8时,串行监视器打印映射值20,LED明显变暗。将b更改为90会导致映射值229,使LED更亮。将b设置为100映射到255,即全亮度。这个实际演示清楚地展示了map()函数如何简化使用用户友好的百分比值控制模拟输出的过程。
本视频的相关项目
- 第14/31-1课:Arduino Millis(无延迟)和map函数 | SunFounder Robojax
- 第14/31-2课:Arduino编码millis()示例2带事件
- 第14/31-3课:Arduino编码millis()示例3带按钮
- 第14/31-5课:Arduino编码map()示例2
章节
- [00:00] Robojax Arduino课程介绍
- [01:19] 什么是millis()以及为什么使用它
- [05:46] 示例1:使用millis()进行定时事件
- [08:29] 示例2:使用millis()配合按钮
- [12:14] 示例3:延迟后检测第二次按钮按下
- [17:20] map()函数介绍
- [19:47] map()示例的代码讲解
- [20:41] map()函数的实际演示
/*
Lesson 14/31: Arduino coding map( ) example-1
Get this code and watch video Download and resource page https://robojax.com/RJT597
YouTube video https://www.youtube.com/watch?v=lqgzQohNiIM
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
int LEDpin = 3; //defind pin for LED
void setup() {
Serial.begin(9600);//initialize serial monitor with 9600 baud
pinMode(LEDpin, OUTPUT);//set pin as output
int b=8;//60% brightness
int brightness = map(b, 0, 100, 0,255);
Serial.println(brightness);//print it on serial monitor
analogWrite(LEDpin, brightness);// turn the LED on with 60% brightness
}
void loop() {
}
|||您可能需要的东西
-
亚马逊SunFounder Arduino Learning Kit on AliExpresss.click.aliexpress.com
-
亚马逊
-
亚马逊
-
亚马逊
资源与参考
-
文档SunFounder 3合1物联网/智能车/学习套件的文档docs.sunfounder.com
-
外部购买SunFounder的3合1智能汽车学习套件sunfounder.com
文件📁
用户手册
-
SunFounder 3-in-1 Arduino Smart Car assembly manualThis document shows step by step assembly of SunFounder 3-in-1 Arduino Smart Car
SunFounder_car_assembly_manual.pdf1.20 MB