当前位置: 首页 > news >正文

ESP32-WROOM-32E LED点灯系列

点灯系列

最简单点亮默认IO2口的灯我就不记录了

直接pinMode()先设置IO2口的状态,再写入电平即可

ESP32默认IO2口和外接的IO4口 交相点灯

主要是应用 millis()函数来记录从程序开始到灯亮起这么一个时间记录的差值判断什么时候亮灭
为什么不用delay()函数?
是因为delay()一用,那么整块板子就会进入休眠状态,导致其他进程也运行不了
补充小知识:
在pinMode()中,有四种状态,分别是: OUTPUT、INPUT_PULLDOWN、INPUT_PULLUP、INPUT
INPUT_PULLUP 指的是在没有对其进行操作之前显示的是高电平模式,对此操作之后(比如按键按下后)才是低电平模式
INPUT_PULLDOWN 指的是在没有对其进行操作之前显示的是低电平模式,对此操作之后(比如按键按下后)才是高电平模式
INPUT单独出现的话接出来是高阻模式

#include <Arduino.h>// put function declarations here:
int pin = 2;
int LED_status = 0;
int pin2 = 4;
int LED_status2 = 0;
unsigned int preTime = 0;//记录上次切换LED状态的时间(类似间隔)
unsigned int preTime2 = 0;//记录上次切换LED状态的时间(类似间隔)void setup() {// put your setup code here, to run once://Serial.begin(115200);pinMode(pin,OUTPUT);digitalWrite(pin,HIGH);LED_status = 1;preTime = millis();//millis() 返回从程序开始运行以来的毫秒数pinMode(pin2,OUTPUT);digitalWrite(pin2,HIGH);LED_status2 = 1;preTime2 = millis();//millis() 返回从程序开始运行以来的毫秒数}void loop() {// put your main code here, to run repeatedly:unsigned int now = millis();if(now - preTime > 3000){preTime = now;if(LED_status == 0) LED_status = 1;else LED_status = 0;digitalWrite(pin,LED_status);} unsigned int now2 = millis();if(now2 - preTime2 > 2000){preTime2 = now2;if(LED_status2 == 0) LED_status2 = 1;else LED_status2 = 0;digitalWrite(pin2,LED_status2);} 
}

按键点灯

主要是采用RDB_button()库来消除按键抖动

#include <Arduino.h>
#include <RBD_button.h>
#include <RBD_Timer.h>// put function declarations here:
int pin_button = 25;
int pin_led = 2;
int ledstatus = 0;
RBD::Button button(pin_button,INPUT_PULLDOWN);
void setup() {// put your setup code here, to run once://pinMode(pin_button, INPUT_PULLDOWN);pinMode(pin_led,OUTPUT);button.setDebounceTime(20);//消除抖动时间20sdigitalWrite(pin_led,HIGH);ledstatus = HIGH;
}void loop() {// if(pin_button == HIGH)// {//   int val = digitalRead(pin_button);//   ledstatus = !ledstatus;//   digitalWrite(pin_led,ledstatus);// }if(button.onPressed()){ledstatus = !ledstatus;digitalWrite(led_pin,ledstatus);}
}

LEDC 输出PWM波

LEDC查阅在线文档
https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html

来一个调试串口的事例

#include <Arduino.h>void setup() 
{int ret = 0;Serial.println(115200);int ch0 = 0;int gpio4 = 4;ret = ledcSetup(ch0,5000,12);delay(200);if(ret == 0) Serial.println("ERROR");else Serial.println("OK");ledcAttachPin(gpio4,ch0);ledcWrite(ch0,pow(2,11));
}void loop() 
{}

LED呼吸灯实现

有两种写法,一种是用delay(),另一种就是用RBD_Button()实现


