Last active
August 29, 2015 14:23
-
-
Save AstroCB/9fd3031d24fe87eb9106 to your computer and use it in GitHub Desktop.
Blinks a light softly by scaling analog values (demo at http://i.imgur.com/dz3ZJUF.gif)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will cause the LED in port D0 to blink softly ("breathe") like the built-in status indicator does when connected to the Internet. | |
// I used a 20 second delay between increments, which seems to be sufficient for a smooth increase or decrease in brightness. | |
// To modify the speed at which the light blinks, change the increment and decrement of output (lines 16 and 24); default is 3 for both. | |
int led = D0; // Attached light | |
int output = 0; // Power to light | |
bool increasing = true; // Decides whether to increase or decrease power to the light | |
void setup() { | |
pinMode(led, OUTPUT); // LED will be used for output (light) | |
} | |
void loop() { | |
if(increasing) { | |
analogWrite(led, output); | |
output += 3; // Change this to change the blinking speed | |
delay(20); | |
if(output >= 255){ // Upper threshold | |
increasing = false; // Start decrementing power | |
} | |
} else { | |
analogWrite(led, output); | |
output -= 3; // Change this to change the blinking speed (usually the same for decrement as is for increment, but that is not required) | |
delay(20); | |
if(output <= 0) { // Lower threshold | |
increasing = true; // Start incrementing power | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment