You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino电压检测程序功能优化问询:低电压触发逻辑调整需求

Hey there! Let's take a look at your current Arduino code and fix up that trigger logic to be more robust. The main issues with your existing code are that delay() blocks the entire program (so you can't do anything else while waiting those 3 minutes) and it will keep triggering repeatedly as long as the voltage stays below 3V, which might not be what you want. Here are a few solid improvements:

方案1:非阻塞延时 + 状态变化触发(推荐)

This approach uses millis() instead of delay() so your program keeps running, and only triggers the action once when the voltage drops below 3V (instead of spamming it every loop cycle).

// 全局变量,记录状态和时间点
bool voltageWasAboveThreshold = true; // 初始假设电压高于3V
unsigned long triggerTime = 0;
bool waitingToTrigger = false;

void setup() {
  Serial.begin(9600); // 别忘了初始化串口!
}

void loop() {
  // 读取模拟引脚0的输入值并转换为电压
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);

  // 检查电压状态变化
  if (voltage < 3.00) {
    // 如果之前电压高于阈值,现在低于,启动等待计时
    if (voltageWasAboveThreshold && !waitingToTrigger) {
      triggerTime = millis();
      waitingToTrigger = true;
      voltageWasAboveThreshold = false;
      Serial.println("Voltage dropped below 3V - waiting 3 minutes...");
    }

    // 检查是否已经等待了3分钟
    if (waitingToTrigger && (millis() - triggerTime >= 180000)) {
      Serial.println("8");
      waitingToTrigger = false; // 重置状态,避免重复打印
    }
  } else {
    // 电压恢复到3V以上,重置状态
    voltageWasAboveThreshold = true;
    waitingToTrigger = false;
  }

  // 这里可以添加其他需要执行的代码,不会被阻塞
}

为什么这个更好?

  • 非阻塞:millis() tracks time without stopping the loop, so you can handle other sensors or tasks while waiting.
  • 单次触发:Only sends "8" once after the 3-minute wait, and won't trigger again until the voltage goes back above 3V and drops again.
  • 状态清晰:Variables track the system state so you know exactly what's happening at each step.
方案2:单次触发直到手动重置

If you want the action to trigger only once ever (until the Arduino resets) when the voltage drops below 3V, you can add a permanent flag:

bool hasTriggered = false;
unsigned long triggerTime = 0;
bool waiting = false;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);

  if (!hasTriggered && voltage < 3.00) {
    if (!waiting) {
      triggerTime = millis();
      waiting = true;
    }

    if (waiting && (millis() - triggerTime >= 180000)) {
      Serial.println("8");
      hasTriggered = true; // 标记为已触发,永远不再执行
      waiting = false;
    }
  }

  // 其他代码...
}
方案3:重复触发(每隔3分钟打印一次,只要电压低)

If you actually want to print "8" every 3 minutes as long as the voltage stays below 3V, this version will handle that without blocking:

unsigned long lastPrintTime = 0;
const unsigned long printInterval = 180000; // 3分钟

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);

  if (voltage < 3.00) {
    if (millis() - lastPrintTime >= printInterval) {
      Serial.println("8");
      lastPrintTime = millis();
    }
  } else {
    lastPrintTime = millis(); // 重置计时,电压恢复后重新开始算
  }

  // 其他代码...
}

额外注意点

  • Don't forget to initialize the serial port in setup() with Serial.begin(9600) (your original code was missing this, so you wouldn't see any output!).
  • If you're using a 3.3V Arduino (like an ESP8266/ESP32), adjust the voltage calculation to sensorValue * (3.3 / 1023.0) instead of 5.0.
  • For more accurate voltage readings, you might want to add a small capacitor (like 100nF) between A0 and GND to smooth out noise.

内容的提问来源于stack exchange,提问作者user3177511

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:17:42