i needed a version of the ‘breathing led’ for a project which would not block everything else. button presses and the actual application should run without delay. after all it would just be cosmetics
the result looks like this:
here is my non-blocking code for a breathing led with arduino:
/*
"Breathing non-blocking sleep LED."
Florian Klien 2011
blog.flo.cx
based on work from Jeremy Saglimbeni (thecustomgeek.com)
*/
#define LED 5 // any PWM led will do
unsigned long status_breathe_time = millis();
int breathe_delay = 10;
boolean breathe_up = true;
int breathe_i = 15;
void setup() { // bring the LED up nicely from being off
for(i = 0 ; i <= 15; i+=1)
{
analogWrite(11, i);
delay(5);
}
}
void loop()
{
nonBlockingBreathe();
otherImportantNonBlockingStuff();
}
void nonBlockingBreathe(){
if( (status_breathe_time + breathe_delay) < millis() ){
analogWrite(LED, breathe_i);
status_breathe_time = millis();
if (breathe_up == true){
if (breathe_i > 150) {
breathe_delay = 4;
}
if ((breathe_i > 125) && (breathe_i < 151)) {
breathe_delay = 5;
}
if (( breathe_i > 100) && (breathe_i < 126)) {
breathe_delay = 7;
}
if (( breathe_i > 75) && (breathe_i < 101)) {
breathe_delay = 10;
}
if (( breathe_i > 50) && (breathe_i < 76)) {
breathe_delay = 14;
}
if (( breathe_i > 25) && (breathe_i < 51)) {
breathe_delay = 18;
}
if (( breathe_i > 1) && (breathe_i < 26)) {
breathe_delay = 19;
}
breathe_i += 1;
if( breathe_i >= 255 ){
breathe_up = false;
}
}else{
if (breathe_i > 150) {
breathe_delay = 4;
}
if ((breathe_i > 125) && (breathe_i < 151)) {
breathe_delay = 5;
}
if (( breathe_i > 100) && (breathe_i < 126)) {
breathe_delay = 7;
}
if (( breathe_i > 75) && (breathe_i < 101)) {
breathe_delay = 10;
}
if (( breathe_i > 50) && (breathe_i < 76)) {
breathe_delay = 14;
}
if (( breathe_i > 25) && (breathe_i < 51)) {
breathe_delay = 18;
}
if (( breathe_i > 1) && (breathe_i < 26)) {
breathe_delay = 19;
}
breathe_i -= 1;
if( breathe_i <= 15 ){
breathe_up = true;
breathe_delay = 970;
}
}
}
}
original code came from thecustomgeek.com















