Fade Arduino Sketch¶
This is a standard Arduino example sketch, copied here for reference.
Full Source Code¶
The full code is all in one file Fade.ino.
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
10int led = 9; // the pin that the LED is attached to
11int brightness = 0; // how bright the LED is
12int fadeAmount = 5; // how many points to fade the LED by
13
14// the setup routine runs once when you press reset:
15void setup() {
16 // declare pin 9 to be an output:
17 pinMode(led, OUTPUT);
18}
19
20// the loop routine runs over and over again forever:
21void loop() {
22 // set the brightness of pin 9:
23 analogWrite(led, brightness);
24
25 // change the brightness for next time through the loop:
26 brightness = brightness + fadeAmount;
27
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}