Shop OBEX P1 Docs P2 Docs Learn Events
In need of assistance with Traffic Light Sim — Parallax Forums

In need of assistance with Traffic Light Sim

Sp3ctreSp3ctre Posts: 2
edited 2010-02-09 02:31 in Learn with BlocklyProp
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:
' {$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

  • FranklinFranklin Posts: 4,747
    edited 2010-02-06 04:28
    The best way to get an idea of the code needed is to write out the steps you need and then code them in small blocks. Also a return is for a gosub and I don't see one in your code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-02-07 00:51
    Be careful about using· IF IN4 = 1·OR IN3 = 1· THEN<<<<....and....>>>>LOOP UNTIL (IN4 =1) OR (IN3 =1) in a DO LOOP· with the same Value
    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·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 2/7/2010 1:18:19 AM GMT
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2010-02-07 01:31
    SP3ctre,

    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
  • Sp3ctreSp3ctre Posts: 2
    edited 2010-02-08 07:30
    Ok so after much trial and error I beleive the code is now perfect for what I need it to do.
    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
    
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-02-09 02:31
    I clean your code up a little bit so it is easyer to follow

    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·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 2/9/2010 2:43:15 AM GMT
Sign In or Register to comment.