Commit c1e07a1b authored by 957dd's avatar 957dd

Merge branch 'feature/shot_bugre' into 'master'

Feature/shot bugre See merge request !5
parents ab6a78c0 ddfbae87
......@@ -86,17 +86,19 @@ void DeviceControl::updateLedBlood() {
// 读取传感器值实现
double DeviceControl::readSensor() {
int rawValue = analogRead(_sensorPin);
// 将读数转换为电压(假设 ADC 12位,参考电压 3.3V 或 2.5V)
// ESP32C3 ADC 可以是 12位 (0-4095),参考电压通常是 3.3V 内部或外部。
// 原始代码使用了 2.5,我们假设是 2.5V 参考电压,或者原始代码有误。
// 使用 3.3V 参考电压是更常见的做法: rawValue * 3.3 / 4095.0
// 如果你的电路使用了外部 2.5V 参考或其他方式,请调整下面的乘数
const double referenceVoltage = 3.3; // 假设使用 3.3V 参考电压
double voltage = (double)rawValue * referenceVoltage / 4095.0;
int DeviceControl::readSensor() {
// int rawValue = analogRead(_sensorPin);
// // 将读数转换为电压(假设 ADC 12位,参考电压 3.3V 或 2.5V)
// // ESP32C3 ADC 可以是 12位 (0-4095),参考电压通常是 3.3V 内部或外部。
// // 原始代码使用了 2.5,我们假设是 2.5V 参考电压,或者原始代码有误。
// // 使用 3.3V 参考电压是更常见的做法: rawValue * 3.3 / 4095.0
// // 如果你的电路使用了外部 2.5V 参考或其他方式,请调整下面的乘数
// const double referenceVoltage = 3.3; // 假设使用 3.3V 参考电压
// double voltage = (double)rawValue * referenceVoltage / 4095.0;
// Serial.printf("Sensor raw: %d, Voltage: %.2fV\n", rawValue, voltage); // 调试信息
return voltage;
int digitalValue = digitalRead(_sensorPin);
return (digitalValue == HIGH) ? 1 : 0;
// return voltage;
}
// 处理来自 MQTT 的控制指令
......@@ -194,25 +196,29 @@ void DeviceControl::update() {
// 简单的实现:每隔一定时间或阈值触发时发布
// static unsigned long lastSensorPublishTime = 0;
// const unsigned long sensorPublishInterval = 50; // 每 1 秒发布一次传感器数据
_shot_count++;
if(_shot_count < 6){
return ;
}
// if (millis() - lastSensorPublishTime >= sensorPublishInterval) {
double sensorVoltage = readSensor();
//double sensorVoltage = readSensor();
// 原始代码中的阈值判断
if (sensorVoltage >= 1.25&& mqttManager.wifi_connected_is()==0) { // 传感器值超过阈值
if (readSensor() == 1&& mqttManager.wifi_connected_is()==0) { // 传感器值超过阈值
if (_mqttManager) {
// 发布 "shoted" 消息
_mqttManager->publishJson("shoted", 1);
Serial.print(sensorVoltage);
audioManager.requestPlay();
// Serial.print(sensorVoltage);
} else {
Serial.println("MQTTManager not set, cannot publish sensor data.");
}
_shot_count=0;
}
// lastSensorPublishTime = millis();
// }
_shot_count=0;
_shot_count=9;
}
......
......@@ -33,7 +33,7 @@ private:
void updateLedBlood();
// 读取传感器值
double readSensor();
int readSensor();
public:
// 构造函数
......
// #include <string> // For std::string
// #include "button_heartbeat.h"
// #ifndef BUTTON_HEARTBEAT_H
// #define BUTTON_HEARTBEAT_H
// class ButtonHeartbeat {
// public:
// // 按键检测常量
// const unsigned long LONG_PRESS_DURATION_MS = 5000; // 长按触发时间:5秒
// const unsigned long DEBOUNCE_DELAY_MS = 50; // 按键去抖动时间:50毫秒
// private:
// }
// #endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment