| 1 |
/* |
| 2 |
Fading |
| 3 |
|
| 4 |
This example shows how to fade an LED using the analogWrite() function. |
| 5 |
|
| 6 |
The circuit: |
| 7 |
- LED attached from digital pin 9 to ground through 220 ohm resistor. |
| 8 |
|
| 9 |
created 1 Nov 2008 |
| 10 |
by David A. Mellis |
| 11 |
modified 30 Aug 2011 |
| 12 |
by Tom Igoe |
| 13 |
|
| 14 |
This example code is in the public domain. |
| 15 |
|
| 16 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fading |
| 17 |
*/ |
| 18 |
|
| 19 |
int ledPin = 9; // LED connected to digital pin 9 |
| 20 |
|
| 21 |
void setup() { |
| 22 |
// nothing happens in setup |
| 23 |
} |
| 24 |
|
| 25 |
void loop() { |
| 26 |
// fade in from min to max in increments of 5 points: |
| 27 |
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { |
| 28 |
// sets the value (range from 0 to 255): |
| 29 |
analogWrite(ledPin, fadeValue); |
| 30 |
// wait for 30 milliseconds to see the dimming effect |
| 31 |
delay(30); |
| 32 |
} |
| 33 |
|
| 34 |
// fade out from max to min in increments of 5 points: |
| 35 |
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { |
| 36 |
// sets the value (range from 0 to 255): |
| 37 |
analogWrite(ledPin, fadeValue); |
| 38 |
// wait for 30 milliseconds to see the dimming effect |
| 39 |
delay(30); |
| 40 |
} |
| 41 |
} |