User manual
LPCXpresso Experiment Kit - User’s Guide
Page 47
Copyright 2013 © Embedded Artists AB
//Update LEDs according to state/counter
...
}
Since there are 8 LEDs is would be suitable to define 8 states or having a counter count between 0-7
(or 1-8, 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
8 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 then the running-one. For example, having 3 LEDs
on simultaneous.
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 a 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 ...).
7.4.2 Lab 3b: Control of Running-One Pattern
The experiment can only be done partially on the breadboard. The goal is to generalize the program
from Lab 3a. Use two push-buttons for increasing and decreasing the LED running-one speed. Use
two more push-buttons for controlling the direction of the LED running-one pattern, and finally use one
push-button for start/stopping the LED flashing.
All-in-all, five push-buttons are needed for this. Only two 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