Monthly Archives: November 2013

Blink a led with style

Basically, when we want to make a LED blink with an Arduino, the program will stick in a loop and hang on delays. Imagine if we want this LED to fade in and out. The program could take all the process time to do it, and the processor won’t be able to do something else. So I imagined a little piece of code that work like this: a list of commands is sent to the program and the LED will turn OFF and ON following these commands. The list of commands is executed, but he program can continue polling other methods. When the program has nothing else to do, it goes back blinking the LED. The timing is respected, but the number of steps to go from the start point to the stop point may vary when the micro-controller is busy doing something else.

The following steps explains how to use the program.

First, list the commands you want the LED do

Create an array containing the scheme that will be played by LED. Each row of this array contains four parameters:

    1. Start brightness;
    2. Stop brightness;
    3. Duration;
    4. Easying (not yet implemented).

The array look like this:

const int commandCount = 4;
int commands[commandCount][4] = {
    {0,255,1000,0}, // 1st command: Start with the LED off then go to full brightness progressively in 1 second.
    {255,255,5000,0}, // 2nd command: Keep the LED at full brightness for 5 seconds.
    {255,0,1000,0}, // 3rd command: Turn the LED off progressively in 1 second.
    {0,0,3000,0} // 4th command: Keep the LED off for 3 seconds.
};

Second, declare your LED

LED myled (LED_PIN, &commands[0][0], commandCount);
  • The first argument is the pin number where the LED is connected.
  • The second argument is the address of the first item of the array.
  • The third argument is the number of commands.

Finally, call the LED loop method in the Arduino loop method

void loop() {
    myled.loop();
}

You can also declare multiple LEDs. They will blink together accordingly to the timing you set.

You can get the source code on my Git account.