搜索代码

第14/31-1课:Arduino Millis(无延迟)和map函数 | SunFounder Robojax

第14/31-1课:Arduino Millis(无延迟)和map函数 | SunFounder Robojax

本指南介绍Arduino的millis()函数,这是基于时间的项目中实现非阻塞代码执行的基本工具。与会暂停整个程序的delay()不同,millis()跟踪自开发板启动以来经过的时间,使您的Arduino能够有效地进行多任务处理。您可以使用此技术来构建:

  • 非阻塞LED闪烁器,可在其他代码执行时运行。
  • 由传感器或按钮触发的定时事件,例如按下按钮5秒后关闭的灯。
  • 定期记录传感器数值的简单数据记录系统。
  • 能够响应输入而不会冻结的用户界面系统。
组装好的Sunfounder小车

理解millis()函数

millis()函数是Arduino的内置命令,返回自Arduino开发板开始运行当前程序以来经过的毫秒数。它在通电时从零开始,每毫秒递增一次。您可以在代码中的任何位置调用此函数来获取当前的“运行时间”值。例如,1秒后它返回1000;5秒后返回5000。关键优势在于它不会暂停您的代码,因此您可以在其他任务继续运行的同时检查时间。

为什么要避免使用delay()?

delay()函数是暂停程序的简单方法,但它是一个阻塞函数。当您调用delay(5000)时,Arduino会停止执行所有其他代码5秒,冻结任何连接的组件。这对于需要持续响应输入或更新输出的项目来说是有问题的。使用millis()允许您检查是否已经过了特定时间,而不会停止程序的其余部分,使其非常适合多任务处理。

所需硬件

  • Arduino开发板(Uno、Mega等)
  • 用于编程和供电的USB线

接线指南

此示例不需要外部接线。Arduino只需通过USB连接到计算机即可上传代码并查看串口监视器输出。

代码解释

第一个示例是millis()工作原理的简单演示。代码初始化串口监视器,并在无限循环中打印millis()的值。

void setup() {
  Serial.begin(9600);//初始化串口监视器
  Serial.println("Introduction to millis");
  delay(2000);
}

void loop() {
  Serial.println(millis());
}

setup()函数以9600波特率启动串行通信并打印欢迎消息。这里的delay(2000)仅用于演示,以表明millis()从程序运行的那一刻开始计数。loop()函数持续打印millis()的当前值。当您打开串口监视器时,您会看到以毫秒为单位的时间迅速增加。这说明了millis()是一个永不停歇的计数器,为您的程序提供连续的时间参考。

这是基本的构建模块。在视频中,讲师演示了如何使用此值在设定时间后触发操作,例如检查是否已经过了5000毫秒(5秒)再执行特定代码块。这是通过将当前的millis()值存储在变量中,然后在主循环中进行比较来实现的。

使用map()函数以百分比值驱动LED的内容在第14/31-4课:Arduino编程map()示例1中介绍。

实际项目演示

在视频中,讲师上传此代码并打开串口监视器。您可以看到输出在最初的2秒延迟后开始,显示接近1999的值。然后数字迅速增加,演示了毫秒的连续计数。这个实时视图确认了函数正常工作,并提供了对时间跟踪方式的清晰直观理解。

本视频中的相关项目

章节

  • [00:00] millis()简介
  • [01:19] 什么是millis()以及为什么使用它
  • [03:06] millis()的实际演示
  • [05:46] 使用millis()触发定时事件
  • [08:29] 带按钮的示例
  • [12:14] 高级按钮定时示例
  • [17:20] map()函数简介
  • [19:47] 使用map()和LED的代码示例
  • [22:41] 创建自定义map函数

图像

Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
916-Lesson 14/31-1: Arduino coding millis() example 1
语言: C++
/*
Lesson 14/31: Arduino coding millis() 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/>.
 */
/*
 * Example-1 of using millis() with Arduino
 * Written by Ahmad Shamshiri on July 27, 2019
 * in Ajax, Ontario, Canada
 * Watch video instructio for millis() :https://youtu.be/u2HsiTS8niQ
 */


void setup() {
  Serial.begin(9600);//initialize serial monitor
  Serial.println("Introduction to millis");
  delay(2000);
  


}

void loop() {

  Serial.println(millis());

}// loop end

文件📁

Arduino 库(zip 格式)