Skip to content

robojay/arduino-LEDFader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Background LED Fading Library for Arduino

Note: Modified by Jay@ReactiveTechnologies.com to allow the use of a helper function to set PWM values on alternative hardware. This README needs to be modified to show an example. Example code does exist in the examples folder.

PWM helper function modification inspired and funded by Todd Hammer, http://www.toddhammerart.com

Original library by Jeremy Gillick, http://blog.mozmonkey.com

========================================

This arduino library can fade individual LEDs in the background without blocking your main program.

Instead of using a for loop and delay to fade an LED from one PWM value to another -- which will block all other processes -- this will check the time each loop cycle to see if the value should be adjusted.

For example, to fade from 0 - 200 in 1 second, it would increment the PWM value by 1 every 50 milliseconds -- without stopping your program while it waits. There are also other optimizations to make the animations smooth and accurate without eating too many chip resources.

Install

Move the arduino-LEDFader folder into the arduino library directory and rename it from arduino-LEDFader to LEDFader.

Then in your sketch you just need to add this to the top of your file:

#include <LEDFader.h>

Simple Examples

Fade an LED on pin 3 up in 3 seconds.

#include <LEDFader.h>

// Create new LED Fader on pin 3
LEDFader led = LEDFader(3);

void setup() {
  // Fade from 0 - 255 in 3 seconds
  led.fade(255, 3000);
}

void loop() {
  // Need to call update each loop cycle to adjust the PWM if needed
  led.update();
}

Building on that example, we can alternate by fading up and down

#include <LEDFader.h>

// Create new LED Fader on pin 3
LEDFader led = LEDFader(3);

void setup() {
  led.fade(255, 3000);
}

void loop() {
  led.update();

  if (led.is_fading() == false) {

    // Fade from 255 - 0
    if (led.get_value() == 255) {
      led.fade(0, 3000);
    }
    // Fade from 0 - 255
    else {
      led.fade(255, 3000);
    }
  }
}

Multiple LEDs or RGB

This example will fade 6 LEDs, all to random PWM values and durations. You could, instead, replace 6 LEDs with 2 RGB LEDs.

#include <LEDFader.h>

#define LED_NUM 6

// 6 LEDs (perhaps 2 RGB LEDs)
LEDFader leds[LED_NUM] = {
  LEDFader(3),
  LEDFader(5),
  LEDFader(6),
  LEDFader(9),
  LEDFader(10),
  LEDFader(11)
};

void setup() {
}

void loop() {

  // Update all LEDs and start new fades if any are done
  for (byte i = 0; i < LED_NUM; i++) {
    LEDFader *led = &leds[i];
    led->update();

    // Set new fade
    if (led->is_fading() == false) {
      int duration = random(1000, 3000); // between 1 - 3 seconds

      // Up
      if (led->get_value() == 0) {
        byte color = random(100, 255);
        led.fade(color, duration);
      }
      // Down
      else {
        led.fade(0, duration);
      }
    }
  }
}

About

An arduino library to fade individual LEDs in the background without blocking your main program.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Other 57.2%
  • C++ 42.8%