Discuss Scratch

jchunter
New Scratcher
17 posts

Stopping Timer

How does one stop the Timer (Sensing) without resetting it? Stop-All doesn't do it.
Paddle2See
Scratch Team
1000+ posts

Stopping Timer

There isn't any way to stop it that I know of. A workaround is to make your own timer out of a variable and update it periodically in a loop. Then, when the loop stops, the variable will stop changing.
The_Grits
Scratcher
1000+ posts

Stopping Timer

Paddle2See wrote:

There isn't any way to stop it that I know of. A workaround is to make your own timer out of a variable and update it periodically in a loop. Then, when the loop stops, the variable will stop changing.
Sorta like this:
when gf clicked
forever
if <(timer_on) = [on]>
set (mytimer) to (timer)
end
end

Last edited by The_Grits (Nov. 5, 2013 02:28:27)

DotDash
Scratcher
1000+ posts

Stopping Timer

The_Grits wrote:

Paddle2See wrote:

There isn't any way to stop it that I know of. A workaround is to make your own timer out of a variable and update it periodically in a loop. Then, when the loop stops, the variable will stop changing.
Sorta like this:
when gf clicked
forever
if <(timer_on) = [on]>
reset timer
set (mytimer) to (timer)
end
end
Fixed

Last edited by DotDash (Nov. 5, 2013 02:46:11)

sonicfan12p
Scratcher
1000+ posts

Stopping Timer

Here's the script Paddle was referring to.
when gf clicked
set [timer v] to [0]
forever
if <(timer?) = [on]>
wait [0.01] secs
change [timer v] by [0.01]
The other scripts provided would only set the variable timer to the timer, not really a pause function, though they could work as a stop. This script allows the flexibility of pausing and then continuing repeatedly, accurate to a hundredth of a second. It can also be used as a dead stop.

Last edited by sonicfan12p (Nov. 5, 2013 02:52:15)

scubajerry
Scratcher
1000+ posts

Stopping Timer

sonicfan12p wrote:

Here's the script Paddle was referring to.
when gf clicked
set [timer v] to [0]
forever
if <(timer?) = [on]>
wait [0.01] secs
change [timer v] by [0.01]
The other scripts provided would only set the variable timer to the timer, not really a pause function, though they could work as a stop. This script allows the flexibility of pausing and then continuing repeatedly, accurate to a hundredth of a second. It can also be used as a dead stop.
I do not think this works very well. If the computer is busy doing other things, or even the program is busy running a RUN WITHOUT SCREEN REFRESH, for more than .01 seconds, the timer will miss increments.

Last edited by scubajerry (Nov. 5, 2013 03:29:41)

scubajerry
Scratcher
1000+ posts

Stopping Timer

DotDash wrote:

The_Grits wrote:

Paddle2See wrote:

There isn't any way to stop it that I know of. A workaround is to make your own timer out of a variable and update it periodically in a loop. Then, when the loop stops, the variable will stop changing.
Sorta like this:
when gf clicked
forever
if <(timer_on) = [on]>
reset timer
set (mytimer) to (timer)
end
end
Fixed
Actually it is broken - this will always set the timer to 0
JAVAProgramming
Scratcher
100+ posts

Stopping Timer

I would use:
when GF clicked
repeat until <(timeOn?) = (0)>
Change (Timer) by [1] // not the timer.. But a different variable
wait (0.99) secs // A fail safe so (hopefully) the timer will be as accurate as possible.

Last edited by JAVAProgramming (Nov. 5, 2013 03:51:52)

sonicfan12p
Scratcher
1000+ posts

Stopping Timer

scubajerry wrote:

sonicfan12p wrote:

Here's the script Paddle was referring to.
when gf clicked
set [timer v] to [0]
forever
if <(timer?) = [on]>
wait [0.01] secs
change [timer v] by [0.01]
The other scripts provided would only set the variable timer to the timer, not really a pause function, though they could work as a stop. This script allows the flexibility of pausing and then continuing repeatedly, accurate to a hundredth of a second. It can also be used as a dead stop.
I do not think this works very well. If the computer is busy doing other things, or even the program is busy running a RUN WITHOUT SCREEN REFRESH, for more than .01 seconds, the timer will miss increments.
That's why the increments are so small, so missing it won't mess you up too bad. However, it will work about as well as anything else, and I'd like to see someone come up with a more accurately scripted timer with pause function. Another solution is changing it to thousandth, or even ten thousandths of a second.
drmcw
Scratcher
1000+ posts

Stopping Timer

sonicfan12p wrote:

scubajerry wrote:

sonicfan12p wrote:

Here's the script Paddle was referring to.
when gf clicked
set [timer v] to [0]
forever
if <(timer?) = [on]>
wait [0.01] secs
change [timer v] by [0.01]
The other scripts provided would only set the variable timer to the timer, not really a pause function, though they could work as a stop. This script allows the flexibility of pausing and then continuing repeatedly, accurate to a hundredth of a second. It can also be used as a dead stop.
I do not think this works very well. If the computer is busy doing other things, or even the program is busy running a RUN WITHOUT SCREEN REFRESH, for more than .01 seconds, the timer will miss increments.
That's why the increments are so small, so missing it won't mess you up too bad. However, it will work about as well as anything else, and I'd like to see someone come up with a more accurately scripted timer with pause function. Another solution is changing it to thousandth, or even ten thousandths of a second.
I'd guess that smaller increments would be more inaccurate. I wouldn't rely on waits less than 1/10 certainly less than 1/100 of a second as being accurate at all. Rather they would be dependent on cpu speed and what other code is running. The most accurate use of the timer is to store its value at the start of an operation then subtract that from the timer value when it has finished, or reset at start and read at the end. So for your method I would rather use the biggest wait I could to make it more accurate.
sonicfan12p
Scratcher
1000+ posts

