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单独出现的话接出来是高阻模式

cpp 复制代码
#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()库来消除按键抖动

cpp 复制代码
#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);//消除抖动时间20s
  digitalWrite(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波

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

来一个调试串口的事例

cpp 复制代码
#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()实现

cpp 复制代码
#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()还没稳定下来,串口就显示不出信息

cpp 复制代码
#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()

cpp 复制代码
#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灯

cpp 复制代码
#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();//需要触发中断去执行中断的内容
}
相关推荐
容沁风8 小时前
用ai来写一个CO2传感器检测
esp32·micropython·microdot·二氧化碳传感器
我先去打把游戏先4 天前
ESP32C3开发指南(基于IDF):console控制台命令行交互功能
笔记·嵌入式硬件·mcu·物联网·学习·esp32·交互
superior tigre12 天前
esp32学习随笔文档1
学习·esp32
特立独行的猫a15 天前
ESP32使用笔记(基于ESP-IDF):小智AI的ESP32项目架构与启动流程全面解析
人工智能·架构·esp32·小智ai
我先去打把游戏先18 天前
ESP32学习笔记(基于IDF):ESP32连接MQTT服务器
服务器·笔记·单片机·嵌入式硬件·学习·esp32
我先去打把游戏先19 天前
ESP32学习笔记(基于IDF):SmartConfig一键配网
笔记·嵌入式硬件·mcu·物联网·学习·esp32·硬件工程
我先去打把游戏先22 天前
ESP32学习笔记(基于IDF):IOT应用——WIFI连接
笔记·单片机·嵌入式硬件·mcu·物联网·学习·esp32
星野云联AIoT技术洞察1 个月前
OpenMQTTGateway 技术全解:统一多协议到 MQTT 的开源网关
lora·esp32·智能家居·ble·ir·iot网关·openmqttgateway
Anonymousgirls2 个月前
正点原子小智BOX0/BOX2 产品使用视频表情功能
学习·esp32·小智ai·视频表情