Arduino开关按键和延时功能

2019/09 01 11:09
void setup() {
  pinMode(13, OUTPUT);
  pinMode(2, INPUT);
}
 
void loop() {
  boolean val = true; 
  unsigned long showtime = millis();
  while(true){ 
    unsigned long nowtime = millis();
    if (digitalRead(2)) {
      showtime = nowtime + 3000;
    }
    if (nowtime <  showtime) {
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    } 
    val = !val;
    delay(100);
  }  
}

LED接1K电阻,分别接GND和13号口

开关接10K电阻,分别接GND,VCC,2号口

--转载请注明: http://blog.coolcoding.cn/?p=126