Shop OBEX P1 Docs P2 Docs Learn Events
Traffic Light problem — Parallax Forums

Traffic Light problem

mad295mad295 Posts: 1
edited 2009-06-12 15:19 in BASIC Stamp
This program I’m having trouble programming and making it work. I would like to change the program and have it hooked up to a timer. At a set time I would like to change the sequence to a four-way flashing red light. Help!!!!!!!!!!!!!
·
' STOPLITE.BS2
'
' {$STAMP BS2}
' {$PBASIC 2.5}
·
'====Input/Output===========================================
·
'·· P5·· EW/red····· P2·· NS/red
'·· P4·· EW/yellow·· P1·· NS/yellow
'·· P3·· EW/green··· P0·· NS/green
·
'====Constants==============================================
·
NSgrn···· CON %00100001·· ' Make NS green, EW red.
NSyel···· CON %00100010·· ' Make NS yellow, EW red.
allRed··· CON %00100100·· ' Make both lights red.
EWgrn···· CON %00001100·· ' Make EW green, NS red.
EWyel···· CON %00010100·· ' Make EW yellow, NS red.
NsgoTime·· CON 8000······ ' Set NS green duration (in milliseconds).
YelTime··· CON 2000······ ' Set duration of any yellow.
EWgoTime·· CON 8000······ ' Set EW green duration.
redOverlap·· CON 1000····· ' Set red/red overlap time.
top10···· CON %1111111111000000·········· ' Mask off lower 6 bits.
btm6···· CON %0000000000111111··········· ' Mask off upper 10 bits.
NSgo···· CON NSgoTime & top10 | NSgrn···· ' 16-bit time/bit pat.
NSwarn···· CON yelTime & top10 | NSyel···· ' 16-bit time/bit pat.
allStop·· CON RedOverlap & top10 | allRed·· ' 16-bit time/bit pat.
EWgo···· CON EWgoTime & top10 | EWgrn···· ' 16-bit time/bit pat.
EWwarn···· CON yelTime & top10 | EWyel···· ' 16-bit time/bit pat.
·
'====Variables===================================================
·
seq·· VAR·· Nib·· ' Current state (0-5) of stoplight sequence.
lkup·· VAR·· Word·· ' Number from lookup table.
·
'====Program=====================================================
·
DIRS = %00111111······ ' Set lower six pins to output.
again:················ ' Endless loop.
FOR seq = 0 TO 5······ ' For each of six stored patterns/times..
· LOOKUP seq,[noparse][[/noparse]NSgo,NSwarn,allStop,EWgo,EWwarn,allStop],lkup ' Get bits.
· OUTS = lkup & btm6·· ' Copy lower 6 bits to pins.
· PAUSE lkup & top10·· ' Set delay to upper 10 bits.
NEXT·················· ' ..and get the next entry from the table.
GOTO again············ ' Repeat endlessly.

Comments

  • CumQuaTCumQuaT Posts: 156
    edited 2009-06-12 03:25
    You can just hook your LEDs up to resistors (about 100ohms is all you need) and put them into pins on your micro, then use the HIGH and LOW commands to turn them on and off and the PAUSE command to control your delays. I think you're overcomplicating a simple application with your code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who are you, and why are you reading my signature?
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-06-12 15:19
    Hi, I like the way you sequence with OUTS and the LOOKUP table. I tend to agree with CumQuat that the way the pause is derived is possibly a little over complicated but then again I have not sat and studied it too much.

    I think the program should work really well especially if you used another lookup table for the pause time (which could be modified to include the "flash time" for the red lights.

    Heres what I mean, it's not working code·just·an illustration
    LOOKUP seq,[noparse][[/noparse]NSgo,NSwarn,allStop,EWgo,EWwarn,allStop,flash],lkup ' Get bits.
    LOOKUP seq,[noparse][[/noparse]8000,2000,8000,8000,2000,8000,flashtimer],PauseTime
    

    .I don't want to distract you from your original thoughts too much but only suggest an alternative.

    Jeff T.
Sign In or Register to comment.