Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:40 07 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Set TIME$ from GPS

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 09:22pm 28 Jun 2016
Copy link to clipboard 
Print this post

Yes I did read the manual - several times and I've spent 3 days trying to sort this
TIME$ = "HH:MM:SS" Sets the time of the internal clock is what it says in the manual

What I want to do is simple (in theory)

1) Get time from GPS ***DONE***
2) Decode time into hours, mins, secs, year, month, day ***DONE***
3) Set TIME$ from data hours:mins:secs ****** CANNOT GET THIS DONE****

I cannot figure out how to set the TIME$ from the time received from the GPS

The reason I want to do this is the GPS will only be powered on once a day to save battery power and I want it to update the TIME$ once a day


sounds simple but I can't figure out how to do it, and I can't find any threads with a similar scenario
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10215
Posted: 09:32pm 28 Jun 2016
Copy link to clipboard 
Print this post

time$=str$(hours,2,0,"0")+":"+str$(mins,2,0,"0")+":"+str$(secs,2,0,"0")

UNTESTED
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:37pm 28 Jun 2016
Copy link to clipboard 
Print this post

Thanks Peter, it didn't work but it gave me enough clues as to how to fix it
I had to do time$ = Line2$
**Line2$ is the current time decoded from the GPS

Now another puzzle

I want to do this

Pin(26) = 1

IF TIME$ is between 02:05:00 and 02:05:30 THEN
PIN(26) = 1
NEXT IF

I can't figure out how to get the "between the 2 times to work"

OR if I was to simply put IF TIME$ = 02:05:10
would the Micromite check the time every second to see if that was the time? or am i best doing what I originally wanted - the time between 2 secs of seconds?


EDIT I did test it with the following code but it came up with an error

[261] If Time$ = 10:50:05 Then
Error: IF without THEN


[code]IF time$ = 10:50:05 THEN
PIN(26) = 1 'GPS Power turned ON
secs = 0
end if[/code] Edited by lew247 2016-06-30
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1573
Posted: 12:33am 29 Jun 2016
Copy link to clipboard 
Print this post

  lew247 said   [code]IF time$ = 10:50:05 THEN[/code]


Just try
IF time$ = "10:50:05" THEN



causality ≠ correlation ≠ coincidence
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1114
Posted: 12:52am 29 Jun 2016
Copy link to clipboard 
Print this post

alternatively, convert times to seconds then just do standard compare

eg. hour, minute and second strings to numbers then seconds plus minutes x 60 plus hours x 60 x 60
... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 12:56am 29 Jun 2016
Copy link to clipboard 
Print this post

  lew247 said  I want to do this

Pin(26) = 1

IF TIME$ is between 02:05:00 and 02:05:30 THEN
PIN(26) = 1
NEXT IF

I can't figure out how to get the "between the 2 times to work"
[/quote]

you could do:-
[Code]
IF LEFT$(TIME$,5)="02:05" THEN
PIN(26)=1
ELSE
PIN(26)=0
END IF
[/code]
That would give you a 60 second period for the to be GPS on.
You'll need the ELSE otherwise once it's turned on it will stay on.


[Quote]
EDIT I did test it with the following code but it came up with an error

[261] If Time$ = 10:50:05 Then
Error: IF without THEN


[code]IF time$ = 10:50:05 THEN
PIN(26) = 1 'GPS Power turned ON
secs = 0
end if[/code]


It needs to be:-
[Code]IF time$ = "10:50:05" THEN[/code]

Or you could do use the TimeSecs Function I mentioned in another thread.

Cheers.


Edited by Phil23 2016-06-30
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1573
Posted: 01:02am 29 Jun 2016
Copy link to clipboard 
Print this post

Another way is to use the SETTICK

from the MM manual p.27:
RTC GETTIME ' set the time at startup
SETTICK 12 * 3600000, SetTime, 4 ' interrupt every 12 hours
< normal program >
SUB SetTime ' interrupt called every 12 hours
RTC GETTIME ' reset the time
END SUB


Please replace the "RTC GETTIME" with your GPS routine.
causality ≠ correlation ≠ coincidence
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 01:03am 29 Jun 2016
Copy link to clipboard 
Print this post

  panky said   alternatively, convert times to seconds then just do standard compare

eg. hour, minute and second strings to numbers then seconds plus minutes x 60 plus hours x 60 x 60


Beat me to it "By That Much"....


[Code]
Function TimeSecs(TimeStr as String) As integer

TimeSecs=Val(left$(TimeStr,2))*60*60+Val(Mid$(TimeStr,4,2))*60+Val(Right$(TimeStr,2))

End function
[/code]

Cheers.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10215
Posted: 01:04am 29 Jun 2016
Copy link to clipboard 
Print this post

  Quote  Thanks Peter, it didn't work but it gave me enough clues as to how to fix it


  Quote  If Time$ = 10:50:05 Then


