PlatformIO使用Arduino[Ticker]库(ESP8266)
在《PlatformIO如何编译Arduino程序(ESP8266)》一文中,我们是使用delay()函数来延时,让LED灯闪烁,但因为delay()函数的使用会阻塞主程序的执行,所以,一般在需要处理多种任务或功能时,不使用delay()函数。要让LED灯闪烁,可以调一个“闹钟”,每隔一定的时间,让“闹钟”自动响起,这个“闹钟”就是定时器。
那么,我们现在来学习一下Arduino的定时器操作库Ticker.h。
首先:第一步,在项目中安装Ticker.h库(注意,在PlatformIO中可能会搜索到多个Ticker.h的定时器库,具体的实现方法或许会有所不同,在这里使用的是JSC electronics所编写的库)。
按如何步骤进行操作:
添加成功后,我们在VSCode中打开项目的资源管理器,找到/.vscode/c_cpp_properties.json文件,打开后可以看到标注黄色底部分的内容,这个就是我们定时器库的安装路径了。
当然,在VSCode中打开项目的资源管理器,可以看到确实已经添加了该库。资源管理器中的路径为:/.pio\libdeps/nodemcuv2/Ticker
在VSCode资源管理器中打开,/.pio/libdeps/nodemcuv2/Ticker/src/Ticker.h可以查阅库中的函数。
该库的内置方法较少,具体如下:
Ticker.h文件内容:
/*** @file Ticker.h* Project Ticker* @brief A helper library to hide complexity of computing time differences in pseudo-multitasking projects* @author JSC electronics* License Apache-2.0 - Copyright (c) 2020 JSC electronics** @section License** Copyright (c) 2020 JSC electronics** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/#ifndef TICKER_H
#define TICKER_H#ifdef __cplusplus#include <Arduino.h>namespace jsc {constexpr static unsigned int DEFAULT_TICK_PERIOD_MS = 10;class Ticker {public:/*** Default constructor. You can specify a custom ticker period.* If it's not specified, <code>DEFAULT_TICK_PERIOD_MS</code> will be used.*/Ticker(unsigned long singleTickMs = DEFAULT_TICK_PERIOD_MS);/*** Calculate number of ticks since the Ticker was started.* For example, if a ticker period is 10 ms, and 30 ms passed since start, * this method returns 3.* * In most cases, however, you can specify the duration of a single tick, and once* this method returns a value greater than 0, you know that your defined period has passed.* * @return number of ticks since the Ticker was started.*/uint16_t elapsedTicks();unsigned long elapsed();void restart();private:unsigned long singleTickMs;unsigned long timeSample = 0;};
}#endif
#endif // TICKER_H
Ticker.cpp文件内容:
/*** @file Ticker.cpp* Project Ticker* @brief A helper library to hide complexity of computing time differences in pseudo-multitasking projects* @author JSC electronics* License Apache-2.0 - Copyright (c) 2020 JSC electronics** @section License** Copyright (c) 2020 JSC electronics** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/#include "Ticker.h"jsc::Ticker::Ticker(unsigned long periodMs) : singleTickMs(periodMs)
{timeSample = millis();
}unsigned long jsc::Ticker::elapsed()
{return millis() - timeSample;
}uint16_t jsc::Ticker::elapsedTicks()
{return elapsed() / singleTickMs;
}void jsc::Ticker::restart()
{timeSample = millis();
}
在/.pio/libdeps/nodemcuv2/Ticker/examples文件夹中还提供了两个例子。这两个例子也非常简单,一看即明白。可以参照两个例子中的语法来进行程序的编写。注意,在不进行手动设定定时器周期时,默认的定时器周期为:10MS。
在头文件中就可以看到:
constexpr static unsigned int DEFAULT_TICK_PERIOD_MS = 10;
现在我们参照例子,把我们的/src/main.cpp内容修改如下:
#include <Arduino.h>
#include <Ticker.h>#define led_1 2 //NodeMCU的D4
#define led_2 0 //NodeMCU的D3
#define led_3 4 //NodeMCU的D2
#define led_4 5 //NodeMCU的D1jsc::Ticker led_blink_1(1000);//创建定时器1
jsc::Ticker led_blink_2(600);//创建定时器2
jsc::Ticker led_blink_3(300);//创建定时器3
jsc::Ticker led_blink_4(100);//创建定时器4void setup() {// put your setup code here, to run once:pinMode(led_1,OUTPUT);//设置GPIO 2的模式为:输出模式pinMode(led_2,OUTPUT);//设置GPIO 0的模式为:输出模式pinMode(led_3,OUTPUT);//设置GPIO 4的模式为:输出模式pinMode(led_4,OUTPUT);//设置GPIO 5的模式为:输出模式}void loop() {// put your main code here, to run repeatedly:if (led_blink_1.elapsedTicks() > 0) {led_blink_1.restart();digitalWrite(led_1, !digitalRead(led_1));}if (led_blink_2.elapsedTicks() > 0) {led_blink_2.restart();digitalWrite(led_2, !digitalRead(led_2));}if (led_blink_3.elapsedTicks() > 0) {led_blink_3.restart();digitalWrite(led_3, !digitalRead(led_3));}if (led_blink_4.elapsedTicks() > 0) {led_blink_4.restart();digitalWrite(led_4, !digitalRead(led_4));}
}
保存main.cpp的内容后编译上传到开发板上,根据程序中设置的引脚,我们再接上额外的三颗LED灯珠(开发板的引脚图如下图所示,开发板在GPIO 2引脚上已集成了LED了)。这样我们总共有4颗LED了,其中:
GPIO 2的LED闪烁周期为:1.0秒;
GPIO 0的LED闪烁周期为:0.6秒;
GPIO 4的LED闪烁周期为:0.3秒;
GPIO 5的LED闪烁周期为:0.1秒;
正确接线、成功编译并上传程序到开发板后,可以看到效果如下:
PlatformIO使用Arduino[Ticker]库(ESP8266)相关推荐
- ESP8266多任务处理---Ticker库
ESP8266多任务处理-Ticker库 ESP8266在运行过程中,只能一条线式的依次执行任务.但是我们在开发物联网项目时,可能需要ESP8266在执行某一任务的过程中,还能处理其它任务.比如,我们 ...
- NodeMcu arduino ESP8266 使用Ticker库(多任务处理)
NodeMcu arduino ESP8266 本文章学习借鉴于太极创客团队,以表感谢.官网http://www.taichi-maker.com/ ESP8266 使用Ticker库(多任务处理) ...
- 【ESP32_8266_WiFi (十四)】ESP8266多任务处理 – Ticker库使用说明
文章目录 ESP8266多任务处理 – Ticker库使用说明 1 Ticker库基本操作 2 停止定时执行函数 3 向定时调用函数传递参数 4 利用多个Ticker对象让ESP8266处理多任务 5 ...
- 深入学习Arduino Adafruit_MQTT库(初学者不再惧怕Arduino MQTT)
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... 快速导航 单片机菜鸟的博客快速索引(快速找到你 ...
- arduino下载库出错_【arduino】DIY音乐播放器,arduino播放wav音乐,TRMpcm库测试及使用...
微信关注 "DLGG创客DIY"设为"星标",重磅干货,第一时间送达. arduino特点库超多,想必大家都领教了,今天来分享一下之前玩过的TRMpcm库. 这 ...
- arduino下载库出错_arduino的I2C通讯 3:驱动1602液晶屏
上个推送,我们学习了I2C功能的基础知识.而且知道了使用很多器件都需要安装库.本次,我们一起来做一个实例,用arduino驱动1602液晶屏 1602代表屏幕有16列,2行.传统的驱动方式占用了大量的 ...
- Mind+上传模式的第三方Arduino用户库实现 -DHT11温湿度模块
Mind+上传模式的第三方Arduino用户库实现--DHT11模块读取温湿度 前言 Mind+是一款拥有自主知识产权的国产青少年编程软件,支持Arduino.micro:bit.掌控板等各种开源硬件 ...
- 利用arduino红外库遥控美的家用空调
由于美的空调使用的不是简单的红外编码信号,因此在使用arduino红外库的时候不能直接调用其已有的编码规则,需要先了解美的空调的编码规则,再使用IRsend::sendRaw这个函数直接发送原始数据. ...
- 用arduino IDE开发esp8266 wifi
用arduino IDE开发esp8266 被临时通知开发一个网络传输数据,于是用起我好旧不用的esp8266,记得当初大一时候第一次使用网络使用是stm32+esp8266,比现在用乐鑫的node ...
最新文章
- 刚开始Windows Mobile的开发,请大家多多关照
- Android多媒体开发:照相机
- matlab常用工具箱的调用指令
- npm ERR! the command again as root/Administrator
- matlab求感知距离的命令,感知机算法(MATLAB)
- python编程语言继承_Python面向对象程序设计类的封装与继承用法示例
- c++已知2点求中垂线_呆哥数学函数合集——函数的概念【2】
- 如何将微商管理模式流程化
- 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)
- springboot10-springcloud-eureka 服务注册与发现,负载均衡客户端(ribbon,feign)调用
- 服务器 python cant open file_如何删除分析*。gcda:无法打开python virtualenv builder出错?...
- 编译ffmpeg:no match insn: lbu $r16,0($r5)
- Cadence Allegro Vertex功能介绍使用方法Vertex与Slide的区别图文教程
- 如何通过Photoshop制作Gif图片(把几张图片合成一张Gif图片)
- 1483选票统计(一)(结构体专题)
- L3-001 凑零钱 (30 分)——01背包+路径记录
- Debian7安装无线网卡
- 流量不清零:还在忽悠广大网民?
- Windows 10电脑使用VMware虚拟机安装macOS苹果系统[一站式保姆级别教程]
- torch对于tensor的常规操作