You certainly don't want to go directly from green to red without passing through yellow; that would really cause a pile-up! Would you ever want to go from red to yellow? I have seen a red-yellow-green progression in a downtown area (I think in Colorado), apparently to move traffic through an intersection as fast as possible. And, of course, there is always the Christmas tree at the drag races! You can decide whether to add a yellow-green transition or remove the red-yellow one; I'll just leave it as is.
Defining and Implementing the Actions
What actions do we need? We need to turn on and turn off each of the three colored lights. As the project is defined at the moment, we could define three compound actions: a Turn on Green that also turned the others off, and its equivalents for the other colors. But if we define six simple actions instead (turn off and turn on for each of the three colors), we will have the flexibility to implement the red and yellow pedestrian light used in Boston and the two-color traffic light that I saw a long while ago in Upstate New York, where the role of the yellow light was filled by turning on red and green simultaneously. More important for the tutorial, we'll get to specify both Entry and Exit Actions for our states.
This project builds on the first project, so go back and do that, if you haven't already. Then...
Open Demo_ABC.vi, so we'll have access to all the LabVIEW and LabHSM capabilities;
Save it as Demo_TrafficLight.vi, to start the new project and disconnect from the old;
Open the block diagram and scroll so that the case label of the Actions Case Structure is visible (Figure 3).
Open the LabHSM Editor... from the Tools menu.
Click the OPEN button,
SAVE AS Demo_TrafficLight.hsm, to connect it with the VI in memory.
Click the Actions List button to open the Actions List Editor.
Push the LabHSM Editor mostly off screen and position the Actions List Editor as in Figure 3.
Click Add New After.
Type Turn on Green in the dialog box (Figure 3).
Click the OK button or hit the return key to close the dialog.
"Whamo!" The screen flashes and you have a new action case as well as a new action in the list (Figure 4).
This new case looks empty but, if you scroll down, you'll find that it is not. The FSM Frequently Used Data cluster is wired across the bottom of the case. This wiring was needed to keep the VI from breaking when we added the new case. When you ask for a New action, you get a copy of the Do Nothing action. In fact, although the action itself has been added where we asked, you'll find the case listed immediately after the Do Nothing case. To avoid confusion, I'd suggest reordering the cases using the pop-up Rearrange...>>Sort. We'll do that later, so you can skip it for now if you'd like.
Now add the code to actually turn the green light on. First, we need a green light. If necessary, scroll back to the top of the case near the Turn on Green label.
Place a Classic Round Light on the front panel and label it Green.
The terminal should appear near the top of the Turn on Green case in the diagram.
Pop-up on the terminal and create a constant.
Click the constant to TRUE, since this is a "turn on" action (Figure 5).
To get the other lights,
Click Add Copy After;
Type over Green with Yellow;
Click OK or hit return;
Repeat, replacing Yellow with Red.
It doesn't look like anything happened on the block diagram this time, but it did! Click the increment button of the case label (or use the drop down list) and you'll move to the Turn on Yellow case, right after the Turn on Green, as you directed.
Relabel the Green 2 terminal Yellow and the Green 3 terminal Red.
If you've been following exactly, the Turn on Red action is still highlighted in the Actions List Editor. Let's create the corresponding Turn off Red action.
Click Add Copy After.
Type over on with off.
Select the Turn off Red case. The following operations change it to match Figure 6, above.
Pop-up on Red 2 and create a local variable.
Click the Red 2 local variable and set it to Red, so it controls the correct light.
Delete the Red 2 indicator; we don't need it.
Wire the constant to the local variable.
Change the constant to FALSE.
Now the Red actions are correct. Duplicate this last action to finish the Yellow and Green.
Click Add Copy Before.
Type over Red with Yellow.
Click Add Copy Before.
Type over Yellow with Green.
Once again, the action cases are not in the order of the actions list.
Select the Turn off Yellow action and set its local variable to Yellow.
Select the Turn off Green action and set its local variable to Green.
Drag the Actions around the actions list to match Figure 6, above, or some other reasonable order.
Pop-up on the case label and Rearrange...>>Sort to get the case order to match the list.
With all the actions now defined, we need to assign them to their logical places as Entry and Exit Actions. In the LabHSM Editor:
Select state A in the lefthand window;
Click Rename, and rename it Red (Figure 7).
(The righthand window automatically updates to the new name.)
Click the first Entry Actions for Selected State.
Select the new Turn on Red action from the pop-up.
Click the first Exit Actions for Selected State.
Select Turn off Red from the pop-up.
(Note how intuitive all this is. Very little chance for errors and very few surprises for the next programmer to access the project.)
Figure 7: Actions for the Red State
Repeat this procedure to turn B into Green and C into Yellow.
The Traffic Light Itself
At the moment, the indicators on the front panel don't look much like a traffic light. Only the Green one is even the right color.
Temporarily drop a Classic Round LED on the panel.
Use the eyedropper to copy the on-state and off-state colors (separately) from the LED.
Apply each to the Red traffic light with the paint brush (Figure 8).
Delete the LED.
For the Yellow light,
Click on the paint brush's background color chip;
Choose the More Colors... palette option (Figure 8);
Match the red intensities to the green (Figure 9).
Now that the lights are colored, you can
Hide their labels;
Stack them with Red, on top;
Align them horizontally;
Distribute them vertical compressed (Figure 8).
(In a project for a client, I would have created some appropriate background artwork, but this is only a demo and that would not have taught you anything useful.)