Shop OBEX P1 Docs P2 Docs Learn Events
block signal color sequence help — Parallax Forums

block signal color sequence help

railroad signalrailroad signal Posts: 5
edited 2012-11-10 07:55 in BASIC Stamp
Hello,

This is my first time writing on the basic stampform. I am also new to the basic stamp.I am using a bs2. I am using P basic language 2.5. I also have Basic StampSyntax and Reference Manual. I am volunteering my time with a none profitorganization. The organization isworking with the railroads in inquiring obsolete signal equipment. We just receiveda 1940s three position signal. It is just like a traffic signal. Except thegreen is on top and yellow in the middle and red on the bottom. What I am trying to accomplish. I am usinggarage door remote controls to control the signal color sequence. Thereceiver has normally open relays. This will be done at a later time. But for know the testing stage I am using twonormally open push buttons. One to start and one to stop. And green, yellow andred LEDs.

I want to operate the block signal color sequence as itdid in the field.

There are two color sequences I want to accomplish. Theway I want it to work.

Train going towardssignal. Color sequence.
All three LEDswill be off at the start. When the startbutton is momentarily pressed. The first color is green then pause 5 secondsthen red pause 5 seconds then yellow pause for 5 seconds then back to green. I want it to keep cycling in this colorsequence until I press the stop button. WhenI momentarily press the stop button. What I would like to have happen is whatever color it is on. I wouldlike it to finish the color sequence and finish to red and pause for fivesecond the then turn off.

Problems that I amhaving train going towards signal color sequence.
When I momentarily press the start button. The pause times seem to be equal. Problem (1) I have to hold down the stop button an tellthe program ends. Momentarily pushing the stop button does not do anything. IfI hold down the stop button right after I momentarily press the start button itwill go green pause then red pause then go to yellow pause then shut off. Problem (2) I want the program to end on rednot yellow then shut off.

Train goingagainst signal. Color sequence.
All three LEDs will be off at the start. When the start buttons is momentarily pressed.The first color will be red pause 5 seconds then green then pause for 5 secondsthen yellow then pause for 5 secondsthen back to red I want it to keep cycling in this color sequence. When Imomentarily press the stop button. What I would like to have happen is whatever color it is on. I wouldlike it to finish the color sequence and finish to green and pause for fiveseconds and then turn off .

Problems that I amhaving train going against signal color sequence.
When Imomentarily press the start button. Problem (1) it starts with red pause thengreen pause then yellow pause then red then there is a extra long pause back togreen. Problem (2) .Momentarily pushing the stop button does not do anything. I have to hold down the stop button an tellthe program ends. Problem (3) theprogram stops on red and the red led stays on I want the program to stop ongreen then pause and shut off.

for now for the learning and testing stage I want to keepit simple and just do one color sequence and one start push button and one stoppush button. I In terms of push buttons there will be three normally open pushbuttons. For a later time. One startbutton for each color sequence and one common stop button. Which I will addboth color sequence and push buttons ina program.

What I need helpon.
What I am looking for is some suggestions and ideas onhow to get the programming to work with the different color sequences.

Thank you for your time.

Outputs - Red Led = 0 , Yellow Led = 1 & Green Led = 2
Inputs - IN14- Start # IN15 - Stop


'Train going towards block signal.

' {$STAMP BS2}
' {$PBASIC 2.5}
DO

Main2:
IF (IN14 = 1) THEN
Main1:
HIGH 2 'Green
PAUSE 5000
LOW 2 'Green
HIGH 0 'Red

PAUSE 5000
LOW 0 'Red
HIGH 1 'Yellow

PAUSE 5000
LOW 1 'Yellow
IF (IN15 = 1) THEN GOTO Main2:
GOTO Main1:
ENDIF
LOOP


'Train going against block signal.
' {$STAMP BS2}
' {$PBASIC 2.5}

DO
Main2:
LOW 2 'Green
IF (IN14 = 1) THEN
Main1:
HIGH 0 'Red
PAUSE 5000
LOW 0 'Red
HIGH 2 'Green

PAUSE 5000
LOW 2 'Green
HIGH 1 'Yellow

PAUSE 5000
LOW 1 'Yellow
HIGH 0

