Digital outputs – More LEDs

Objective:

Control the three diferent colors of the RGB Led.

What we need to know before we start

The KL25Z and K64F come with an RGB Led soldered in the board directly to some digital pins. These pins vary depending the model of the board, but mbed has declared some flriendly names (so you don’t have to worry about learning all the pins) so you can easy remember them. This pins are:

  • For Red color: LED1 or LED_RED
  • For Green color: LED2 or LED_GREEN
  • For Bluecolor: LED3 or LED_BLUE

The KL46Z has only two separated leds, LED1 for color red and LED2 for color green.

Step 1. Adding new digital output pins

Create a new program using the same template (Blinky LED Hello World). In the previous exercise you only had declared one digital pin as output using “DigitalOut”, now you will need to configure two more pins as digital outputs too. Add the following lines:

[cpp]
DigitalOut red_led(LED1);
DigitalOut green_led(LED2);
DigitalOut blue_led(LED3);
[/cpp]

Step 2. Combining colors

So we want to show the three colors (red, green and blue) and the four combinations of these colors (Cyan, yellow, magenta and white).

[cpp]
int main(){
// Turn off all Leds
red_led = 1;
green_led = 1;
blue_led = 1;
while(1){
// Turn on red color
red_led = 0;
wait(1);
// Turn on green color, first turn off red led
red_led = 1;
green_led = 0;
wait(1);
// Turn on blue color
green_led = 1;
blue_led = 0;
wait(1);
// Now blue and green = cyan
green_color = 0; // Blue still on at this point
wait(1);
// Green and red = yellow
blue_led = 1;
red_led = 0;
wait(1);
// Red and blue = magenta
green_led = 1;
blue_led = 0; // Remember that red still on at this point
wait(1);
// Red + blue + green = white
green_led = 0;
wait(1);
// Turn all off
red_led = 1;
green_led = 1;
blue_led = 1;
wait(1);
[/cpp]

Code

You can find the complete program at: mbed.org/users/gcarmonar/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s