欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > esp32触发相机

esp32触发相机

2024/11/29 18:21:40 来源:https://blog.csdn.net/isyoungboy/article/details/144051786  浏览:    关键词:esp32触发相机

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; // 拼接命令字符}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com