Created
January 7, 2017 14:28
-
-
Save kmark/c3156cb2fd073b70bfb9dc7fcf246287 to your computer and use it in GitHub Desktop.
This file contains 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
/* Non-blocking Breathing LED | |
* Kevin Mark, 2017 | |
* https://kmark.io | |
* | |
* Adapted from: | |
* Breathing sleep LED, like on a Mac. | |
* Jeremy Saglimbeni 2011 | |
* http://thecustomgeek.com/2011/06/17/breathing-sleep-led/ | |
*/ | |
static const unsigned char LED_PIN = 3; | |
void setup() { | |
// Bring the LED up nicely from being off | |
pinMode(LED_PIN, OUTPUT); | |
for (int i = 0; i <= 15; i++) { | |
analogWrite(LED_PIN, i); | |
delay(5); | |
} | |
} | |
void loop() { | |
breathe(); | |
} | |
void breathe() { | |
static unsigned long next = 0; | |
unsigned long mil = millis(); | |
if (mil < next) { | |
return; | |
} | |
static unsigned char i = 15; | |
analogWrite(LED_PIN, i); | |
next = mil; | |
if (i > 150) { | |
next += 4; | |
} | |
else if (i > 125) { | |
next += 5; | |
} | |
else if (i > 100) { | |
next += 7; | |
} | |
else if (i > 75) { | |
next += 10; | |
} | |
else if (i > 50) { | |
next += 14; | |
} | |
else if (i > 25) { | |
next += 18; | |
} | |
else if (i > 1) { | |
next += 19; | |
} | |
static char up = 1; | |
if (i == 255 && up) { | |
up = 0; | |
return; | |
} | |
if (i == 15 && !up) { | |
up = 1; | |
next += 970; | |
return; | |
} | |
i += up ? 1 : -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment