Including a delay into a program

koyli

Member
In an earlier thread I asked for help in getting a model railway signalling interlocking working.

Now with some brilliant help from this forum, the problem of extra inputs has been solved.

This is the next question.

The home signal for the terminal platforms has to have a two minute delay before it clears to make sure the train is under control.

I have done this at the other end of the station, but it stops the entire program from running. This does not matter at that end because only one train can move at any one time.

But at this end several moves can take place at once. So, can I incorporate a delay into the program without stopping everthing else.
 

tmfkam

Senior Member
If you are using an M2 processor, this is easy.

At the start of the 'delay required' section...
Code:
DisableTime
Let Time=0
EnableTime
later on, or periodically...

Code:
If Time > 120 Then
   'two minutes have elapsed
EndIf
As it seems as though you are using an X2 processor. You'll have to do something similar within your program loop. Something that sets a counter variable to zero initially, then increments it each time the loop is executed before checking to see if the counter has increased beyond the value (found by experimentation) that represents the approximate time it takes for the loop to be executed over two minutes.

Code:
Let MyCount=0

MainProgramLoop:
If ResetDelayNeeded Then
   Let MyCount=0
Else
   Inc MyCount
EndIf
If MyCount > 500 Then
   'More than two minutes elapsed at loop execution time of 240mS
EndIf
'Do other things in loop here...
Goto MainProgramLoop
 
Last edited:

koyli

Member
If you are using an M2 processor, this is easy.

At the start of the 'delay required' section...
Code:
DisableTime
Let Time=0
EnableTime
later on, or periodically...

Code:
If Time > 120 Then
   'two minutes have elapsed
EndIf
As it seems as though you are using an X2 processor. You'll have to do something similar within your program loop. Something that sets a counter variable to zero initially, then increments it each time the loop is executed before checking to see if the counter has increased beyond the value (found by experimentation) that represents the approximate time it takes for the loop to be executed over two minutes.

Sorry, I should have mentioned, I am using a 40x2. It does not have multitasking, that would have made the job easy.

Thanks for your reply
 

lbenson

Senior Member
Post 3 in this thread -- http://www.picaxeforum.co.uk/showthread.php?27625-Time-and-Time-again -- shows a way to have multiple events occur at various delay times (in seconds). The code is for M2 devices. If using X2, you need to initialize with "settimer tls_16" (with whatever speed you're running at instead of "16"). Then, instead of "if time ..." you would use "if timer ...".

Note that some commands throw off the timer count, if that is critical.
 

geoff07

Senior Member
For the kind of thing you are doing you might think about a state-machine approach. A state-machine (or finite-state-machine) is a system with known states, and known transitions from one state to another when a particular event occurs. The application is defined in the definitions of the states, events, and transitions. The program itself simply looks for events, decides if they are relevant to the current state, and if so, transitions the system from the current state to the next state. This sounds quite complex and abstract but in fact is quite simple in reality. It is suitable for things where states can be defined, like traffic lights, lift mechanisms, etc. In my house I have numerous picaxe-based state machines running to control simple things like the doorbell and the solar panel pump, to complex things like the garage door, the central heating, and led lighting. There is a skeleton FSM program on my blog on this forum.

To get to your question, in the FSM world a delay is simply the definition of a future event. So delays are non-blocking, as the program continues to look for events that occur before the timer expires, and can deal with them independently, addressing the 'delay' when the next event is the expiry of the timer.

In my program, the event_monitor subroutine, which runs most of the time looking for all possible events, would see that an event is called for in x seconds time, and will count down the time each second, and when it reaches zero will generate the event for action. The action is simply the calling of the transition subroutine for the event.

Using this approach, you can have very complex activities with many states and events, but the code remains simple and understandable and thus maintainable. You might like to think about drawing up the logic of your railway system in the form of a state-transition diagram. There might be one for each train, running in parallel. The states for a train might be something like this:
Code:
[FONT=courier new]state  (v next state)                      event            transition action
stopped at platform x, loading             departure time   shut doors
   v
stopped at platform x, waiting for green   green signal     start moving, then set signal to red
   v
moving to  signal a                        signal is green  proceed and set signal to red(next state would be 'move to signal b' or whatever)
                                           signal is red    stop (next state would be 'stopped at signal a, waiting for green')
   v
etc. and this has to be very thoroughly worked through, but this is true of any system to be programmed.[/FONT]
Apologies for the simplistic example, but the thing is, once you have the state/transition diagram debugged, programming is almost trivial.
 
Last edited:

Dartmoor

Member
@ Koyli - you said: "Sorry, I should have mentioned, I am using a 40x2. It does not have multitasking, that would have made the job easy."

I think the 40X2 does allow up to 4 program slots to run at the same time?
This was very useful on a level crossing to go through the light sequence and flashing for a train on line1, yet still allow instant response to track circuits etc for a train on line2.

Admittedly I used an 18M2 programmed with logicator, but I believe RUN 0, RUN 1, RUN 2, RUN 4 (in logicator START, START1, START2, START3) are also available on an X2?

Perhaps too abstract for this for forum but your 2 minute timer on the Home signal sounds excessive for a model railway?

Have fun!
 

edmunds

Senior Member
I think the 40X2 does allow up to 4 program slots to run at the same time?
@koyli,

Not correct. Parallel tasks only on M2.

However, the parallel tasks as I understand them, are only a way to make life easier for a programmer, since what it does, is that it executes a piece of code from each task sequentially anyway. There is no such thing as multi-core processor sitting in M2 :). You could write and many people do write the code in similar fashion for X2 devices. In model train environment, picaxe running even at 8MHz is fully capable of detecting everything and anything with careful coding. And you have 8 times that at your disposal.

I have, for an example, picaxe 40x2 sitting in the middle of four lane model car crossing, checking each lane for approaching cars and continuing to check the other three lanes while figuring out what to do with the one where a car was detected over a pretty heavy algorithm involving getting parameters out of I2C I/O expanders. All of that is fast enough even with 4 cars arriving at literally the same time and the chip running at 8MHz.

So it is not difficult, but it is also not a one evening project. It probably took me a year of full time learning and testing to put this together. Admittedly, my programming and electronics background was close to zero and micro controller programming background exactly zero.

What I'm saying is that what you are trying to do is well within reach of picaxe platform and should be fun project. However, Rome was not built in a day and from your other thread it sounds like you are underestimating your expense of time by a factor of 10 or even a 100.


Good luck,

Edmunds
 

koyli

Member
Perhaps too abstract for this for forum but your 2 minute timer on the Home signal sounds excessive for a model railway?
This was part of the signalling regulations here in the UK in the 1970`s, the period I am modelling.

A passenger train entering a terminal or partially occupied platform must be brought to a stand at the home signal for at least two minutes to ensure the driver has it under control.
 

Dartmoor

Member
Apologies for the mix up with parallel tasks, I obviously misread the manual.

Yes, the 2 minutes was used but my thoughts were that a model train would be brought to a stand much quicker than a full size one?
2 minutes is a long time with a model - I just thought that should be 'scaled'?
Speed & distance are scaled (assuming you do not run trains at a real 100mph, nor along miles of track?), so it makes sense to me that time should be scaled too?

It was just a thought . . .
 

koyli

Member
I have asked a signal engineer about this and I have got it slightly wrong. The two minutes delay starts when the train enters the berth track circuit.
If the train travels at the required speed, the signal will clear as the train approches it. But lf the train travels at a faster speed, then it will arrive at the signal too soon and will have to wait till the signal clears.

Yes I know it seems a long time but I am modelling the signal installation as accurately as I can and this is just part of it.
 
Top