Timing

js28

New Member
Hi,

I was just wondering if anyone knows of a timing operation that can be used in a PIC. I have looked through the manuals and the only two operations that looked like they would help me were the wait and pause operations. However these wouldn't be perfect and I was wondering if there was simply a timing operation that could be started and stopped when certain criteria is filled.

Thanks
 

BeanieBots

Moderator
Depends which PICAXE you using.
Have a look at settimer X1/X2 only.

You could use an external 'clock' to trigger an interrupt which in turn counts up.
Some variants allow access to internal timers via 'peek' but I'll leave it to others to offer up which locations they can be found at.
 

hippy

Ex-Staff (retired)
Welcome to the PICAXE forum.

A lot depends on what you want to measure, how long or short the periods are, whether separate start/stop signals or a single signal on/off, and to what accuracy you want to measure.

Have you chosen a PICAXE or will you choose one to match what you want to do ? Are there any limitations on choice ?
 

js28

New Member
The picaxe im using is a 28X, there will have two be two seperate conditions, one to start the timing and one to stop the timing.
 

westaust55

Moderator
More information would be helpful.

What creates the two conditons. Input signals or other?

what are the required timing durations?

Try providing a more detailed explanation of the program process/flow
 

Andrew Cowan

Senior Member
Have you looked at the interrupt command? This may be your answer.

Alternatively, how long is the pause? You could use something like

Code:
main:
If trigger = 1 then starttimer
b3=0
goto main

starttimer:
for w1 = 0 to 65535
 inc b3
 if stopbutton = 1 then endtimer
 pause 1
next w1

etc
A
 

Dippy

Moderator
I agree with Westy, more information please.

Draw us a flow diagram.

How accurate does the timing have to be?

Is this a school project?
 

js28

New Member
Sorry for being so vague and thanks for the help.

Its for a degree course and my task is to design a speed detection device for trains. This would be done by the use of two electro magnetic sensors on the track placed at about 16 meters apart or further if needs be for timing purposes. One sensor will start the timing and the other will stop it.

For the assignment though it may not be nessasery to construct a model so simply two inputs to control the starting and stopping of the timer could be used.

As I said in the origional post i've looked around in the help manuals and on this forum to try and find a operation that would be sutible but was unable to find one.

Thanks again.
 

hippy

Ex-Staff (retired)
Should be as simple as ( in pseudo-code ) ...

periods = 0
Do : Loop Until First_Sensor_Triggered
Do
Pause time_period
periods = periods + 1
Loop Until Second_Sensor_Triggerd
time_between_sensors = periods * time_period
speed = distance_between_sensors / time_between_sensors

That's a good starting point. You haven't said what sort of accuracy you are wanting or what the ranges of speeds you need to measure are.
 

js28

New Member
In terms of accuracy I think mill seconds would be surficciant, however if that is to accurate a time for the picaxe then I could always move the two sensors further apart. The reason I choose 16 meters apart is because there are 1 609.344 meters in a mile so using 16 meters just requires me to multiply the time elapsed by 100 which would give me the mph of the train.

I know this would be an average mph over the 16 meter rather than its actual mph but I with my aim being to make sure the train isnt traveling at unsafe speeds I figure that any change in speed over those 16 meters would be negliagable at such unsafe speeds.
 

Dippy

Moderator
What maximum speed are you expecting?
i.e. Whats the shortest time between sensors?
This will give you the first estimate concerning accuracy and resolution.

I'm just thinking about required accuracy and if the train is doing 1mph then thats a lot of miiliseconds...
If its less time than average student takes to get to the Union bar then we'll need microseconds ;)

But, I'm sure you'll be able to get excellent results.
 

js28

New Member
The current fast speeds on the UK rail networks are 125 mph which would give a time of 800 milliseconds.
 

hippy

Ex-Staff (retired)
Even with a doubling of rail speeds the timing should not be too fast as to cause the PICAXE any problems so the next course of action would seem to be build a prototype and compare your results against expectations.
 

js28

New Member
Ive got so far as designing part of the program without finding the right timing command yet:

Code:
main:
Start:		if pin0=1 then StartTimer
		goto Start

StartTimer:	**Timer Start**
		if pin1=1 then StopTimer
		goto StartTimer

