March 5, 2015

Animation of five LEDs using Arduino PWM output pins

This example is part of the ALA (Arduino Light Animation) library and shows how to create animations using some LEDs and an Arduino board.
Checkout this small video to see how the animations look like.



Circuit

The circuit is really simple. Just five red LEDs connected to five analog output pins of the Arduino and five 220 Ohm resistors.



Code

You have to download and install the ALA library from here first.

#include <AlaLed.h>

AlaLed leds;
byte pins[] = { 5, 6, 9, 10, 11 };

AlaSeq seq[] =
{
  { ALA_FADEIN, 1000, 1000 },
  { ALA_ON, 1000, 1000 },
  { ALA_FADEOUT, 1000, 1000 },
  { ALA_BARSHIFTRIGHT, 1000, 1000 },
  { ALA_BARSHIFTLEFT, 1000, 1000 },
  { ALA_OFF, 1000, 1000 },
  { ALA_PIXELSHIFTRIGHT, 700, 1400 },
  { ALA_PIXELSHIFTLEFT, 700, 1400 },
  { ALA_BLINKALT, 500, 3000 },
  { ALA_PIXELSMOOTHSHIFTRIGHT, 1000, 4000 },
  { ALA_PIXELSMOOTHSHIFTLEFT, 1000, 4000 },
  { ALA_FADEINOUT, 1000, 4000 },
  { ALA_COMET, 1000, 4000 },
  { ALA_GLOW, 1000, 4000 },
  { ALA_STROBO, 200, 4000 },
  { ALA_ENDSEQ, 0, 0 }
};


void setup()
{
  leds.initPWM(3, pins);
  leds.setAnimation(seq);
}

void loop()
{
  leds.runAnimation();
}

I have designed the ALA library with simplicity in mind. You just need to initialize the AlaLed object in the setup function passing the number of LEDs you want to drive. Then you call the runAnimation method passing an array of triplets to describe the desired animation sequence. Finally you have to call the runAnimation method in the loop function to animate your LEDs.
The tricky part is the AlaSeq structure that is made by 3 fields:
  1. A numeric code identifying the desired animation. Valid codes are listed in the Ala.h header file.
  2. The animation loop duration in milliseconds.
  3. The animation total duration in milliseconds.
The last entry in the array must be ALA_ENDSEQ to close the animation sequence.

1 comment:

  1. Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. https://krmlight.com/2000-lumen-led-bulb/

    ReplyDelete

Note: Only a member of this blog may post a comment.