Sinclair Basic count seconds...

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Sinclair Basic count seconds...

Post by firelord »

Is there a way to count seconds in Sinckair Basic;
I want to do some stuff at various intervals and di some waiting.

So far I was using eg PAUSE 100 but when I oress a key it just passes that delay comnand.

Thanks in advance
catmeows
Manic Miner
Posts: 718
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: Sinclair Basic count seconds...

Post by catmeows »

There is a system variable called FRAMES, it Is increased every 20 ms
Proud owner of Didaktik M
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

Thanks for the reply.
We can access system variables from normal basic?
catmeows
Manic Miner
Posts: 718
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: Sinclair Basic count seconds...

Post by catmeows »

You can use PEEK command to read byte memory.
FRAMES Is three byte value so you read it with code

Code: Select all

10 LET frames=....
20 LET time=PEEK frames+256*PEEK (frames+1)+65536*PEEK(frames+2)
30 PRINT AT 0,0;time,:GOTO 20
Thera two traps:
1) BEEP and tape commands stops time during its execution.
2) since computer changes FRAMES when it wants, on rare occassions you can get wrong time because computer changed FRAMES exactly between evaluating PEEKs. But it can be handled with little extra logic.

I don't remember FRAMES address, please refer Spectrum manual.
Proud owner of Didaktik M
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

ok. Thanks
User avatar
p13z
Manic Miner
Posts: 611
Joined: Sun Feb 17, 2019 10:41 pm
Location: UK
Contact:

Re: Sinclair Basic count seconds...

Post by p13z »

If you're simply trying to replicate the PAUSE command, but without keypresses cutting it short:
Easiest thing is just to add:

Code: Select all

FOR T=0 TO {arbitrary} : NEXT T
And calibrate the {arbitrary} number to your needs.
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

p13z wrote: Wed May 11, 2022 4:59 pm If you're simply trying to replicate the PAUSE command, but without keypresses cutting it short:
Easiest thing is just to add:

Code: Select all

FOR T=0 TO {arbitrary} : NEXT T
And calibrate the {arbitrary} number to your needs.
I know that but it seconds counter it s much more secure...
User avatar
XTM
Manic Miner
Posts: 789
Joined: Mon Jun 22, 2020 12:09 am
Location: Cologne, Germany
Contact:

Re: Sinclair Basic count seconds...

Post by XTM »

If you add PAUSE 1 to that FOR/NEXT loop, it should take exactly 1 frame (unless BASIC is too slow?) for each cycle. So 50 for 1 second, 500 for 10 seconds etc.
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

XTM wrote: Wed May 11, 2022 6:26 pm If you add PAUSE 1 to that FOR/NEXT loop, it should take exactly 1 frame (unless BASIC is too slow?) for each cycle. So 50 for 1 second, 500 for 10 seconds etc.
If I press a key won't override the PAUSE command ?
User avatar
XTM
Manic Miner
Posts: 789
Joined: Mon Jun 22, 2020 12:09 am
Location: Cologne, Germany
Contact:

Re: Sinclair Basic count seconds...

Post by XTM »

I'm not sure. Guess you could just test it with a loop of 250 which should take 5 seconds. Try running it and stop the time, then run it again but keep a key pressed to see if it makes any difference.

I used to use "PAUSE 1: PAUSE 0" in some of my programs, but I don't remember what the exact reason for it was. I think this can cancel out a key being held before a PAUSE 0?
User avatar
+3code
Manic Miner
Posts: 433
Joined: Sat Mar 19, 2022 7:40 am

Re: Sinclair Basic count seconds...

Post by +3code »

I've noticed the +3 BASIC interpreter some times ignores PAUSE statements when they are near LOAD/SAVE "m:" commands, but not always, it's a bit buggy.
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

XTM wrote: Wed May 11, 2022 7:40 pm
I used to use "PAUSE 1: PAUSE 0" in some of my programs, but I don't remember what the exact reason for it was. I think this can cancel out a key being held before a PAUSE 0?
I did something like that . I think if you kept a key pressed only the first PAUSE was affected.
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

