Home ยป How to fade an LED on pin 9 using the analogWrite() function ?

How to fade an LED on pin 9 using the analogWrite() function ?

Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 This example code is in the public domain.

int brightness = 0;    // how bright the LED is
int brightness2 = 10;    // how bright the LED is
int brightness3 = 20;
int fadeAmount = 2;    // how many points to fade the LED by
int fadeAmount2 = 2;
int fadeAmount3 = 2;

void setup()  {
  // declare pin 9 to be an output:
  pinMode(10, OUTPUT);
    pinMode(9, OUTPUT);
        pinMode(11, OUTPUT);
}

void loop()  {
  // set the brightness of pin 9:
  analogWrite(9, brightness);   
  analogWrite(10, brightness-5);   
    analogWrite(11, brightness-10);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
    brightness2 = brightness2 + fadeAmount2;
        brightness3 = brightness3 + fadeAmount3;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 30) {
    fadeAmount = -fadeAmount ;
  }   
  if (brightness2 == 0 || brightness2 == 30) {
    fadeAmount2 = -fadeAmount2 ;
  }   
  if (brightness3 == 0 || brightness3 == 30) {
    fadeAmount3 = -fadeAmount3 ;
  }
  // wait for 30 milliseconds to see the dimming effect   

  delay(150);   
}

Add Comment

Click here to post a comment

Your email address will not be published. Required fields are marked *