Neblokuojantis „LED fader“

const int ledPin = 13;

int direction = 10;
bool needToFade = true;
int fadeValue = 0;

void fader();

void setup() {
  Serial.begin(9600);
}

void loop() {
  static uint32_t prevMillis = 0;
  if(millis() - prevMillis > 100 && needToFade) {
    prevMillis = millis();
    fader();  
  }
}

void fader() {
  if(fadeValue > 255 || fadeValue < 0) {
    direction *= -1;
  }
  analogWrite(ledPin, fadeValue);
  fadeValue += direction;
  Serial.print("Debug fade: ");
  Serial.println(fadeValue);
}

Related Posts