Bicolor LED Library

This is a simple library to drive a two-lead bicolour LED that's been attached to a pair of pins. It lets you drive both solid colors, as well as allowing you to put the two together "simultaneously" by passing AC current through, rapidly turning on one color, then the other.

For the purposes of this page, I'm assuming you have a red/green LED, but they come in a number of other colors as well. Just alter 'red' and 'green' to whatever two colors your LED supports.

Download

How to use

Instantiating

To declare an instance, just use

BiColorLED led=BiColorLED(pin1,pin2);
where pin1 and pin2 are the two pins you connected the LED to.

Colour Codes

Bi-color LEDs support 4 colors--off, red on, green on, or alternating both on (making yellow). This library represents these colours as 4 numbers:

0Black (off)
1Red
2Green
3Yellow

Basic use

You can set the LED's colour with:

led.setColor(color);
and read it with:
color=led.getColor();

If you're using yellow (colour 3) or blinking, you'll also need to call led.drive(); at least every 9 milliseconds so that the library can alternate the colors without the flickering being obvious. This means that if you use delay();, the LED will freeze in one colour until the delay finishes.

Blinking

You can make the library alternate between two colours with

led.setBlinkSpeed(speed); // In milliseconds
// And to get the blinkspeed:
unsigned long speed=led.getBlinkSpeed();
To stop the blinking, just set the speed to 0.

If you want it to alternate with a colour other than black, you can set the second colour with:

led.setColor(color1, color2);

To set separate delays for each color, call

led.setBlinkSpeed(speed1, speed2);

Also remember to use led.drive(); for the reasons stated above.

Obtaining the version

BiColorLED_H contains the version number of the library, in case you need to check the library version. Note that this number is only incremented when the library itself changes (for example, 1.3 still reports itself as 1.1).

ToDo, Bugs, & Future Features

Eventually, I might also add support for varying the ratio of red to green, as well as the duty cycle for blinking.

No known bugs.

Add any bug reports or feature requests here, post them to this forum thread, or email them to the address listed in BiColorLED.cpp and BiColorLED.h

ChangeLog

  • Version 1.4 add support for separate blink speeds for each color. (12 April 2014)
  • Version 1.3 fix Blink example. (8 February 2014)
  • Version 1.2 added Arduino 1.0 support--thanks to PaulDriver for the patch. (13 December 2012)
  • Version 1.1 added support for alternating two colours at various intervals; updated syntax highlighting files. (25 August 2012)

Share