esp32触发相机,测试成功上升沿触发
串口发送命令 up 20000 1 20000 触发
#include <Arduino.h>
const int outputPin = 12; // 输出引脚
String inputCommand = ""; // 串口输入缓冲区
// 解析命令参数,例如 "up 10 5" 解析为 delayMicrosecondsTime=10, repeatCount=5
// 解析命令参数,例如 "up 10 5" 解析为 delayMicrosecondsTime=10, repeatCount=5
bool parseParameters(String command, int &delayMicrosecondsTime, int &repeatCount) {
int firstSpace = command.indexOf(' '); // 查找第一个空格位置
if (firstSpace < 0) return false; // 没有参数
int secondSpace = command.indexOf(' ', firstSpace + 1); // 查找第二个空格位置
if (secondSpace < 0) return false; // 参数不完整
// 提取参数并转换为整数
String delayMicrosecondsStr = command.substring(firstSpace + 1, secondSpace);
String repeatStr = command.substring(secondSpace + 1);
delayMicrosecondsTime = delayMicrosecondsStr.toInt();
repeatCount = repeatStr.toInt();
// 检查参数是否合法
return delayMicrosecondsTime > 0 && repeatCount > 0;
}
// 解析命令参数,例如 "up 10 5 3" 解析为 delayMicrosecondsTime=10, repeatCount=5, additionalParam=3
bool parseParametersThree(String command, int &delayMicrosecondsTime, int &repeatCount, int &additionalParam) {
int firstSpace = command.indexOf(' '); // 查找第一个空格位置
if (firstSpace < 0) return false; // 没有参数
int secondSpace = command.indexOf(' ', firstSpace + 1); // 查找第二个空格位置
if (secondSpace < 0) return false; // 参数不完整
int thirdSpace = command.indexOf(' ', secondSpace + 1); // 查找第三个空格位置
if (thirdSpace < 0) return false; // 参数不完整
// 提取参数并转换为整数
String delayMicrosecondsStr = command.substring(firstSpace + 1, secondSpace);
String repeatStr = command.substring(secondSpace + 1, thirdSpace);
String additionalStr = command.substring(thirdSpace + 1);
delayMicrosecondsTime = delayMicrosecondsStr.toInt();
repeatCount = repeatStr.toInt();
additionalParam = additionalStr.toInt();
// 检查参数是否合法
return delayMicrosecondsTime > 0 && repeatCount > 0 && additionalParam > 0;
}
void handleUpCommand(String command) {
int delayMicrosecondsTime = 0, repeatCount = 0, additionalParam = 0;
if (parseParametersThree(command, delayMicrosecondsTime, repeatCount, additionalParam)) {
Serial.println("Triggering UP signal...");
for (int i = 0; i < repeatCount; i++) {
digitalWrite(outputPin, HIGH); // 输出高电平
delayMicroseconds(additionalParam); // 等待
digitalWrite(outputPin, LOW); // 恢复低电平
delayMicroseconds(delayMicrosecondsTime); // 等待间隔
}
} else {
Serial.println("Invalid parameters for 'up' command");
}
}
void handleDownCommand(String command) {
int delayMicrosecondsTime = 0, repeatCount = 0;
if (parseParameters(command, delayMicrosecondsTime, repeatCount)) {
Serial.println("Triggering DOWN signal...");
for (int i = 0; i < repeatCount; i++) {
digitalWrite(outputPin, LOW); // 输出低电平
delayMicroseconds(delayMicrosecondsTime); // 等待
digitalWrite(outputPin, HIGH); // 恢复高电平
delayMicroseconds(delayMicrosecondsTime); // 等待间隔
}
} else {
Serial.println("Invalid parameters for 'down' command");
}
}
void parseCommand(String command) {
command.trim(); // 去掉多余的空格或换行符
if (command.startsWith("up")) {
handleUpCommand(command);
} else if (command.startsWith("down")) {
handleDownCommand(command);
} else {
Serial.println("Invalid command! Use 'up' or 'down'");
}
}
void setup() {
pinMode(outputPin, OUTPUT); // 设置输出引脚
digitalWrite(outputPin, LOW); // 初始化输出为低电平
Serial.begin(9600); // 初始化串口通信
}
void loop() {
// 检查是否有串口输入
while (Serial.available() > 0) {
char receivedChar = Serial.read();
if (receivedChar == '\n') { // 检测到回车符,执行命令
parseCommand(inputCommand);
inputCommand = ""; // 清空输入缓冲区
} else {
inputCommand += receivedChar; // 拼接命令字符
}
}
}