#include <stdbool.h> bool isServoRunning = false; Clear_Buffer(); while (1) { if (RxBuffer[0] != 0) { HAL_Delay(1000); strx = strstr((const char*)RxBuffer, (const char*)"manual_feed"); if (strx == NULL) { printf("手动喂食获取失败"); } else { len = strlen(strx); number = strx[13]; printf("手动喂食获取成功"); HAL_Delay(500); printf("收到信息:manual_feed=%c", number); HAL_Delay(500); // 停止舵机转动 isServoRunning = false; if (number - '0' == 1) { isServoRunning = true; HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); while (isServoRunning) { __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1000); HAL_Delay(1000); } } else if (number - '0' == 0) { __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1500); HAL_Delay(1000); } Clear_Buffer(); } Clear_Buffer(); } HAL_Delay(500); }改成舵机转5s后停止
时间: 2024-04-10 14:28:29 浏览: 60
stdbool.zip_stdbool_stdbool.h_stdbool.h下载_stdbool下载
5星 · 资源好评率100%
您可以将代码修改如下,以使舵机转动 5 秒后停止:
```c
#include <stdbool.h>
bool isServoRunning = false;
int servoDuration = 5000; // 舵机转动时间(单位:毫秒)
Clear_Buffer();
while (1)
{
if (RxBuffer[0] != 0)
{
HAL_Delay(1000);
strx = strstr((const char*)RxBuffer, (const char*)"manual_feed");
if (strx == NULL)
{
printf("手动喂食获取失败");
}
else
{
len = strlen(strx);
number = strx[13];
printf("手动喂食获取成功");
HAL_Delay(500);
printf("收到信息:manual_feed=%c", number);
HAL_Delay(500);
// 停止舵机转动
isServoRunning = false;
if (number - '0' == 1)
{
isServoRunning = true;
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
// 舵机转动 5 秒
for (int i = 0; i < servoDuration / 1000; i++)
{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1000);
HAL_Delay(1000);
}
// 停止舵机转动
isServoRunning = false;
}
else if (number - '0' == 0)
{
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1500);
HAL_Delay(1000);
}
Clear_Buffer();
}
Clear_Buffer();
}
HAL_Delay(500);
}
```
这样修改后,舵机将会在收到 "manual_feed=1" 的指令后转动 5 秒,然后停止转动。如果收到 "manual_feed=0" 的指令,则舵机将转到中间位置,并保持不动。
阅读全文