Arduino ESP32 WiFi 控制两个 180 度舵机同步快速转动 - Blinker 库实现
基于 Arduino ESP32 WiFi 通过 Blinker 控制两个 180 度舵机同步快速转动
本文介绍如何使用 Arduino ESP32 WiFi 和 Blinker 库控制两个 180 度舵机同步快速转动。舵机 A 从 0 度到 180 度,然后从 180 度到 0 度,反复循环;舵机 B 从 180 度到 0 度,然后从 0 度到 180 度,反复循环。Blinker 控制条件如下:
- 按下第一个按键:启动 AB 舵机同步转动。
- 按下第二个按键:AB 舵机加速转动。
- 按下第三个按键:A 舵机单独加速,B 舵机保持原来速度。
- 按下第四个按键:B 舵机单独加速,A 舵机保持原来速度。
- 按下第五个按键:AB 舵机停止转动。
代码内容
#include <Blinker.h>
#include <Servo.h>
#define PIN_A 14 // A 舵机接口
#define PIN_B 15 // B 舵机接口
Servo servoA; // A 舵机对象
Servo servoB; // B 舵机对象
int angleA = 0; // A 舵机当前角度
int angleB = 180; // B 舵机当前角度
bool isRunning = false; // 是否正在运行
bool isSpeedUp = false; // 是否加速
int speedA = 10; // A 舵机速度
int speedB = 10; // B 舵机速度
int button1 = 0; // 第一个按键
int button2 = 0; // 第二个按键
int button3 = 0; // 第三个按键
int button4 = 0; // 第四个按键
int button5 = 0; // 第五个按键
void button1_callback(const String& state) {
if (state == BLINKER_BUTTON_SHORTPRESS) {
isRunning = true;
}
}
void button2_callback(const String& state) {
if (state == BLINKER_BUTTON_SHORTPRESS) {
isSpeedUp = true;
}
}
void button3_callback(const String& state) {
if (state == BLINKER_BUTTON_SHORTPRESS) {
speedA = 20;
speedB = 10;
}
}
void button4_callback(const String& state) {
if (state == BLINKER_BUTTON_SHORTPRESS) {
speedA = 10;
speedB = 20;
}
}
void button5_callback(const String& state) {
if (state == BLINKER_BUTTON_SHORTPRESS) {
isRunning = false;
angleA = 0;
angleB = 180;
servoA.write(angleA);
servoB.write(angleB);
speedA = 10;
speedB = 10;
}
}
void setup() {
Blinker.begin("ESP32"); // 初始化 blinker,设备名称为 ESP32
servoA.attach(PIN_A); // 绑定 A 舵机接口
servoB.attach(PIN_B); // 绑定 B 舵机接口
servoA.write(angleA); // 将 A 舵机旋转到 0 度
servoB.write(angleB); // 将 B 舵机旋转到 180 度
}
void loop() {
button1 = Blinker.button(BLINKER_BUTTON_1); // 获取第一个按键状态
button2 = Blinker.button(BLINKER_BUTTON_2); // 获取第二个按键状态
button3 = Blinker.button(BLINKER_BUTTON_3); // 获取第三个按键状态
button4 = Blinker.button(BLINKER_BUTTON_4); // 获取第四个按键状态
button5 = Blinker.button(BLINKER_BUTTON_5); // 获取第五个按键状态
if (isRunning) {
if (angleA == 0) {
// A 舵机从 0 度到 180 度
for (int i = 0; i <= 180; i++) {
angleA = i;
servoA.write(angleA);
if (isSpeedUp) {
delay(speedA / 2);
} else {
delay(speedA);
}
}
isSpeedUp = false;
// A 舵机从 180 度到 0 度
for (int i = 180; i >= 0; i--) {
angleA = i;
servoA.write(angleA);
if (isSpeedUp) {
delay(speedA / 2);
} else {
delay(speedA);
}
}
isSpeedUp = false;
}
if (angleB == 180) {
// B 舵机从 180 度到 0 度
for (int i = 180; i >= 0; i--) {
angleB = i;
servoB.write(angleB);
if (isSpeedUp) {
delay(speedB / 2);
} else {
delay(speedB);
}
}
isSpeedUp = false;
// B 舵机从 0 度到 180 度
for (int i = 0; i <= 180; i++) {
angleB = i;
servoB.write(angleB);
if (isSpeedUp) {
delay(speedB / 2);
} else {
delay(speedB);
}
}
isSpeedUp = false;
}
}
Blinker.attachButton(BLINKER_BUTTON_1, button1_callback); // 绑定第一个按键回调函数
Blinker.attachButton(BLINKER_BUTTON_2, button2_callback); // 绑定第二个按键回调函数
Blinker.attachButton(BLINKER_BUTTON_3, button3_callback); // 绑定第三个按键回调函数
Blinker.attachButton(BLINKER_BUTTON_4, button4_callback); // 绑定第四个按键回调函数
Blinker.attachButton(BLINKER_BUTTON_5, button5_callback); // 绑定第五个按键回调函数
Blinker.run(); // 运行 blinker
}
注意:
- 舵机转动的速度通过
delay()函数实现,但是在程序运行过程中需要不断检测按钮状态,因此可能会出现舵机转动不够平滑的情况。可以在使用过程中根据实际情况调整舵机速度。 - 确保已经安装 Blinker 库,可以在 Arduino IDE 中的“工具”-“管理库”中搜索“blinker”进行安装。
- 替换代码中的
PIN_A和PIN_B为实际连接的舵机引脚。 - 运行程序前,将舵机电源连接到 ESP32 的 5V 或 3.3V 引脚,并确保 GND 引脚连接正确。
- 根据需要调整代码中的
speedA和speedB来控制舵机转动速度。 - 详细的 Blinker 库使用说明请参考 Blinker 官方文档: https://github.com/blinker-io/blinker-doc
原文地址: https://www.cveoy.top/t/topic/nuiv 著作权归作者所有。请勿转载和采集!