PAUSE 5000
IF (IN15 = 1) THEN GOTO Main2:
GOTO Main1:
ENDIF
LOOP








Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-10-31 06:59
    Your main problem is that the PAUSEs do just that and nothing more. When the Stamp is doing one thing, it can't do anything else like check whether a button is being pushed. The trick is breaking up the PAUSEs into smaller units and checking the button state between short pauses like this:
    wait5:
      quit = 0
      for time = 1 to 50  ' repeat 50 times for a total of 5 seconds
        pause 100  ' a 1/10th second pause won't be noticed
        if in15 = 1 then  ' if button pushed, quit early
          quit = 1 ' indicate that button was pushed
          return
        endif
      next
      return
    
    You'd use a GOSUB wait5 instead of the PAUSE 5000 and follow the next LOW with a IF quit = 1 THEN GOTO main2

    You'll need to define quit as a bit and time as a byte variable.
  • railroad signalrailroad signal Posts: 5
    edited 2012-11-01 14:11
    Mike Green wrote: »
    Your main problem is that the PAUSEs do just that and nothing more. When the Stamp is doing one thing, it can't do anything else like check whether a button is being pushed. The trick is breaking up the PAUSEs into smaller units and checking the button state between short pauses like this:
    wait5:
      quit = 0
      for time = 1 to 50  ' repeat 50 times for a total of 5 seconds
        pause 100  ' a 1/10th second pause won't be noticed
        if in15 = 1 then  ' if button pushed, quit early
          quit = 1 ' indicate that button was pushed
          return
        endif
      next
      return
    
    You'd use a GOSUB wait5 instead of the PAUSE 5000 and follow the next LOW with a IF quit = 1 THEN GOTO main2

    You'll need to define quit as a bit and time as a byte variable.


    Dear Mike Green,
    Thanks for your help. I really do not understand where toput the GOSUB wait5. I tried toreplace the pauses with it but all I got was Underfined label .When Itried to down load it to the bs2. And where do I put thenext LOW with a IF quit = 1 THEN GOTO main2 in the code? And How do I integratethe code that you provided for me on the push button? I did some research on the variable on theBit 0 to 1. Is this used for storing push button condition?
    Thanks again for your help.
    Train going towards block signal.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DO
    Main2:
    IF (IN14 = 1) THEN
    Main1:
    HIGH 2 'Green = GREEN
    GOSUB WAIT 5000
    LOW 2 'Green = RED
    HIGH 0 'Red
    GOSUB WAIT 5000
    LOW 0 'Red = YELLOW
    HIGH 1 'Yellow
    GOSUB WAIT 5000
    LOW 1 'Yellow
    IF (IN15 = 1) THEN GOTO Main2:
    GOTO Main1:
    ENDIF
    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-01 15:23
    You need to put that whole subroutine (wait5: through the return at the end) at the end of your program, otherwise the Stamp Editor doesn't know what you're talking about when you put GOSUB wait5 in your program. Note that it's not GOSUB WAIT 5000, it's GOSUB wait5 so it all looks like
    HIGH 2
    GOSUB wait5
    LOW 2
    IF quit = 1 THEN GOTO main2
    
    Remember that at the beginning of your program (after the directives and before the DO) you'll need
    quit var bit
    time var byte
    
    Also, please don't quote your program or previous messages when you reply. It just makes the thread longer and longer and you'll get less help because people get turned off scrolling through all that stuff.
  • railroad signalrailroad signal Posts: 5
    edited 2012-11-05 13:43
    Mike Green wrote: »
    You need to put that whole subroutine (wait5: through the return at the end) at the end of your program, otherwise the Stamp Editor doesn't know what you're talking about when you put GOSUB wait5 in your program. Note that it's not GOSUB WAIT 5000, it's GOSUB wait5 so it all looks like
    HIGH 2
    GOSUB wait5
    LOW 2
    IF quit = 1 THEN GOTO main2
    
    Remember that at the beginning of your program (after the directives and before the DO) you'll need
    quit var bit
    time var byte
    
    Also, please don't quote your program or previous messages when you reply. It just makes the thread longer and longer and you'll get less help because people get turned off scrolling through all that stuff.


    Hello Mike,
    Thanks again. I am trying to understand the GOSUB I research it in the stamp manual. It isdifficult to understand. I do have somequestions. (1.) I think what you are trying to say is that I am completelyremoving the PAUSE commands and replacing it with GOSUB wait5?(2.) Am I putting If quit = 1 thenGOTO main2 after every LOWcommand? Or is it one time at the end. (3) do I remove Main1? And is thecode starting to look the way it should.

    'Train going towards block signal.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    quit VAR Bit
    time VAR Byte
    DO
    Main2:
    IF (IN14 = 1) THEN
    Main1:
    HIGH 2 'Green = GREEN
    GOSUB wait5
    LOW 2 'Green
    IF (quit = 1) THEN GOTO main2
    HIGH 0 'Red
    GOSUB wait5
    LOW 0 'Red = YELLOW
    HIGH 1 'Yellow
    GOSUB wait5
    LOW 1 'Yellow

    RETURN
    IF (IN15 = 1) THEN quit = 1
    GOTO Main1:
    ENDIF
    NEXT

  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-06 06:22
    GOSUB and RETURN provide for subroutine calls and return ... a really fundamental concept in programming. The "What's a Microcontroller?" tutorial is the introductory text for learning programming concepts. You probably should go through it and try as many of the examples as you can.

    1) The subroutine "wait5" was written to provide a 5 second delay like the PAUSE 5000, but to check I/O pin 15 periodically (about every 1/10 second) to see if it's button is pushed. If it's pushed, the delay is cut short and the subroutine leaves an indicator ("quit") to show that the button push stopped the delay. So, yes, the GOSUB is supposed to take the place of the PAUSE.

    2) The reason for putting the IF after the LOW is the following:

    You want to quit the main loop early if the button was pushed during the 5 second pause. That's why the IF is there. You also want the LED to be turned off if the loop is terminated early ... you don't want the LED to be left on. To do this, you can wait until after the LOW to test for the early exit from the loop or you can put 3 LOWs in the wait5 routine to make sure all the LEDs are turned off if the button is pushed (where quit is set to 1). A third choice is to put 3 LOWs after the Main1 label to make sure the LEDs are all off before starting each loop. If you look at a lot of programs, you'll see that these are the 3 main ways that people use to initialize something in their programs: a) make sure a specific thing is set properly when a specific condition occurs; b) make sure a whole set of things are set properly when something unusual occurs (like an error exit); c) make sure a whole set of things are set properly at the beginning of a program or program loop.

    At the end of your program, you've got an IF and a GOTO before the ENDIF. They won't do what you want. This is why you have to learn some basic programming skills. You have to understand what you're doing rather than just copy something that sounds like it might do what you want. Working through "What's a Microcontroller?" will help.
  • railroad signalrailroad signal Posts: 5
    edited 2012-11-09 20:35
    Mike Green wrote: »
    GOSUB and RETURN provide for subroutine calls and return ... a really fundamental concept in programming. The "What's a Microcontroller?" tutorial is the introductory text for learning programming concepts. You probably should go through it and try as many of the examples as you can.

    1) The subroutine "wait5" was written to provide a 5 second delay like the PAUSE 5000, but to check I/O pin 15 periodically (about every 1/10 second) to see if it's button is pushed. If it's pushed, the delay is cut short and the subroutine leaves an indicator ("quit") to show that the button push stopped the delay. So, yes, the GOSUB is supposed to take the place of the PAUSE.

    2) The reason for putting the IF after the LOW is the following:

    You want to quit the main loop early if the button was pushed during the 5 second pause. That's why the IF is there. You also want the LED to be turned off if the loop is terminated early ... you don't want the LED to be left on. To do this, you can wait until after the LOW to test for the early exit from the loop or you can put 3 LOWs in the wait5 routine to make sure all the LEDs are turned off if the button is pushed (where quit is set to 1). A third choice is to put 3 LOWs after the Main1 label to make sure the LEDs are all off before starting each loop. If you look at a lot of programs, you'll see that these are the 3 main ways that people use to initialize something in their programs: a) make sure a specific thing is set properly when a specific condition occurs; b) make sure a whole set of things are set properly when something unusual occurs (like an error exit); c) make sure a whole set of things are set properly at the beginning of a program or program loop.

    At the end of your program, you've got an IF and a GOTO before the ENDIF. They won't do what you want. This is why you have to learn some basic programming skills. You have to understand what you're doing rather than just copy something that sounds like it might do what you want. Working through "What's a Microcontroller?" will help.


    Thank youMike for your help. Yes I just started with the basic stamp about four monthago. Yes and the stamp came with the book WHAT IS A MICROCONTROLLER Version 3.0. I got up to chapter four and then I got involved with this block signal project. Not understandingthe basic codes and what they mean. I am going to research WHAT IS A MICROCONTROLLER Version 3.0. I amalso going to research the Stamp Manual along with it. Could you recommend anyother places where I could do some research that would help me? And if you donot mind I might have some questions about some of the codes at a later time.
    Thanks
  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-10 07:55
    What's a Microcontroller? and the Stamp Manual are the basic texts for learning how to use the Stamps. The link I posted (in #6) is to the whole webpage of tutorials. These are great once you get through the basics. Also, the Nuts and Volts Columns have lots of good examples, some simple and some advanced. Feel free to ask other questions as you go along, but I think many of them may be answered in one or another of these tutorials and columns.
Sign In or Register to comment.