Posted by SeeDoubleYou on 10th November 2009

Fade a led based on interval

During some of the tinkering moments while working on the sCAPE interactive lounge (more on that to come!), I wanted a pulsating LED. I use an arduino duemilanova to control everything. This is an open source hardware/software platform that makes prototyping easy enough that even students from architecture can do it (sorry guys, just a joke (truth lies in humor)).

Well, the pulsing of A LED is not that hard to make, but I wanted the interval to be able to change without using delays and keeping track of the value of every LED. So I started thinking of a way to get the value of a LED (or any other thing for that matter) based on an interval. This resulted in the following function. It is now way bigger than version 1.0 (lets call this version 1.17, don’t no why but .17 feels just right you know). It has more functionality than just fading. I’ll just give you the code and than I will explain it a bit more.

float getMappedLedValueIntervalBased(int interval, boolean fade,
                                     float minValue, float maxValue,
                                     long offset)
{
    float currentStep = (currentTime + offset) % (2*interval);
    float value;
    if(fade)
    {
        float valueDifference = currentStep/interval;
        value = currentStep < interval ? 1- valueDifference : valueDifference -1;
        value = mapFloat(value, 0, 1, minValue, maxValue);
    }
    else
    {
        value = currentStep < interval ? HIGH : LOW;
    }
    return value;
}

As You can see you can also tell the function to give a blink or a fade (pulsing) value, a minimum and a maximum value (between 0 and 1)  and an offset (useful when dealing with more LEDs). This returns a value between 0 and 1, which than can be mapped to the corresponded value (0-255 when using standard LEDs). This function is done (we’re going for v2.17). What I want to add is the ability to have a different on- and off-time. This will lead to a functionality like, for example, LED fades in in two seconds, fades out in 5.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • email
  • LinkedIn
  • PDF
  • Reddit
  • Slashdot
  • StumbleUpon
  • Symbaloo
  • Suggest to Techmeme via Twitter
  • Twitthis

No comments yet!

Post your comments