即使在digitalRead很低的情况下,Led也会保持不变

时间:2015-05-07 16:30:32

标签: arduino arduino-uno

我刚刚创建一个简单的基本程序无法弄清楚什么是错误的。 我设置了3个引脚作为输出,3个引脚作为输入 当这三个引脚digitalRead == HIGH时,它们将设置为HIGH但是我的led总是保持高电平。 这是我的arduino代码

int ledLow = 4;//Red Led
int ledAvg = 3;//Yellow Led
int ledHigh = 2;//Green Led
//
int WaterLow = 7;
int WaterAvg = 8;
int WaterHigh = 9;


void setup() {
  // put your setup code here, to run once:
  pinMode(ledLow,OUTPUT);
  pinMode(ledAvg, OUTPUT);
  pinMode(ledHigh, 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 read led indication water level is low
  }
  else{
    digitalWrite(ledLow, LOW);
  }
  if(digitalRead(WaterAvg) == HIGH){//If Water level is medium
    digitalWrite(ledAvg, HIGH);//Turn 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();

}

enter image description here

enter image description here

在上面的图像中,我已经连接了引脚2,3,4上的1.5K欧姆电阻和引脚7,8,9中的三根线,这将接收来自5伏引脚的输入并相应地转向引脚5伏引脚连接到电源总线上的正极端子和串联的9.1 * 2电阻,然后该导线连接引脚2,3,4

1 个答案:

答案 0 :(得分:0)

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

1)定义延迟

if(digitalRead(WaterLow) == HIGH){//if Water level is low
digitalWrite(ledLow, HIGH);//Turn read 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;
}