#include <Arduino.h>
#include <RBD_Button.h>
#include <RBD_Timer.h>int pin = 4;
int ch1 = 1;//ledc通道号
int duty = 0;//目前信号占空比
int cnt = 0; // 100%占空比的格子
int step = 0; //占空比步进值
int breathTime = 6; //呼吸灯的周期
//上述均在做初始化操作,以便被分配到未定义值
int preTime = 0;void setup() 
{ledcSetup(ch1,1000,12);cnt = pow(2,12);step = 2 * cnt / (50 * breathTime);//计算一次增加多少(其实我也不知道这里怎么算的)preTime = millis();ledcAttachPin(pin,ch1);//将IO口和PWM信号连接起来  
}void loop() 
{int now = millis();if(now - preTime >= 20){ledcWrite(ch1,duty);duty += step;if(duty > cnt){duty = cnt;step = -step;duty += step;}else if(duty < 0){duty = 0;step = -step;duty += step;}preTime = now;//一定不要忘记更新最新的preTime的值!!!}//delay(20);
}

定时器

创建单次运行的定时任务

运用的函数:setTimeout()
要注意在一开始的时候延时长一点,因为AsyncTimer() 根据mills()的精度来的,要是延时不够,mills()还没稳定下来,串口就显示不出信息

#include <Arduino.h>
#include <AsyncTimer.h>AsyncTimer timer;void huidiao()
{Serial.println("Hello,World!");
}void setup() 
{Serial.begin(115200);delay(2000);//需要延时延长一点,ESP 的 millis() 有时在启动初期还没稳定timer.setTimeout(huidiao, 1000);
}void loop() 
{timer.handle();//需要触发中断去执行中断的内容
}

创建周期运行的定时任务

运用的函数: setInterval()

#include <Arduino.h>
#include <AsyncTimer.h>AsyncTimer timer;void huidiao()
{Serial.println("Hello,World!");
}void setup() 
{Serial.begin(115200);delay(2000);//需要延时延长一点,ESP 的 millis() 有时在启动初期还没稳定//timer.setTimeout(huidiao, 1000);timer.setInterval(huidiao, 1000);
}void loop() 
{timer.handle();//需要触发中断去执行中断的内容
}

停止单个定时任务

运用到的函数:cancell()

停止多个定时任务

运用到的函数:cancellAll()

改变定时任务周期

运用到的函数:changeDelay()

重置定时任务

运用到的函数:reset()

改进的LED灯

#include <Arduino.h>
#include <AsyncTimer.h>
int pin = 4;
int status = 1;
AsyncTimer timer;
void huidiao()
{status = !status;digitalWrite(pin, status);
}void setup() 
{Serial.begin(115200);delay(2000);//需要延时延长一点,ESP 的 millis() 有时在启动初期还没稳定pinMode(pin, OUTPUT);digitalWrite(pin, HIGH);//timer.setTimeout(huidiao, 1000);timer.setInterval(huidiao, 1000);
}void loop() 
{timer.handle();//需要触发中断去执行中断的内容
}
http://www.dtcms.com/a/568782.html

相关文章:

  • 《红色脉络:一部PLMN在中国的演进史诗 (1G-6G)》 第15篇 | 结语:无尽的前沿——PLMN的未来与中国的全球角色
  • 付网站开发费计入什么科目seo外包杭州
  • 外贸网站域名被封免费网络游戏大全
  • PySide6 Win10记事本从零到一——第七章 格式菜单界面与功能实现
  • PDF文件损坏打不开怎么修复?2025年最新修复工具测评与对比
  • 谈谈MYSQL索引失效场景
  • Qwen-Image-Edit本地到底如何部署使用?怎么还有comfyui
  • 佳能LBP6018L打印浅淡问题的尝试性解决方法
  • 微算法科技(NASDAQ MLGO):以隐私计算区块链筑牢多方安全计算(MPC)安全防线
  • SpringCache :让缓存开发更高效
  • 电路分析 | Phasor Analysis(篇 1)
  • 网站备案取消长春网站建设模板样式
  • get_ccopt系列命令介绍(二)
  • 成都工业学院文献检索在哪个网站做破解wordpress密码
  • 做网站用什么系统好网站登录验证码是怎么做的
  • SQL语法基础教程
  • 算法25.0
  • 无穿戴动捕技术:解锁动作捕捉新维度,拓展多元应用边界
  • 高速PCB设计指南(5)
  • 栈与队列---算法题
  • 外包加工网站开发一个网页具体流程
  • 泰安肥城做网站的公司平台推广活动策划方案
  • 衡石科技跨平台数据网关技术解析:实现多源异构数据整合的新范式
  • 计算机网络实验04:IP与ICMP数据报分析实验
  • 基于python的天气预报系统设计和可视化数据分析源码+报告
  • lerobot so-arm101复现步骤
  • 司马阅与数之境科技达成生态战略合作,释放1+1>2的产业赋能价值
  • IE跳转Chrome浏览器及静默打包
  • Chrome恢复关闭网页的快捷键
  • python报修网站开发源码建设网站遇到的问题