Stopping Timer

drmcw wrote:

I'd guess that smaller increments would be more inaccurate. I wouldn't rely on waits less than 1/10 certainly less than 1/100 of a second as being accurate at all. Rather they would be dependent on cpu speed and what other code is running. The most accurate use of the timer is to store its value at the start of an operation then subtract that from the timer value when it has finished, or reset at start and read at the end. So for your method I would rather use the biggest wait I could to make it more accurate.
(facepalm) I can't believe I forgot about that method. Well played sir.
when gf clicked
set [time difference v] to [0]
reset timer
forever
if <(timer on?) = [yes]>
set [my timer v] to ((timer) - (time difference))
else
set [saved timer v] to (timer)
wait until <(timer on?) = [yes]>
set [time difference v] to ((timer) - (my timer))
end
Good job defeating my challenge, but you still didn't post the script, so ha! I still get to be helpful!

Last edited by sonicfan12p (Nov. 6, 2013 01:38:31)

WarriorNinja983
Scratcher
100+ posts

Stopping Timer

Do this. Instead of using the time block in the sensing, use a variable. Like this:
when green flag clicked
forever
set [time v] to (timer)

now when you want to show the timer block, show the varible instead.
drmcw
Scratcher
1000+ posts

Stopping Timer

WarriorNinja983 wrote:

Do this. Instead of using the time block in the sensing, use a variable. Like this:
when green flag clicked
forever
set [time v] to (timer)

now when you want to show the timer block, show the varible instead.
What is that supposed to do? It's just adding work to duplicate the timer but without being as accurate.
sonicfan12p
Scratcher
1000+ posts

Stopping Timer

Yeah, that doesn't really help anything…he wants to be able to stop and/or pause the timer, which we've already accomplished. All that does is let you show and hide the timer at will, which could already be done with any of our other scripts.

Last edited by sonicfan12p (Nov. 7, 2013 15:10:58)

JsnPrkr
Scratcher
100+ posts

Stopping Timer

sonicfan12p wrote:

when gf clicked
set [time difference v] to [0]
reset timer
forever
if <(timer on?) = [yes]>
set [my timer v] to ((timer) - (time difference))
else
set [saved timer v] to (timer)
wait until <(timer on?) = [yes]>
set [time difference v] to ((timer) - (my timer))
end

SonicFan, it looks like you can delete the SavedTimer line in the Wait loop.

Drmcw, thanks for the bright ideas. I think you'll find it best to only reset the system timer when starting the program. Letting the system timer run allows you to create as many user timers in your program as you like that use the system timer as their basic engine (otherwise resetting the system timer would goof up any timers that were enabled and running during the reset.)

Saving the system time is very clever. The numbers can grow very large, though, and lead to memory problems (I don't know what “too large” would be in Scratch, though.) Here's a way to only accumulate time and keep the system timer's accuracy: http://scratch.mit.edu/projects/16163344/#editor

The top version has a couple of extra lines to make it easier to follow the logic. The bottom version would be the one to use in a project.

Here's another approach: In order to save processor overhead, this project only updates the timer in whole second steps: http://scratch.mit.edu/projects/16160520/#editor

-Jason
deadjmau5
Scratcher
7 posts

Stopping Timer

Yea I tried that when its finished it broadcasts but that didnt really work XD
UniMog
Scratcher
58 posts

Stopping Timer

Hi jchunter

Take a look at this project.

One Cat has a continuous timer, the other cat has a start-stop timer - use the space bar to control the timer.
http://scratch.mit.edu/projects/18276983/
JsnPrkr
Scratcher
100+ posts

Stopping Timer

deadjmau5 wrote:

Yea I tried that when its finished it broadcasts but that didnt really work XD

Tried what? Sounds like some debugging is needed in the code you tried.
Par0dy
Scratcher
100+ posts

Stopping Timer

This is how. First of all don't use timer just create a variable called timer. Whenever you want your timer to start make a broadcast called start timer. Then do this
when I receive [Start Timer v]
set [Timer v] to [Whatever you want]
repeat until <(Timer(the variable you just created)) = [0]>
wait (1) secs

change [Timer v] by (-1)
end

and voila you're done. Note that timer should be orange and it should be a variable, the only reason it's red is coz of the forum code. Also don't have the variable you just created as part of the name of your variable.
Hope this helped.
minekraftkid
Scratcher
100+ posts

Stopping Timer

when green flag clicked
forever

if <(time) < [ 0.01]> then
change [time v] by (0.01)
wait (0.01) secs

end
else
set [time v] to (timer)
wait until <not <(timer) = (time)>>

end

Powered by DjangoBB