基于ardiuno的esp32WiFi通过blinker控制两个180度舵机a和b同步快速转动转动条件为:a舵机从0度到180然后从180到0反复循环。b舵机从180度到0然后从0到180反复循环。blinker控制条件:blinker通过按下第一个键可控制ab舵机同步启动按下第二个按键ab舵机加速转动按下第三个按键a舵机单独加速转动b舵机保持原来速度按下第四个按键b舵机单独加速转动a舵机保持原来
由于题目中需要使用blinker库来控制舵机转动,因此需要先安装blinker库。可以在Arduino IDE中的“工具”-“管理库”中搜索“blinker”进行安装。
接下来是详细代码:
#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()函数实现,但是在程序运行过程中需要不断检测按钮状态,因此可能会出现舵机转动不够平滑的情况。可以在使用过程中根据实际情况调整舵机速度
原文地址: https://www.cveoy.top/t/topic/cm8o 著作权归作者所有。请勿转载和采集!