即使 digitalRead 低,LED 也会保持亮起

LED stays on even when digitalRead is low

我只是在创建一个简单的基本程序,但我不知道出了什么问题。

我已经将三个引脚设置为输出,三个引脚设置为输入。当这三个引脚 digitalRead == HIGH 时,它们会将 LED 设置为 HIGH,但我的 LED 始终保持高电平。

这是我的 Arduino 代码:

int LED_Low = 4; // Red LED
int LED_Avg = 3; // Yellow LED
int LED_High = 2; // Green Led

int WaterLow = 7;
int WaterAvg = 8;
int WaterHigh = 9;


void setup() {
  // Put your setup code here, to run once:
  pinMode(LED_Low,  OUTPUT);
  pinMode(LED_Avg,  OUTPUT);
  pinMode(LED_High, OUTPUT);

  pinMode(WaterLow,  INPUT);
  pinMode(WaterAvg,  INPUT);
  pinMode(WaterHigh, INPUT);
}

void check(){
    if(digitalRead(WaterLow) == HIGH){ // If Water level is low
        digitalWrite(ledLow, HIGH); // Turn on red LED indication water level is low
    }
    else{
        digitalWrite(ledLow, LOW);
    }
    if(digitalRead(WaterAvg) == HIGH){ // If water level is medium
        digitalWrite(ledAvg, HIGH); // Turn on yellow LED indicating water level is average
    }
    else{
        digitalWrite(ledAvg, LOW);
    }
    if(digitalRead(WaterHigh) == HIGH){ //
        digitalWrite(ledHigh, HIGH); //
    }
    else{
        digitalWrite(ledHigh, LOW);
    }
}

void loop() {
    // Put your main code here, to run repeatedly:
    check();
}

在上图中,我在引脚 2、3 和 4 上连接了 1.5 kΩ 电阻器,并在引脚 7、8 和 9 中连接了三根电线,它们将接收来自 5 伏引脚的输入并打开 LED .因此,5伏引脚连接到电源总线上的正极端子并串联9.1 * 2个电阻,然后该线连接引脚2、3和4。

我发现了问题。我的代码没问题。这是我的电路。

我声明接收输入的引脚没有接地。

你可以在两个案例的帮助下做到这一点

  1. 定义延迟

    if (digitalRead(WaterLow) == HIGH) // If Water level is low
    {
        digitalWrite(ledLow, HIGH); // Turn red LED indication water level is low
        delay(2000);
    }
    else
    {
        digitalWrite(ledLow, LOW);
    }
    
  2. 设置这样的条件

    int stateled = LOW;
    int previous = LOW;
    long time = 0;
    long debounce = 200;
    
    void loop()
    {
        stateButton = digitalRead(WaterLow);
        if (stateButton == HIGH && previous == LOW && millis() - time > debounce)
        {
            if(stateLED == HIGH)
            {
                stateLED = LOW;
            }
            else
            {
                stateLED = HIGH;
            }
            time = millis();
        }
        digitalWrite(ledlow, stateLED);
        previous == stateButton;
    }