Arduino Breathing LED Tutorial Revisited

Nishant Arora 23/Sep/2013
Facebook
Twitter
LinkedIn
Reddit

Hi

I had some discussion with folks at thecustomgeeks.com regarding an arduino breathing LED program just like mac. So I wrote a simpler version as the version mentioned there was brute in nature. That version can be accessed here. Today I got my hands over the Arduino Leonardo boards bought from the Indiegogo campaign by Harold Timmis of Borderless Electronics. I admit that this is the best arduino board I ever had in terms of quality and flawless production. The board is ready out of the box and just needs to be connected to a USB and you're good to go. Superb work and awesome campaign. I just regret I only purchased 5, I should have ordered 50 of these. Thanks Harold.

Coming back to the point, when I wrote the program for the discussion thread I had couple of arduinos lying around but all required a UART serial adaptor, which was not functioning properly on my machine (just a fake replica I suppose). Anyway I love the new boards which utilized the micro-USB cables. I have a plenty lying around everywhere in my house. As of now I made some changes to the code, to produce the breathing LED effect but is a bit more smoother and can be varied easily.

Here is the video:

Here is the Arduino Sketch:

/**
 * @author [email protected]
 * LED is attached to pin ledPin in series with a 2K resistor
 */

/**
 * Configuration
 * the higher the breathe speed, the faster it goes, keep it <255/2
*/
int breathe_speed= 10;
int ledPin = 13;

/**
  Code
*/
int i = 0;
int d = 0;
void setup(){
  /**
    not needed actually
  */
}
void loop(){
  for(i = 1 ; i <= 255; i++){
    analogWrite(ledPin, i);
    d  = i/breathe_speed;
    delay(d);
  }
  for(i = 255; i >=1; i--){
    analogWrite(ledPin, i);
    d  = i/breathe_speed;
    delay(d);
  }
  delay(970);
}

My Original Code:

/*
"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);
}

The Original Code from the customgeek.com:

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

LED is attached to pin 11 in series with a 5.6K resistor
*/
int i = 0;
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+=1)
  {
    analogWrite(11, i);
    if (i > 150) {
      delay(4);
    }
    if ((i > 125) &amp;&amp; (i < 151)) {
      delay(5);
    }
    if (( i > 100) &amp;&amp; (i < 126)) {
      delay(7);
    }
    if (( i > 75) &amp;&amp; (i < 101)) {
      delay(10);
    }
    if (( i > 50) &amp;&amp; (i < 76)) {
      delay(14);
    }
    if (( i > 25) &amp;&amp; (i < 51)) {
      delay(18);
    }
    if (( i > 1) &amp;&amp; (i < 26)) {
      delay(19);
    }
  }
  for(i = 255; i >=15; i-=1)
  {
    analogWrite(11, i);
    if (i > 150) {
      delay(4);
    }
    if ((i > 125) &amp;&amp; (i < 151)) {
      delay(5);
    }
    if (( i > 100) &amp;&amp; (i < 126)) {
      delay(7);
    }
    if (( i > 75) &amp;&amp; (i < 101)) {
      delay(10);
    }
    if (( i > 50) &amp;&amp; (i < 76)) {
      delay(14);
    }
    if (( i > 25) &amp;&amp; (i < 51)) {
      delay(18);
    }
    if (( i > 1) &amp;&amp; (i < 26)) {
      delay(19);
    }
  }
  delay(970);
}

Cheers!

Happy Hacking