Sinclair Basic count seconds...

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
+3code
Manic Miner
Posts: 427
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: 545
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: 545
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: 545
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: 545
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: 545
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