Is there a way to stop a timer in Netlogo? - timer

If you make a timer in Netlogo, is there a way to stop it?

The built-in timer cannot be paused.
I think you will need to call reset-timer, but before calling it, add the current value of timer to a running total that you keep in a global variable.

You can reset the timer but I'm not sure if you can stop it.
More info on resetting it here:
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#reset-timer
I think the timer will end with your end statement and there would be no way to stop in while its running where your creating it.
I dunno if that helps im kinda new to Netlogo....

For example, you could specify the number of ticks you want to stop in a variable that you set (i.e. ticks-start ticks-end), and then use the stop and ticks commands to control it using code like the following:
globals [ ticks-start ticks-end ] ;Otherwise, setting slider/box in the interface instead
if (ticks > ticks-start)
if (ticks >= ticks-end) [ stop ]

Related

Delete timers alarm before it fire callback nodemcu

I have for example this code on my function on my nodemcu:
mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function() digitalWrite(16, HIGH) end)
But, before 5 seconds, I want delete this timer with another function, because if I want (before 5 seconds) launch a function like:
digitalWrite(16, LOW)
If I don't delete timers, it toggle state of led.
So, how can I delete certain timer?
You could either call mytimer:stop() or mytimer:unregister() depending on whether you want to restart it later on or not.
UPDATE: Just showing this picture to proof the working example:
Hint: make sure the mytimer variable is accessible from within the other function.
Please also have a look at the timer documentation before posting a question.

how to change number of #Core.periodic(30)

I make a new agent, and I will call every 30 seconds through Core.periodic(30).
And I write such as #Core.periodic(self.heart_beat), but this is not operate.
(heart_beat is config file's variable.)
I don't know how to solve it.
enter image description here
#Core.periodic is a decorator (you can tell by the # at the start of the line). It is supposed to be used like this:
#Core.periodic(30)
def heart_beat(self):
#Do heartbeat stuff here
If you need to do something dynamic you can hook up the callback during run time with self.core.periodic. This allows you to change the setting dynamically and stop the periodic function later if needed.
self.heart_beat_greenlet = self.core.periodic(30, self.heart_beat)
Later you can call self.heart_beat_greenlet.kill() to stop the periodic function.

Timer to represent AI reaction times

I'm creating a card game in pygame for my college project, and a large aspect of the game is how the game's AI reacts to the current situation. I have a function to randomly generate a number within 2 parameters, and this is how long I want the program to wait.
All of the code on my ai is contained within an if statement, and once called I want the program to wait generated amount of time, and then make it's decision on what to do.
Originally I had:
pygame.time.delay(calcAISpeed(AIspeed))
This would work well, if it didn't pause the rest of the program whilst the AI is waiting, stopping the user from interacting with the program. This means I cannot use while loops to create my timer either.
What is the best way to work around this without going into multi-threading or other complex solutions? My project is due in soon and I don't want to make massive changes. I've tried using pygame.time.Clock functions to compare the current time to the generated one, but resetting the clock once the operation has been performed has proved troublesome.
Thanks for the help and I look forward to your input.
The easiest way around this would be to have a variable within your AI called something like "wait" and set it to a random number (of course it will have to be tweaked to your program speed... I'll explain in the code below.). Then in your update function have a conditional that waits to see if that wait number is zero or below, and if not subtract a certain amount of time from it. Below is a basic set of code to explain this...
class AI(object):
def __init__(self):
#put the stuff you want in your ai in here
self.currentwait = 100
#^^^ All you need is this variable defined somewhere
#If you want a static number as your wait time add this variable
self.wait = 100 #Your number here
def updateAI(self):
#If the wait number is less than zero then do stuff
if self.currentwait <= 0:
#Do your AI stuff here
else:
#Based on your game's tick speed and how long you want
#your AI to wait you can change the amount removed from
#your "current wait" variable
self.currentwait -= 100 #Your number here
To give you an idea of what is going on above, you have a variable called currentwait. This variable describes the time left the program has to wait. If this number is greater than 0, there is still time to wait, so nothing will get executed. However, time will be subtracted from this variable so every tick there is less time to wait. You can control this rate by using the clock tick rate. For example, if you clock rate is set to 60, then you can make the program wait 1 second by setting currentwait to 60 and taking 1 off every tick until the number reaches zero.
Like I said this is very basic so you will probably have to change it to fit your program slightly, but it should do the trick. Hope this helps you and good luck with your project :)
The other option is to create a timer event on the event queue and listen for it in the event loop: How can I detect if the user has double-clicked in pygame?

Tiny OS timer not resetting

I'am currently working on tinyos and I Am trying to reset timer
lets say to 2 seconds when it is running at 45 seconds
but it is not working, i can't figure out why,
can someone help me figure it out
here is the code:
printf("timer before resetting it %ld",call Timer1.getNow());
offset = ((TimeMote_t*) payload)->tdata;
call Timer1.startPeriodic(offset);
printf("timer after resetting it %ld",call Timer1.getNow());
now actually it should have reset the timer to offset but it's not resetting it.
both printf statements are giving the same time.
No, it shouldn't. Timer.getNow() returns absolute time, which can't be changed or reset. Timer interface can be used to schedule events at a particular moment in the future. Timer.startPeriodic(offset) starts the timer, meaning that the event Timer.fired() will be signaled in the future. In this particular example, the event will be signaled offset units from the call to Timer.startPeriodic and then repeated every offset units infinitely or until call to Timer.stop(). Return value of Timer.getNow() doesn't change and increases monotonically regardless of whether the timer is started or not.
See: Interface: tos.lib.timer.Timer

VisualBasic6 - read continuously a variable

I need read in an infinite loop some variables and in the case it changes the boolean status it must do something.
I tried to use a Do...Loop but the application crashes.
Is there a way in visual basic 6 to use an infinite loop without stunk?
My code:
Do
asd1 = readValue1
asd2 = readValue2
If asd1 <> asd1ex Then
Text1.Text = "yes"
End If
If asd2 <> asd2ex Then
Text1.Text = "no"
End If
Loop While True
Make a timer and on that timer check the status, instead of the loop.
Solved after comment that explained where the data was coming from (async COM component's property):
working with vb6 IDE on a realtime client-server project. I have to read some variables
and when one of these changes status it sends a socket message to
server. With the sleep it stuck equally
What did not help:
DoEvents and sleep
DoEvents
Sleep 100
might help, will need to refer to the windows function sleep. But VB6 is single thread (well one for UI and one for logic) so you should have a way to get out of the loop. What are you really trying to do? Can you describe at a top level?
Are you working on the VB6 IDE or in VBA code in Office?
For sleep to work declare:-
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
See this too https://stackoverflow.com/a/4540553/1643558
If your showing a form to the user to read value 1 and 2 then you can use a modal form and have a button to click when they are done, and hide the form only when you like the values. No need to have a loop then. Can show an error MsgBox on a modal form too.
See http://www.tek-tips.com/viewthread.cfm?qid=1117372
Maybe remove the sleep and only keep the DoEvents.
You could also make a timer and on that timer check the status, instead of the loop
It looks like you're trying to set up a sort of event handler. In effect, your loop is "listening" for a change to the variable. You don't explain how the variables get changed, and this is important . If whatever is changing the variables can also raise an event, then you're home free--you can get rid of your loop and use the event handler to send the socket message. (This is probably why Deanna asked how the variables change.) This is the preferred way to do what you want, so you should find ways to raise an event if the variables change.

Resources