LPC812 MAX Experiment: Multi LED

Experiment: Control Multiple LEDs

In this experiment you will learn how to control multiple I/O pins simultaneously. More specifically you will learn how to control six LEDs. This experiment builds on the knowledge you have gained from the previous experiments.

1) Hardware

In this lab you will need:

  • 1x breadboard
  • 3x push-button
  • 9x 330 ohm resistor
  • 2x yellow LED
  • 2x green LED
  • 2x red LED
  • cables

Mount the components on the breadboard and connect the breadboard to the LPC812 as show in the image below.

Breadboard Setup

The pin(s) are specified in the mbed library with the actual pin names as well as some useful aliases:

Schematic Namembed Pin NameArduino Shield Alias
PIO0_0P0_0D0
PIO0_4P0_4D1
PIO0_8P0_8D3
PIO0_17P0_17D8
PIO0_16P0_16D9
PIO0_13P0_13D10
PIO0_14P0_14D11
PIO0_15P0_15D12
PIO0_12P0_12D13

1) Description

In this experiment the 6 LEDs shall be controlled in a running-one pattern. First let the running rate be fixed. Use the mbed library's wait function as the time base. The program structure is suggested to be as below. 1) Wait a fixed time, 2) Update the state variable (or counter, which is technically also a state) and 3) set outputs according to state.

Information

Note that only one of the push-buttons is used in this experiment, the other two will be used in the following experiments.

main.cpp

#include "mbed.h"

// Declare variables

int main()
{
    // Set LED start values

    // Enter forever loop
    while(1) {
        // Delay a specified period of time or wait for push-button to be pressed
        ...

        // Update state/counter
        ...

        // Update LEDs according to state/counter
        ...
    }
}

Since there are 6 LEDs is would be suitable to define 6 states or having a counter count between 0-5 (or 1-6, if that makes more sense). The "set outputs according to state" can be discussed in more detail. One method is to first reset all outputs to their inactive state. In our case, that means setting the 6 LED outputs high (which will turn the LEDs off). After that, the state/counter determines which output to set low (turn on one LED). This structure will save code since the resetting to default state is done only once.

Another method, also outlined below, is to set all outputs to correct levels, in each state. This will duplicate much code but the program can possibly be easier to understand and maintain.

// Reset all outputs
...
// Set only the active output
switch(...)
{
    case ...: ... break; //Set only active output
    case ...: ... break; //Set only active output
    case ...: ... break; //Set only active output
    default:
}

or

//Set and reset the outputs
switch(...)
{
    case ...: ... break; //Set only active output and reset all others
    case ...: ... break; //Set only active output and reset all others
    case ...: ... break; //Set only active output and reset all others
    default:
}

It is also possible to experiment with other patterns than the running-one. For example, having 3 LEDs on simultaneously.

When the fixed running rate is working, adjust the program so that a push-button press is advancing the running LEDs instead of time. Simply replace the delay function with a while-loop that waits for a push-button press.

A little twist on above is to add timeout functionality. If the push-button is not pressed for 5 seconds, the running LEDs should be advanced one step. How to implement that?

Tip: sample the push-button at a fixed (known) rate. Count how many times sampling is done. If too many samples without detecting a press, then a timeout has occurred. Define the timeout with a constant (#define ...).

2) Hardware

The same setup is used so no changes are needed.

2) Description

The experiment can only be done partially on the breadboard. The goal is to generalize the previous program. Use two push-buttons for increasing and decreasing the LED running-one speed. Use one more push-button to change the direction direction of the LED running-one pattern and finally use one push-button for start/stopping the LED flashing.

Information

After completing the I2C Experiment you can use the push-button on the LPC812 MAX Board to start/stop the LED flashing.

All-in-all, four push-buttons are needed for this. Only three are available for mounting on a breadboard. Develop the program in steps. First develop the variable speed solution. Then set the speed to a fixed value and continue developing the direction control. Then fix the direction and develop the start/stop control. After these three steps all functionality has been developed. Use the breadboard setup as illustrated in the schematic above.

Solution(s)

Import programlpc812_exp_solution_multi-led

Solutions for the Multi LED experiments for LPC812 MAX


Please log in to post comments.