0

我试图在其他代码运行时让 LED 淡入淡出,作为一个很好的状态指示器。我在这里问过如何做到这一点,我得到了使用 msTimer2 库的建议。这似乎有效,直到我尝试在代码中使用analogWrite。它只是跳过了那部分。当我删除该库时,它再次工作,但当然我没有状态指示器工作。为什么我的代码不起作用?

int ledPin = 10;
int brightness = 0;
int fadeAmount = 1;
#include <MsTimer2.h>

void flash() {
  for(int fadeValue = 20 ; fadeValue <= 21; fadeValue +=1) {   
    delay(20000);                            
  }

    for(int fadeValue = 20 ; fadeValue <= 220; fadeValue +=1) {
    analogWrite(ledPin, fadeValue);          
        delay(50);                            
  }

  for(int fadeValue = 220 ; fadeValue >= 50; fadeValue -=1) {
    analogWrite(ledPin, fadeValue);           
    delay(50);
  }

    for(int fadeValue = 50 ; fadeValue <= 110; fadeValue +=1) {
    analogWrite(ledPin, fadeValue);          
    delay(130);                            
  }

  for(int fadeValue = 110 ; fadeValue >= 20; fadeValue -=1) {
    analogWrite(ledPin, fadeValue);          
    delay(250);
  }



}

void setup() {
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);

  MsTimer2::set(500, flash); 
  MsTimer2::start();
}

void loop() {
  digitalWrite(9, 1);
  delay(1000);
  digitalWrite(11, 1);
  delay(1000);
  digitalWrite(9, 0);
  delay(1000);
  digitalWrite(11, 0);
  delay(1000);
  analogWrite(9, brightness);    
  brightness = brightness + fadeAmount;
  delay(400);   
}
4

0 回答 0