PDA

View Full Version : Calculating time difference



Gadd3rs
24-01-2006, 09:46
Hi there,

I have two times: a take off and a landing time recorded to an EEPROM.I can recall both times individually onto an LCD screen, but i have no idea how to subtract the two values (landing - take off time) to find the flight duration.
Here's the program (which no doubt has plenty of mistakes in), would appreciate any help :)

symbol lcd = 7 'pin for the lcd firmware
symbol line = 128 '128 for top line or 192 for bottom line
symbol clock = %11010000
symbol top = 128
serout 7,T2400,(254,1)

main:
if pin1 = 1 then hello
if pin1 = 0 then timer
if pin0 = 1 then hello2
if pin0 = 0 then timer
if pin7 = 1 then hello3
if pin7 = 0 then timer
if pin6 = 1 then hello4
if pin6 = 0 then timer

timer:
if pin1 = 1 then hello
if pin0 = 1 then hello2
if pin7 = 1 then hello3
if pin6 = 1 then hello4
serout 7,T2400,(254,192,"Current time ")
pause 250
i2cslave clock, i2cslow, i2cbyte
readi2c 0, (b0, b1, b2, b3, b4, b5, b6, b7)
debug
pause 100
b8 = b0/16
b9 = b8 * 10
b10 = b0//16
b0 = b9 + b10
b8 = b1/16
b9 = b8 * 10
b10 = b1//16
b1 = b9 + b10
b8 = b2/16
b9 = b8 * 10
b10 = b2//16
b2 = b9 + b10
b8 = b3/16
b9 = b8 * 10
b10 = b3//16
b3 = b9 + b10
b8 = b4/16
b9 = b8 * 10
b10 = b4//16
b4 = b9 + b10
b8 = b5/16
b9 = b8 * 10
b10 = b5//16
b5 = b9 + b10
b8 = b6/16
b9 = b8 * 10
b10 = b6//16
b6 = b9 + b10
if b0 < 10 then ooi
if b1 < 10 then oio
if b2 < 10 then ioo
serout lcd,T2400,(254,top,#b2,":",#b1,":",#b0," ")
goto main
ooi:
if b1 < 10 then oii
if b2 < 10 then ioi
serout lcd,T2400,(254,top,#b2,":",#b1,":0",#b0," ")
goto main
oio:
if b2 < 10 then iio
serout lcd,T2400,(254,top,#b2,":0",#b1,":",#b0," ")
goto main
ioo:
serout lcd,T2400,(254,top,"0",#b2,":",#b1,":",#b0," ")
goto main
oii:
if b2 < 10 then iii
serout lcd,T2400,(254,top,#b2,":0",#b1,":0",#b0," ")
goto main
ioi:
serout lcd,T2400,(254,top,"0",#b2,":",#b1,":0",#b0," ")
goto main
iio:
serout lcd,T2400,(254,top,"0",#b2,":0",#b1,":",#b0," ")
goto main
iii:
serout lcd,T2400,(254,top,"0",#b2,":0",#b1,":0",#b0," ")
serout 7,T2400,(254,192,"Current time")
if pin1 = 1 then hello
if pin0 = 1 then hello2
if pin7 = 1 then hello3
if pin6 = 1 then hello4
goto main

original:
i2cslave %10100000, i2cfast, i2cbyte
writei2c 0, ("glider1")
pause 10
readi2c 0, (b0,b1,b2,b3,b4,b5,b6)
pause 10
serout 7,T2400, (254,1)
pause 1000
serout 7,T2400, (254,128,b0,b1,b2,b3,b4,b5,b6)
serout 7,T2400, (254,192,"JFM Take Off ")
pause 6000
serout 7,T2400, (254,1)
goto main

hello:
i2cslave %10100000, i2cfast, i2cbyte
readi2c 0, (b0,b1,b2,b3,b4,b5,b6,b7)
pause 10
serout 7,T2400, (254,1)
pause 1000
serout 7,T2400, (254,128,#b2,":",#b1,":",#b0)
serout 7,T2400, (254,192,"GEG landing")
pause 6000
serout 7,T2400, (254,1)

hello2:
i2cslave clock, i2cslow, i2cbyte
readi2c 0, (b0, b1, b2, b3, b4, b5, b6, b7)
pause 10
b8 = b0/16
b9 = b8 * 10
b10 = b0//16
b0 = b9 + b10
b8 = b1/16
b9 = b8 * 10
b10 = b1//16
b1 = b9 + b10
b8 = b2/16
b9 = b8 * 10
b10 = b2//16
b2 = b9 + b10
b8 = b3/16
b9 = b8 * 10
b10 = b3//16
b3 = b9 + b10
b8 = b4/16
b9 = b8 * 10
b10 = b4//16
b4 = b9 + b10
b8 = b5/16
b9 = b8 * 10
b10 = b5//16
b5 = b9 + b10
b8 = b6/16
b9 = b8 * 10
b10 = b6//16
b6 = b9 + b10
i2cslave %10100000, i2cfast, i2cbyte
writei2c 0, (b0, b1, b2, b3, b4, b5, b6, b7)
pause 10
readi2c 0, (b0,b1,b2,b3,b4,b5,b6,b7)
pause 10
serout 7,T2400, (254,1)
pause 1000
serout 7,T2400, (254,128,#b2,":",#b1,":",#b0)
serout 7,T2400, (254,192,"GEG landing")
pause 6000
serout 7,T2400, (254,1)
goto main

hello3:
i2cslave %10100010, i2cfast, i2cbyte
readi2c 0, (b0,b1,b2,b3,b4,b5,b6,b7)
pause 10
serout 7,T2400, (254,1)
pause 1000
serout 7,T2400, (254,128,#b2,":",#b1,":",#b0)
serout 7,T2400, (254,192,"GEG take off")
pause 6000
serout 7,T2400, (254,1)

goto main

hello4:
i2cslave clock, i2cslow, i2cbyte
readi2c 0, (b0, b1, b2, b3, b4, b5, b6, b7)
pause 10
b8 = b0/16
b9 = b8 * 10
b10 = b0//16
b0 = b9 + b10
b8 = b1/16
b9 = b8 * 10
b10 = b1//16
b1 = b9 + b10
b8 = b2/16
b9 = b8 * 10
b10 = b2//16
b2 = b9 + b10
b8 = b3/16
b9 = b8 * 10
b10 = b3//16
b3 = b9 + b10
b8 = b4/16
b9 = b8 * 10
b10 = b4//16
b4 = b9 + b10
b8 = b5/16
b9 = b8 * 10
b10 = b5//16
b5 = b9 + b10
b8 = b6/16
b9 = b8 * 10
b10 = b6//16
b6 = b9 + b10
i2cslave %10100010, i2cfast, i2cbyte
writei2c 0, (b0, b1, b2, b3, b4, b5, b6, b7)
pause 10
readi2c 0, (b0,b1,b2,b3,b4,b5,b6,b7)
pause 10
serout 7,T2400, (254,1)
pause 1000
serout 7,T2400, (254,128,#b2,":",#b1,":",#b0)
serout 7,T2400, (254,192,"GEG take off")
pause 6000
serout 7,T2400, (254,1)
goto main






Edited by - Gadd3rs on 1/24/2006 8:47:30 AM

Jeremy Leach
24-01-2006, 11:24
Hi, I haven't had the time to review your code, but generally if I need to compare times I first convert them to an integer number representing seconds. Then you can subtract the values to get the difference.

Unfortunately though, 24*60*60 = 86400 which is higher than a Word variable can hold, so either you limit the times to minutes (which is probably ok in your case) or you could use 12-hour clock with a separate variable holding am or pm.

You might also need a conversion routine to convert a seconds value to hours, minutes and seconds.

Edited by - Jeremy Leach on 1/24/2006 11:15:37 AM

hippy
24-01-2006, 12:21
This may give you a stating point ...

SYMBOL startHour = b0
SYMBOL startMins = b1
SYMBOL startSecs = b2

SYMBOL endHour = b3
SYMBOL endMins = b4
SYMBOL endSecs = b5

SYMBOL periodHour = b6
SYMBOL periodMins = b7
SYMBOL periodSecs = b8

startHour = 22 ' 22:59:13
startMins = 59
startSecs = 13

endHour = 02 ' 02:45:03
endMins = 45
endSecs = 03

CalculatePeriod:
periodHour = endHour - startHour
periodMins = endMins - startMins
periodSecs = endSecs - startSecs

CheckSecs:
IF periodSecs < $80 THEN CheckMins
periodSecs = periodSecs+60
periodMins = periodMins-1

CheckMins:
IF periodMins < $80 THEN CheckHour
periodMins = periodMins+60
periodHour = periodHour-1

CheckHour:
IF periodHour < $80 THEN CalculationDone
periodHour = periodHour+24

CalculationDone:

PAUSE 10000
SEROUT 0,N2400,("Flight Time = ",#periodHour,":",#periodMins,":",#periodSecs,CR,L F)

Fowkc
24-01-2006, 23:26
What's the project for, if you don't mind me asking? I'm interested as I'm a (glider) pilot. I was a powered pilot till I ran out of money!

Gadd3rs
26-01-2006, 14:54
Ahh a glider pilot :D
The project is actually for an electronic log to replace pen and paper for GCSE. Im currently @ Newark and Notts Gliding Club desperately trying to get solo :(

Ive tried out some ideas, and this one seems to work quite nicely. Like sum1 said it converts the overall times to seconds before breaking them back down.

Here's the snippet of the program:
...
w5 = b2 * 3600
w4 = b1 * 60
w5 = w5 + w4
w5 = w5 + b0

i2cslave %10100000, i2cfast, i2cbyte
readi2c 0, (b0,b1,b2,b3,b4,b5,b6,b7)
pause 10
serout 7,T2400, (254,192,#b2,":",#b1,":",#b0)
pause 6000
serout 7,T2400, (254,1)

w6 = b2 * 3600
w4 = b1 * 60
w6 = w6 + w4
w6 = w6 + b0

w4 = w6 - w5

b10 = w4 / 3600
b11 = w4 // 3600
b12 = b11 // 60
b11 = b11 / 60
...

Fowkc
26-01-2006, 17:39
Wow! I'm a member of the Nottingham University Gliding Club! I'm making a PICAXE-based G-meter. What are the odds that two glider pilots in (fairly) close geographic proximity are both working on PICAXE projects?

You guys fly at Winthorpe, right? We're at Cranwell. I flew solo back in April (or June). It's amazing!

I know you're still doing GCSE, but check out www.nugc.net and consider coming to Nottingham (if you want to go to uni). We're the best (largest, cheapest) university club in the country!

Sorry to detract from the thread, but this was quite a coincidence!

Edited by - Fowkc on 1/26/2006 4:40:24 PM

hippy
26-01-2006, 18:09
<i>Ive tried out some ideas, and this one seems to work quite nicely. Like sum1 said it converts the overall times to seconds before breaking them back down. </i>

The problem there - as Jeremy pointed out earlier - is that a PICAXE word variable can only hold a maximum value of 65535 and the value overflows, and the result will be wrong, if take-off or landing is after 18:12:15 local time.

It would be possible to adjust both starting and landing times down so maths remains in range.

Flights of over 18.2 hours, or which straddle midnight, will create problems, but I imagine they aren't likely occurances.

I also think there's a probable issue with your code ...

- b10 = w4 / 3600
- b11 = w4 // 3600 &lt;--- HERE
- b12 = b11 // 60
- b11 = b11 / 60

I haven't tested it but it looks like that b11 ought to be a word variable.

You can also optimise some of your code to save space ...

- w6 = b2 * 3600
- w4 = b1 * 60
- w6 = w6 + w4
- w6 = w6 + b0

Can be ...

- w6 = b2 * 60 + b1 * 60 + b0


Edited by - hippy on 1/26/2006 5:22:57 PM

Fowkc
26-01-2006, 20:54
I would forget about converting to seconds for reasons of code efficiency and the 18:12:16 problem, but mostly because you don't need to store values to the second, so why bother?

Are you planning to get the data back to a PC after a days flying has finished?

Gadd3rs
27-01-2006, 22:13
Hehe, thats awesome Fowkc :D Yeah, we do fly at Winthorpe, but unfortunately the Newark Showground is kicking us off later this year so we're frantically trying to relocate :( I don't actually live in Newark i live in Bramcote, Nottingham, so i guess thats even closer :P

Regarding the program:
I (with the help of another guy) ironed out a few issues - i think the seconds and minutes were screwed here and there.

I didnt have a clue about the 18:12:15 thing - ill have to see what i can do about that, but i doubt any1 will be getting over 18.2 hours in a K13 :P (or staddling midnight).

I'll deffo consolidate that code up - im beginning to run out of space on the 18X.

Cheers :)