I tried this:

Code: Select all

10 FOR T=0 TO 1000 :PAUSE 1: NEXT T
It stops for 20seconds in QAOP (even if I keep a key pressed)

This is interesting to know but my main question is counting seconds so I can :
eg Do A after 10 seconds and do B after 5 more seconds etc...
If I put "IF" inside the FOR loop seconds counting will not work.
User avatar
madaxe
Drutt
Posts: 35
Joined: Sun Dec 02, 2018 5:40 pm
Location: Portugal

Re: Sinclair Basic count seconds...

Post by madaxe »

Hi there;

Try this:

Code: Select all

  10 POKE 23672,0
  20 LET n=PEEK (23672)
  30 IF n<50 THEN GOTO 20
In this case 50 means 1 second. Hope you find this usefull :)

Regards.
User avatar
tkfan
Drutt
Posts: 28
Joined: Wed Jan 05, 2022 4:35 am
Location: Curitiba - Brazil
Contact:

Re: Sinclair Basic count seconds...

Post by tkfan »

firelord wrote: Wed May 11, 2022 7:14 pm
XTM wrote: Wed May 11, 2022 6:26 pm If you add PAUSE 1 to that FOR/NEXT loop, it should take exactly 1 frame (unless BASIC is too slow?) for each cycle. So 50 for 1 second, 500 for 10 seconds etc.
If I press a key won't override the PAUSE command ?
The PAUSE statement do:
  1. initialize a counter with value of PAUSE operand;
  2. wait for a maskable interrupt;
  3. if counter is not 0, decrement it;
  4. read keyboard;
  5. repeat steps 2-5 until counter is 1 or a valid key has been pressed.
(this is not exactly BASIC ROM PAUSE routine, of course, but the above algorithm has the same behaviour)

When step 2 is executed by the first time, it is unpredictable when a maskable interrupt will be activated, i. e., the delay will take 0-20 ms (milliseconds). Further maskable interrupts will be activated regularly in 20 ms period. Thus, unless FOR/NEXT loop takes more than 20 ms to be executed, PAUSE operand is equivalent to:

Code: Select all

FOR n=1 TO operand: PAUSE 1: NEXT n
but it is not affected by any key press.
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

Code: Select all

10 FOR T=0 TO 1000 :PAUSE 1: NEXT T
This is identical to my code. But the to 1000 is the difference.in mine the 1000 resulted to about 20seconds.

What I wanted was something like that :
Starttime=(get somehow start time)

Code: Select all

If currenttime-starttime=20 sevonds THEN function1:counter1=counter1+1
If currenttime-starttime=30 sevonds THEN function2
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

madaxe wrote: Fri May 13, 2022 9:29 am Hi there;

Try this:

Code: Select all

  10 POKE 23672,0
  20 LET n=PEEK (23672)
  30 IF n<50 THEN GOTO 20
In this case 50 means 1 second. Hope you find this usefull :)

Regards.
(I am a bit slow :) )
In combination with another thread (viewtopic.php?f=6&t=8404 ) I managed to use a -kind of- reliable counter...

Code: Select all

  5 LET COUNTER=0
  10 POKE 23672,0
  20 LET n=PEEK (23672)
  30 IF n<50 THEN GOTO 20
  35 LET counter=counter+1
  40 PRINT AT 5,0;"Counter:";counter
  150 GOTO 10
I bet that I f I start doing stuff between the counter I will lost the accuracy :)
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: Sinclair Basic count seconds...

Post by Bedazzle »

firelord wrote: Sun Nov 13, 2022 7:15 pm I bet that I f I start doing stuff between the counter I will lost the accuracy :)
Sure. But you can rewrite code a little, and check if value is less that 200 (4 seconds), and by value/50 you can get elapsed seconds.
Why 200? Because after 255 it goes back to zero. :)
firelord
Manic Miner
Posts: 552
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Sinclair Basic count seconds...

Post by firelord »

I'm just posting a related post that seems very interesting

viewtopic.php?t=8429
Post Reply