]> git.piffa.net Git - sketchbook_andrea/blob - basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino
54cbb433e0dbdd4386171bdd098b3ea9c17c2628
[sketchbook_andrea] / basic / pwm / pwm_3_fade_reverser / pwm_3_fade_reverser.ino
1 /*
2 Fade
3  
4  This example shows how to fade an LED on pin 9
5  using the analogWrite() function.
6  This example code is in the public domain.
7  From Arduino for dummies.
8  */
9
10 int led = 9;
11 int brightness = 0;
12 int fadeAmount = 5;
13 // the pin that the LED is attached to
14 // how bright the LED is
15 // how many points to fade the LED by
16 // the setup routine runs once when you press reset:
17
18 void setup() {
19   // declare pin 9 to be an output:
20   pinMode(led, OUTPUT);
21 }
22 // the loop routine runs over and over again forever:
23 void loop() {
24   // set the brightness of pin 9:
25   analogWrite(led, brightness);
26   // change the brightness for next time through the loop:
27   brightness = brightness + fadeAmount;
28   // reverse the direction of the fading at the ends of the fade:
29   if (brightness == 0 || brightness == 255) {
30     fadeAmount = -fadeAmount ;
31   }
32   // wait for 30  milliseconds to see the dimming effect
33   delay(30);
34 }
35
36
37
38
39
40