In need of assistance with Traffic Light Sim
Hi all,
I just joined this forum and would like a little feedback for a traffic light sim project I have to finish.
I understand most of what I'm doing, but my problem is in·using the correct syntax.
I'm working with an NX1000 Homework board and a BS2·BASIC Stamp. I have it all physically wired correctly.
In peticular, I guess I'm not sure as how to properly use the IF...Then statement. I know what I want it to do but to code it has me frustrated.
The RedDuration and YellowDuration conditions are self-explanitory. The PedGreen and PedRedDuration conditions are pedestrian "go" and "stop" lights at the crosswalks.
The light is supposed to stay green indefinitely until either button is pressed and then it goes to yellow and then·red, then back to green.
my code:
Any feedback would be greatly appreciated.
I just joined this forum and would like a little feedback for a traffic light sim project I have to finish.
I understand most of what I'm doing, but my problem is in·using the correct syntax.
I'm working with an NX1000 Homework board and a BS2·BASIC Stamp. I have it all physically wired correctly.
In peticular, I guess I'm not sure as how to properly use the IF...Then statement. I know what I want it to do but to code it has me frustrated.
The RedDuration and YellowDuration conditions are self-explanitory. The PedGreen and PedRedDuration conditions are pedestrian "go" and "stop" lights at the crosswalks.
The light is supposed to stay green indefinitely until either button is pressed and then it goes to yellow and then·red, then back to green.
my code:
' {$STAMP BS2}
' {$PBASIC 2.5}
' ----Two-Way Traffic Light Sim----
RedLED PIN 15
YellowLED PIN 14
GreenLED PIN 13
PedGreenLED PIN 1
PedRedLED PIN 0
RedDuration CON 10000 ' 10 sec
YellowDuration CON 5000 ' 05 sec
PedGreenDuration CON 10000 ' 10 sec
PedRedDuration CON 5000 ' 05 sec
IF IN4 OR IN3 = 1 THEN
DEBUG CLS
GOTO Pedestrians
ENDIF
DO
' Turn only Green light on indefinitely
DEBUG "The light is GREEN" CR,
DEBUG "Pedestrians STOP" CR,
HIGH GreenLED
HIGH PedRedLED
LOOP UNTIL (IN4 =1) OR (IN3 =1)
Pedestrians:
' Turn only Yellow light on for 05sec
DEBUG "The light is YELLOW" CR,
DEBUG "CAUTION" CR,
DEBUG "Pedestrians WAIT" CR,
LOW GreenLED
HIGH YellowLED
HIGH PedRedLED
PAUSE PedRedDuration
PAUSE YellowDuration
DEBUG CLS
' Turn only Red light on for 10sec
DEBUG "The light is RED" CR,
DEBUG "Pedestrians May Walk" CR,
LOW YellowLED
LOW PedRedLED
HIGH RedLED
HIGH PedGreenLED
PAUSE RedDuration
PAUSE PedGreenDuration
DEBUG CLS
RETURN
Any feedback would be greatly appreciated.
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
I have found that sometime it dose not work the way you would think
IF IN4 = 1·OR IN3 = 1 THEN
DEBUG CLS
GOTO Pedestrians
ENDIF
·
DO
················································ ' Turn only Green light on indefinitely
DEBUG "The light is GREEN" CR,
DEBUG "Pedestrians STOP" CR,
HIGH· GreenLED
HIGH· PedRedLED
LOOP UNTIL (IN4 =1) OR (IN3 =1)
You could also try this but may not work as you might think but might be better
IF IN4 = 1·OR IN3 = 1 THEN
DEBUG CLS
GOTO Pedestrians
ENDIF
·
DO WHILE (IN4 = 0) OR (IN3 = 0)
··············································· ' Turn only Green light on indefinitely
DEBUG "The light is GREEN" CR,
DEBUG "Pedestrians STOP" CR,
HIGH· GreenLED
HIGH· PedRedLED
LOOP··
·Try this it should work for you
DO
IF IN4 = 1·OR IN3 = 1 THEN
DEBUG CLS
GOTO Pedestrians
ELSE
··········································· ' Turn only Green light on indefinitely
DEBUG "The light is GREEN", CR
DEBUG "Pedestrians STOP", CR
HIGH· GreenLED
HIGH· PedRedLED
ENDIF
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 2/7/2010 1:18:19 AM GMT
I think you are almost there. You can dispense with the first 4 lines of program code and have your program start at the DO. That will esablish the initial conditions of the lights that need to be on and then wait for the buttons. Turn the LOOP UNTIL command into
IF conditions THEN GOSUB pedestrians
After that a simple LOOP to keep it running around that main loop. The conditions you have are okay for the button inputs, so long as you have them wired up correctly.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Many thanks to those who replied.
' {$STAMP BS2} ' {$PBASIC 2.5} RedLED PIN 15 YellowLED PIN 14 GreenLED PIN 13 PedGreenLED PIN 1 PedRedLED PIN 0 RedDuration CON 5000 ' 05 sec YellowDuration CON 3000 ' 03 sec PedGreenDuration CON 5000 ' 05 sec PedRedDuration CON 3000 ' 03 sec Beginning: ' Turn only Green light on indefinitely DEBUG "The light is GREEN", CR DEBUG "Pedestrians STOP", CR HIGH GreenLED HIGH PedRedLED PAUSE GreenLED PAUSE PedRedLED DO WHILE (IN4 = 0) OR (IN3 = 0) IF (IN4 = 1) OR (IN3 = 1) THEN DEBUG CLS GOSUB Pedestrians ELSEIF (IN3 = 1) AND (IN4 = 1) THEN DEBUG CLS GOSUB Pedestrians ENDIF LOOP Pedestrians: ' Turn only Yellow light on for 05sec DEBUG "The light is YELLOW", CR DEBUG "CAUTION", CR DEBUG "Pedestrians WAIT", CR LOW GreenLED HIGH YellowLED HIGH PedRedLED PAUSE PedRedDuration PAUSE YellowDuration DEBUG CLS ' Turn only Red light on for 10sec DEBUG "The light is RED", CR DEBUG "Pedestrians May Walk", CR LOW YellowLED LOW PedRedLED HIGH RedLED HIGH PedGreenLED PAUSE RedDuration PAUSE PedGreenDuration LOW RedLED LOW PedGreenLED DEBUG CLS GOTO Beginning
There is also a blank one that I use when I an done writing the test demo
I·hope this helps
' ========================================================================= ' ' File...... Traffic Light Sim Project ' Purpose... The Pedestrian "go" and "stop" Lights at the Crosswalks ' Author.... Sp3ctre ' E-mail.... ' Started... ' Updated... ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ========================================================================= ' -----[noparse][[/noparse] Program Description ]--------------------------------------------- ' -----[noparse][[/noparse] Revision History ]------------------------------------------------ ' -----[noparse][[/noparse] I/O Definitions ]------------------------------------------------- Red_LED PIN 15 Yellow_LED PIN 14 Green_LED PIN 13 Ped_Green_LED PIN 1 Ped_Red_LED PIN 0 ' -----[noparse][[/noparse] Constants ]------------------------------------------------------- Red_Duration CON 5000 ' 05 sec Yellow_Duration CON 3000 ' 03 sec Ped_Green_Duration CON 5000 ' 05 sec Ped_Red_Duration CON 3000 ' 03 sec ' -----[noparse][[/noparse] Variables ]------------------------------------------------------- ' -----[noparse][[/noparse] EEPROM Data ]----------------------------------------------------- ' -----[noparse][[/noparse] Initialization ]-------------------------------------------------- Beginning: ' Turn only Green light on indefinitely DEBUG "The light is GREEN", CR DEBUG "Pedestrians STOP", CR HIGH Green_LED HIGH Ped_Red_LED PAUSE Green_LED PAUSE Ped_Red_LED ' -----[noparse][[/noparse] Program Code ]---------------------------------------------------- DO WHILE (IN4 = 0) OR (IN3 = 0) IF (IN4 = 1) OR (IN3 = 1) THEN DEBUG CLS GOSUB Pedestrians ELSEIF (IN3 = 1) AND (IN4 = 1) THEN DEBUG CLS GOSUB Pedestrians ENDIF LOOP ' -----[noparse][[/noparse] Subroutines ]----------------------------------------------------- Pedestrians: ' Turn only Yellow light on for 05sec DEBUG "The light is YELLOW", CR DEBUG "CAUTION", CR DEBUG "Pedestrians WAIT", CR LOW Green_LED HIGH Yellow_LED HIGH Ped_Red_LED PAUSE Ped_Red_Duration PAUSE Yellow_Duration DEBUG CLS ' Turn only Red light on for 10sec DEBUG "The light is RED", CR DEBUG "Pedestrians May Walk", CR LOW Yellow_LED LOW Ped_Red_LED HIGH Red_LED HIGH Ped_Green_LED PAUSE Red_Duration PAUSE Ped_Green_Duration LOW Red_LED LOW Ped_Green_LED DEBUG CLS GOTO Beginning
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 2/9/2010 2:43:15 AM GMT