Last night, I went to the second night of MARS, and learnt about robot computer systems (embedded software, embedded systems, etc.) and made a realistic traffic light set with a Parallax BASIC Stamp II. The lights did everything that real traffic lights did (apart from sense traffic). The code for it is below:
' {$STAMP BS2}
' {$PBASIC 2.5}
' A realistic traffic light simulator
' Coded by RubyGuy and Perlguy
' All code Copyright 2008 by RubyGuy and Perlguy under the GNU General Public Licence
' A lot simpler than the complicated state-driven example, but it can be broken down into states if you wanted to
' Created at MARS at Massey University, School of Engineering and Advanced Technology, Wellington Campus, Week 2
'|--Initialisation--|
pushbtn PIN 0
pedred PIN 1
pedgreen PIN 2
vred PIN 3
vyel PIN 4
vgreen PIN 5
btndown CON 0
i VAR Byte
INPUT pushbtn
OUTPUT pedred
OUTPUT pedgreen
'|--Set default lights on--|
HIGH pedred
HIGH pedgreen
LOW vgreen
HIGH vyel
HIGH vred
'|--Main loop--|
trafficlights:
'|--If the pedestrian presses the button--|
IF pushbtn=btndown THEN buttondown
GOTO trafficlights
'|--Executes when a pedestrian presses button--|
buttondown:
LOW pedred 'Pedestrian red on
PAUSE 3000
HIGH vgreen 'Vehicle green off
LOW vyel 'Vehicle yellow on
PAUSE 3000 'Give cars a chance to slow down
HIGH vyel 'Vehicle yellow off
LOW vred 'Vehicle stop light on
PAUSE 1000 'Give cars a chance to stop
LOW pedgreen 'Pedestrian green on
HIGH pedred 'Pedestrian red off
PAUSE 5000 'Give pedestrians time to cross
HIGH pedgreen 'Pedestrian green off
GOTO flash 'Make red man flash
'|--Flashing man effect - don't start crossing--|
flash:
FOR i = 1 TO 8 'Flash eight times
LOW pedred
PAUSE 500
HIGH pedred
PAUSE 500
NEXT
'|--Set appropriate lights on and off--|
LOW pedred
PAUSE 2000
HIGH vred
LOW vgreen
PAUSE 5000
HIGH pedred
'|--Go back to beginning--|
GOTO trafficlights
'|--We are cool--|
'|--Perl, Python, Ruby and Parallax Inc FTW!--|
Wednesday, September 3, 2008
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment