In need of assistance with Traffic Light Sim
Sp3ctre
Posts: 2
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··that you may have and all of your time finding them
·
·
·
·
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.
There is also a blank one that I use when I an done writing the test demo
I·hope this helps
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 2/9/2010 2:43:15 AM GMT