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();//需要触发中断去执行中断的内容
}
