Sinclair Basic count seconds...
Sinclair Basic count seconds...
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
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
Re: Sinclair Basic count seconds...
There is a system variable called FRAMES, it Is increased every 20 ms
Proud owner of Didaktik M
Re: Sinclair Basic count seconds...
Thanks for the reply.
We can access system variables from normal basic?
We can access system variables from normal basic?
Re: Sinclair Basic count seconds...
You can use PEEK command to read byte memory.
FRAMES Is three byte value so you read it with code
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.
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
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
Re: Sinclair Basic count seconds...
ok. Thanks
Re: Sinclair Basic count seconds...
If you're simply trying to replicate the PAUSE command, but without keypresses cutting it short:
Easiest thing is just to add:
And calibrate the {arbitrary} number to your needs.
Easiest thing is just to add:
Code: Select all
FOR T=0 TO {arbitrary} : NEXT T
Re: Sinclair Basic count seconds...
I know that but it seconds counter it s much more secure...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:And calibrate the {arbitrary} number to your needs.Code: Select all
FOR T=0 TO {arbitrary} : NEXT T
Re: Sinclair Basic count seconds...
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.
Re: Sinclair Basic count seconds...
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?
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?
Re: Sinclair Basic count seconds...
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.
Re: Sinclair Basic count seconds...
I did something like that . I think if you kept a key pressed only the first PAUSE was affected.
Re: Sinclair Basic count seconds...
I tried this:
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.
Code: Select all
10 FOR T=0 TO 1000 :PAUSE 1: NEXT T
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.
Re: Sinclair Basic count seconds...
Hi there;
Try this:
In this case 50 means 1 second. Hope you find this usefull
Regards.
Try this:
Code: Select all
10 POKE 23672,0
20 LET n=PEEK (23672)
30 IF n<50 THEN GOTO 20
Regards.
Re: Sinclair Basic count seconds...
The PAUSE statement do:
- initialize a counter with value of PAUSE operand;
- wait for a maskable interrupt;
- if counter is not 0, decrement it;
- read keyboard;
- repeat steps 2-5 until counter is 1 or a valid key has been pressed.
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
Re: Sinclair Basic count seconds...
Code: Select all
10 FOR T=0 TO 1000 :PAUSE 1: NEXT T
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
Re: Sinclair Basic count seconds...
(I am a bit slow )madaxe wrote: ↑Fri May 13, 2022 9:29 am Hi there;
Try this:
In this case 50 means 1 second. Hope you find this usefullCode: Select all
10 POKE 23672,0 20 LET n=PEEK (23672) 30 IF n<50 THEN GOTO 20
Regards.
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
Re: Sinclair Basic count seconds...
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.