Advertisement
Guest User

Fade (Arduino IDE)

a guest
Apr 1st, 2012
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. /*
  2.  Fade
  3.  
  4.  This example shows how to fade an LED on pin 9
  5.  using the analogWrite() function.
  6.  
  7.  This example code is in the public domain.
  8.  
  9.  */
  10. int brightness = 0;    // how bright the LED is
  11. int fadeAmount = 5;    // how many points to fade the LED by
  12.  
  13. void setup()  {
  14.   // declare pin 9 to be an output:
  15.   pinMode(9, OUTPUT);
  16. }
  17.  
  18. void loop()  {
  19.   // set the brightness of pin 9:
  20.   analogWrite(9, brightness);    
  21.  
  22.   // change the brightness for next time through the loop:
  23.   brightness = brightness + fadeAmount;
  24.  
  25.   // reverse the direction of the fading at the ends of the fade:
  26.   if (brightness == 0 || brightness == 255) {
  27.     fadeAmount = -fadeAmount ;
  28.   }    
  29.   // wait for 30 milliseconds to see the dimming effect    
  30.   delay(30);                            
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement