HTML | CSS | JavaScript | ASP.NET | PHP | C# | Java | VB.NET | MS SQL | MySQL | Flash
Traffic Light Simulation using the Basic Stamp 2
So, it took me long enough right? I know in my last post I left off by saying that I would try to have at least 1 project that is build with the Basic Stamp 2 and include it on my next post. I came up with a Traffic Light simulation which I thought was a very practical application and I am very confident that everyone reading this blog is familiar on how a traffic signal works.
Initially, I thought about posting screenshots of the Homework Board circuit; but I felt that these types of projects would be best to demonstrate with a video.
Here is the circuit schematic that I have constructed using ExpressPCB which is absolutely FREE!
As you can see, the connections on the left represents the I/O pins of the Basic Stamp. Then there is a series connection of a 220 ohm resistor and the LED for the traffic signals and cathode or the minus (-) or the flat end of the LED is connected to GRND. I think it’s worth mentioning that when an I/O pin goes HIGH each circuit would have a source voltage of +5 volts and when the I/O pin goes LOW then the source voltage goes down to 0 volts. In this circuit configuration, the LED is configured as Active High which means that when I send a HIGH signal to an I/O pin, the LED is turned on and a LOW turns the LED off. According to the documentation for the Basic Stamp, each I/O pin can only source up to 20 mA of current if only 1 pin is connected to a circuit. However, if we are using a group of pins, we can only source up to 40 mA. So, we have to be careful not to supply our circuit with too much current or we can risk damaging our Basic Stamp that is the reason I included a 220 ohm resister to limit the current flowing through the circuit. Also, take into the account that the Basic Stamp Homework Board has built-in 220 ohm resistors as a safety feature but I didn’t want to take any changes so I added my own. But, if you don’t decide to use the resistor, then you LED would light up more brightly than in the video. Good. That is out of the way.
Ok. Now here is the code that I used to get all of this done. Remember, the Basic Stamp is programmed using PBASIC so the code should be very easy to read.
' ========================================================================= ' {$STAMP BS2} ' {$PBASIC 2.5} ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' SIMPLE TRAFFIC LIGHT CIRCUIT SIMULATION ' Traffic Light Transition: ' RED ' GREEN ' YELLOW ' -----[ I/O Definitions ]------------------------------------------------- RedLED PIN 15 YellowLED PIN 10 GreenLED PIN 5 RedDuration CON 20000 '-- 20 sec GreenDuration CON 20000 '-- 20 sec YellowDuration CON 10000 '-- 10 sec ' -----[ Constants ]------------------------------------------------------- ' -----[ Variables ]------------------------------------------------------- ' -----[ EEPROM Data ]----------------------------------------------------- ' -----[ Initialization ]-------------------------------------------------- Reset: ' -----[ Program Code ]---------------------------------------------------- Main: DO '-- turn only Red light on for 20sec HIGH RedLED LOW YellowLED LOW GreenLED PAUSE RedDuration '-- turn only Green light on for 20sec LOW RedLED HIGH GreenLED PAUSE GreenDuration '-- turn only Yellow light on for 10sec LOW GreenLED HIGH YellowLED PAUSE YellowDuration LOOP END ' -----[ Subroutines ]-----------------------------------------------------
Hopefully, the code is strait forward. As you can see I set up some variables to hold the numbers of the I/O pins I am using. Then I have some variables to represent the duration that each LED will be turned on. Then if you jump down to the main body of the code, you would see that this code would run in a continuous loop and will never end until the battery is disconnected or runs out.
Then, you can see that I first turn on the Red LED by sending a HIGH to that I/O pin while setting all the other pins to LOW. Then I pause for the duration specified in the variable and then everything else is pretty cut and dry. I just turn on and off the other lights in the sequence that I want. Pretty simple huh?
Hopefully, this was helpful to you and you can see how easy Microcontrollers are to work with these days. I welcome any questions or comments you may have. Until next time…
Happy Coding!
| Print article | This entry was posted by admin on July 10, 2008 at 1:11 pm, and is filed under Basic Stamp, Electronics, Microcontrollers. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 1 year ago
Saw your video on YouTube, and thought I’d improve on it a bit.
BTW, with the homework board, you don’t need the resistors, just make sure that the long leads of the LED’s are installed nearest the ports.
Have fun!
Try this:
‘{$STAMP BS2}
‘{$PBASIC 2.5}
‘Vehicle Stoplight Controller w/Pedestrian Signals
‘Author: Bob Holden – EAWF 7/7/2009
‘==================================================
‘ Initialization
‘ LED Setup:
‘ North/South Signals:
‘ Walk Signals:
NSPG PIN 0 ‘P0 – Green
NSPR PIN 1 ‘P1 – Red
‘ Traffic Signals:
NSTG PIN 3 ‘P3 – Green
NSTY PIN 4 ‘P4 – Yellow
NSTR PIN 5 ‘P5 – Red
‘ East/West Signals:
‘ Walk Signals:
EWPG PIN 10 ‘P10 – Green
EWPR PIN 11 ‘P11 – Red
‘ Traffic Signals:
EWTG PIN 13 ‘P13 – Green
EWTY PIN 14 ‘P14 – Yellow
EWTR PIN 15 ‘P15 – Red
second CON 1000 ‘Length of Second in Milliseconds
x VAR Word ‘Counters
y VAR Word
direction VAR Word : direction = 0 ‘current directional state
Start:
GOSUB Test
GOSUB AllRed
PAUSE 5*second
Main:
DO
GOSUB AllRed
PAUSE 3*second
SELECT direction
CASE 0
TOGGLE NSTR
TOGGLE NSTG
TOGGLE NSPR
TOGGLE NSPG
PAUSE 10*second
TOGGLE NSPG
FOR x = 0 TO 18
TOGGLE NSPR
PAUSE second/2
NEXT
PAUSE second*2
TOGGLE NSTG
TOGGLE NSTY
PAUSE 3*second
direction = 1
CASE 1
TOGGLE EWTR
TOGGLE EWTG
TOGGLE EWPR
TOGGLE EWPG
PAUSE 10*second
TOGGLE EWPG
FOR x = 0 TO 18
TOGGLE EWPR
PAUSE second/2
NEXT
PAUSE second*2
TOGGLE EWTG
TOGGLE EWTY
PAUSE 3*second
direction = 0
ENDSELECT
LOOP
END
‘ SUBROUTINES
AllRed:
GOSUB AllOff
HIGH NSTR
HIGH EWTR
HIGH NSPR
HIGH EWPR
RETURN
END
Test:
FOR y = 0 TO 3
FOR x = 0 TO 15
‘ Valid Lights @ 0,1,3,4,5,10,11,13,14,15
IF (x=2 OR x=6 OR x=7 OR x=8 OR x=9 OR x=12) THEN Skipit
TOGGLE x
PAUSE second/10
Skipit:
NEXT
NEXT
RETURN
END
AllOff:
FOR x = 1 TO 15
LOW x
NEXT
RETURN
END
AllOn:
FOR x = 1 TO 15
HIGH x
NEXT
RETURN
END
about 1 year ago
@Bob
Wow. Nice work! When I get some time, I will have to plug this in when I get some time. Yes, you are right about the resistors. Thanks
about 11 months ago
Thanks for this whole coding… it helped me a lot!