StopTimer:	**Timer Stop**
		**Time Outputted to b0**
		let b1= b0 * 100
		if b1 > 110 then Warning
		serout 7,N2400,(254,128)
		serout 7,N2400,(",#b1," mph")
		pause 100
		goto Start

Warning:	serout 7,N2400,(254,128)
		serout 7,N2400,(",#b1,"mph")
		pause 100
		serout 7,N2400,(254,192)
		serout 7,N2400,("Warning Speed to High")
		goto Start


As you may be able to tell I am relitivaly inexperienced in Picaxe programming.
 
Last edited:

lbenson

Senior Member
Something on the order of this. Initialize a counter to 0 (w6); when you have detected the signal on pin0, go to the StartTimer loop and within that loop, increment the counter and pause for 10 milliseconds. When you detect the signal on pin1, the counter will tell you the number of milliseconds divided by 10. Do the maths in StopTimer (I'm not sure I've gotten it right). Some accuracy will be lost to the other code in the StartTimer loop, but probably not much more than a millisecond per loop. You can test to calibrate the timing to see if you need to adjust for the amount of time it takes to execute the loop aside from the "pause 10". Note that at the end, "goto Start" is changed to "goto main".

Code:
main:
                         w6 = 0
Start:		if pin0=1 then StartTimer
		goto Start

StartTimer:	**Timer Start**
		if pin1=1 then StopTimer
                         w6 = w6 + 1
                         pause 10
		goto StartTimer

StopTimer:	**Timer Stop**
		**Time Outputted to b0**
		let b1= w6 * 10 / 16 * 100
		serout 0,N2400,(254,128)
		serout 0,n2400,(Section AA = ",#b1)
		pause 100
		serout 0,N2400,(254,129)
		serout 0,n2400,(Section AA = mph)
		pause 100
		goto main
 
Last edited:

js28

New Member
Thanks again for all the help. I now have the following program:

Code:
Main:		let w0 = 0

Start:	        if pin0=1 then StartTimer
		goto Start

StartTimer:     pause 100
		w0 = w0 + 1
		if pin1=1 then StopTimer
		goto StartTimer
		
StopTimer:	let w1= w0 / 36000
		let w2= 16 / w1
		let w3= w2 * 100
		if w3 > 110 then Warning
		serout 7,N2400,(254,1)
		serout 7,N2400,(254,128)
		serout 7,N2400,(#w3," mph ")
		pause 100
		goto Main

Warning:    	serout 7,N2400,(254,1)
		serout 7,N2400,(254,128)
		serout 7,N2400,(#w3,"mph Warning")
		pause 100
		serout 7,N2400,(254,192)
		serout 7,N2400,("Speed to High")
		goto Main
The pause is in 0.1 second increments and w0 is counting the number of 0.1 second increments until pin1 goes high.

The following maths is then done:

With there being 3,600 seconds in an hour I need to divide the w0 value by 36,000 with the value in w0 being 1 to every 0.1. Then with there being 1609.344 meters in a mile I will have the two sensors 16 meters apart (if I could figure out how to use decimal points then I would use 16.09344 meters) so I divide the value in w1 by 16. Then because I have scaled the mile down by 100 times I need to multiply the value in w2 by 100 to give the mph answer.

This obviously doesn't work in practice because of the w0,w1,w2 values not being able to display decimal values. My question being is there code which would allow me to use decimal points or will I simply have to scale up my calculations?
 
Last edited:

SilentScreamer

Senior Member
You will need to scale it up. Do't forget you can simply divide by 360 rather than dividing by 36000 then multiplying by 100. You could further simplify it so you divide by 5760 to achieve the same result.

Decimal places can be used depending on how you output the data, but the PICAXE itself only understands integers.
 

hippy

Ex-Staff (retired)
Looking at your maths, you seem to have ....

Code:
     (     16     )
     ( ---------- )
w3 = (            ) * 100
     (  (  w0  )  )
     (  ( ---- )  )
     (  ( 3600 )  )
That could be more simply put as "w3 = ( 3600 * 16 * 100 ) / w0" though you'll have to do some re-arranging to avoid 16-bit overflows.
 
Top