| |
| Menu principal |
 |
|
| |
|
Traffic Light
This project was submitted to Parallax by Mike Gold
Generating a State Engine Traffic Light Control for the BASIC Stamp® 2 using WithClass:
This is a follow-up article to the article Using WithClass and C# to Create a Code Generator for the BASIC Stamp® 2 Microcontroller. In the previous article, we talked about how you could use .NET to construct a PBASIC code generator that generates codes from a state diagram in WithClass for the Basic Stamp 2 module. In this article we have improved the generator slightly to handle time as an event, and to use some of the BASIC Stamp microcontroller's built in functionality to count time ticks in our state engine loop. With this added functionality, we can create the control logic and hardware for a model traffic light with a road sensor.

The first step is to construct a state diagram in WithClass with all of the necessary states and transitions. Figure 2 shows the UML state chart for the control of the traffic light: The light starts initially in the RED state and remains RED until a car runs over the Road Sensor. When the sensor is triggered, it resets the counter and the timer begins counting once every second. After 10 seconds, it goes to GREEN and waits 60 seconds to allow the cars to pass. After 60 seconds, the light goes to Yellow. After 10 more seconds, the car goes back to RED and begins again waiting for the road sensor to trigger. Note that except for the road sensor, all state transitions are triggered by the timer.

So what does our traffic light circuit look like? Our circuit consists of a road sensor (really an oversized pushbutton) and 3 very bright LEDs. (The model is of course scaled down a bit, but the logic is the same.) The state diagram pretty much dictates how we should construct our circuit. Figure 3 shows our traffic light system circuit:
The resulting circuit is pretty simple since most of the functionality is coming from the programming of the BASIC Stamp 2 module. The circuit has a debouncing 10k ohm resistor for the pushbutton and four 220K ohm resistors to pull up the light emitting diodes and the push button. Pressing the pushbutton sends pin 4 on the basic stamp to high which triggers the road sensor. When either pin 15, 14, or 13 on the basic stamp goes high, the corresponding LED is lit.
Except for the addition of the timer, the state engine code that is generated looks fairly the same as in the previous article. The states of the UML state chart are represented in code in each CASE of the SELECT statement. Conditions and Events are tested in if-then statements inside the case of the select. The timer is counted each time through the loop and also pauses each time through the loop for the interval of time specified in the code generator form shown in figure 1. In the case of the traffic light, we pause for 1000 milliseconds or 1 second. Listing 1 shows the complete PBASIC file generated from WithClass. Note that we've also added some beautification to the code to improve overall readability.
State Engine Code Generated for the Traffic Light Control Logic:
'{$STAMP BS2}
'{$PBASIC 2.5}
' WithClass State Engine Generator for Traffic Light State Engine
State VAR Word
TimerCount VAR Word
TimerCount = 0
RED CON 2
WaitForGreen CON 1
GREEN CON 3
Yellow CON 6
State = RED
DO
SELECT State
CASE RED
IF IN4 = 1 THEN ' Car on Road Sensor
TimerCount = 0 ' Reset Timer
State = WaitForGreen
ENDIF
CASE WaitForGreen
IF TimerCount = 30 THEN ' 30 seconds passed
LOW 15 ' Turn off Red
HIGH 13 ' Turn Green
State = GREEN
ENDIF
CASE GREEN
IF TimerCount = 60 THEN ' turn yellow after 1 minute
LOW 13 ' Turn off Green
HIGH 14 ' Turn Yellow
State = Yellow
ENDIF
CASE Yellow
IF TimerCount = 70 THEN ' turn red after 10 seconds
LOW 14 ' Turn off Yellow
HIGH 15 ' Turn Red
State = RED
ENDIF
ENDSELECT
PAUSE 1000
TimerCount = TimerCount + 1
LOOP
Conclusion:
This article explains a practical example of using the State Engine Code Generator for the BASIC Stamp 2 module. By diagramming your state engine first and then generating the code, you can get a better feel for where the hardware should be after each event from the user. With the ease of going directly to PBASIC and immediately programming the BASIC Stamp microcontroller, you have a one-stop solution for creating BASIC Stamp control logic and at the same time producing documentation for your state engine. Although this state engine could probably be coded by hand, the whole thing took me less than an hour (including wiring on the Board of Education® carrier board) with the code generator. The code generator is included free in this article. If you don't own WithClass, you can download a demo copy from Microgold Software Inc. and test out the state engine on your BASIC Stamp projects.
About the Author: Mike Gold is President of Microgold Software Inc. and Creator of WithClass 2000 a UML Design Tool for C#. In the last few years Mike has consulted for companies such as Merrill Lynch and Chase Manhattan Bank in New York. He is been active in developing Visual C++ Applications for 10 years and looks forward to the possibilities in C#. |
|
|