Arduino Breathing LEDs - Inspired From thecustomgeek.com

Nishant Arora 13/Jan/2013
Facebook
Twitter
LinkedIn
Reddit

hi All,

Almost a year ago, I was working on my final year project, when I cam across this awesome breathing LED tutorial on thecustomegeek.com. There I commented that this could be achieved more easily with the MOD function, but did not follow up with the article. The writer another nice dude, followed up with me to give an update on the same. Thanks to bullsh*t facebook messaging system, it went to "other messages", which were never in sight.

Long story short, I do not remember what was on my mind while writing that comment, still I can give in a solution, that just works, is more easy to manage and at the same time it saves us almost 200 bytes on the memory (for those who are wondering why 200bytes is so crucial, that's almost 3% memory of a 8kb flash module on arduino uno and is almost 14% reduction than the previous code.)

So here is my updated version (I did not have a board in hand, I tested this on the simulator available here)

/*
"Breathing sleep LED, like on a Mac.
Jeremy Saglimbeni 2011
thecustomgeek.com

Updated by Nishant Arora 2013
nishantarora.in

LED is attached to pin 11 in series with a 5.6K resistor
*/

int i = 0;
int d = 0;
int breathe_speed  = 1;    // 1 being the fastest
void setup(){ // bring the LED up nicely from being off
  for(i = 0 ; i <= 15; i+=1){
    analogWrite(11, i);
    delay(5);
  }
}
void loop(){
  for(i = 15 ; i <= 255; i++){
    analogWrite(11, i);
    d  = (i/30)*breathe_speed;
    delay(d);
  }
  for(i = 255; i >=15; i--){
    analogWrite(11, i);
    d  = (i/30)*breathe_speed;
    delay(d);
  }
  delay(970);
}

Lemme know in case of any further updates, or errors!... I loved coding it.

Cheers!