you are getting confused between numbers and strings. Strings are sequences of ascii characters that may, of course, include the ascii representation of a number.

Variables with a $ are strings. To assign a literal to them you include the ascii sequence in quotes

e.g. time$="11:11:11"

A number can be converted to a string using the STR$ function. It is also converted automatically using a PRINT statement.

My code:

time$=str$(hours,2,0,"0")+":"+str$(mins,2,0,"0")+":"+str$(secs,2,0,"0")

works perfectly if hours, mins and secs are numbers but not if they are strings.

It is really worth trying to fully understand the three datatypes that MMBasic on the Micromite supports:

strings
integer numbers
floating point numbers

If you put the following at the top of your program:

OPTION EXPLICIT
OPTION DEFAULT NONE

It will probably cause multiple errors as it forces you to declare each and every variable and define its type. However, it is IMHO an unconditional truth that doing this is best programming practice.

If you do this you will fully understand what each and every variable is and which functions apply to it.

So VAL(string$) converts a string to a number (integer or float)
str$(number) converts a number to a string

etc.

 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:19am 29 Jun 2016
Copy link to clipboard 
Print this post

Thanks everyone
[code]
IF time$ = "12:15:00" THEN
secs = 0
end if[/code]

did exactly what I wanted

Peter your right I do get confused, it's because of the brain injury/surgery I have problems remembering things, and quite often I have to "learn" certain things such as the difference between numbers and strings when I haven't used them for a while (or didn't fully understand it anyway

There are days when something so simple as saying PRINT TIME looks like
magazeti wakati$ in Swahili

but I will get there - learning slowly, sorry for so many questions and always asking why something wont work!Edited by lew247 2016-06-30
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 04:26am 02 Jul 2016
Copy link to clipboard 
Print this post

This is really stupid

It's been working perfectly with the above code
IF time$ = "12:15:00" THEN
secs = 0
end if

However today I changed the time to [code]IF time$ = 02:20:00 THEN
secs = 0
end if[/code]

02:20:00 and now it will not work

it comes up with the error
[code]
[261] If Time$ = 02:20:00 Then
Error: IF without THEN
>
[/code]

Anyone know Why this is happening?
Nothing else has changed in the code other than me changing the time

and it's been working perfectly for the past 3 days with no problems
Edited by lew247 2016-07-03
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 04:30am 02 Jul 2016
Copy link to clipboard 
Print this post

Don't forget the qoutes as it is a string!

[code]
IF time$ = "02:20:00" THEN
[/code]

Microblocks. Build with logic.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 04:50am 02 Jul 2016
Copy link to clipboard 
Print this post

Thanks Jean
That was the problem - working fine now
arghh will I ever get the difference with string$ and numbers!
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 12:13pm 02 Jul 2016
Copy link to clipboard 
Print this post

  lew247 said   Thanks Jean
That was the problem - working fine now
arghh will I ever get the difference with string$ and numbers!


Just think of it in there terms:-

ABC or "ABC" is a string; it's letters.

123 is a number.
123x2=246, another number.

"123" is a string, just like "ABC123".

Just like you can't multiply "ABC" by 2, you cant multiply "123" by 2 if it's a string; it's designated as a "Letters & Numbers" value.

If you wanted to multiply a string of "123" by 2, you'd need to convert it to a number first.

[Code]
> Print "ABC"*2
Error: Incompatible types in expression
> Print "123"*2
Error: Incompatible types in expression.
[/code]

If a string can be converted to a number, VAL() will return it.

[Code]
> print Val("123")
123
> print Val("123")*2
246
> print Val("ABC")
0
>
[/code]

Phil
 
plover

Guru

Joined: 18/04/2013
Location: Australia
Posts: 306
Posted: 04:06am 03 Jul 2016
Copy link to clipboard 
Print this post

lew247
Here is a little trick I use when staring myself blind on a simple statement that "should" or "ought" work. I use notepad and carefully line the staements in question up under each other.

This way I try to avoid to have 'remember' it more becomes a 'comparison' this has saved me a few times, the example earlier:
[code]
IF time$ = "12:15:00" THEN this one works
IF time$ = 02:20:00 THEN now this one does not work
[/code]

 
Dylan
Regular Member

Joined: 17/06/2013
Location: Netherlands
Posts: 81
Posted: 11:22am 03 Jul 2016
Copy link to clipboard 
Print this post

Although comparing for equality (to the second) won't hurt much(worst case scenario the clock won't update for three days running), consider doing it properly.

1) Power GPS module on when time is greater than whatever time you have picked.
2) Set power pin to 1
3) Loop until you either have the new time from GPS, or 5 minutes have passed on the current clock (again using greater than)
4) Set power pin back to 0

Log the number of:
a) successful updates
b) failed updates
c) total seconds gained
d) total seconds lost
Send these to COM port every minute or hour, or whatever works for you.

Anything more is gold plating (not necessary). But turning off the GPS once you have your fix makes sense.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025