如何使用arduino中的PWM在5s内淡出LED。我写了这个函数,它没有用。我试着调用这个函数。
const int minPWM = 0;
const int maxPWM = 255;
int fadeValue = 255;
int fadeIncrement = 5;
unsigned long previousFadeMillis;
int fadeInterval = 50;
void setLightPWM(unsigned long thisMillis) {
if (thisMillis - previousFadeMillis >= fadeInterval) {
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
}
analogWrite(garageLED, fadeValue);
previousFadeMillis = thisMillis;
}
return;
}
另外,我在loop()
中写了这一行 unsigned long currentMillis = millis();
setLightPWM(currentMillis);