Momentary button script on Perspective

Hi,
a few days ago I posted a question about how to handle a bool type tag that needs to go back to 0 after few seconds after it goes to 1.

In Vision I just used a Momentary Button but in Perspective such a component doesn't exist.
Searching through the forum I found out about a possible solution that uses the Timer function (I believe from Java) used in a tag script to comands the value back to 0 after a few seconds

The code that @aspatchek2013 is using doens't seem the "standard Ignition Script language" (Jython?), will the function Timer work in a "standard script"? Should I import any library?
I'm not familiar with Java so any help would be great, thanks.

Hi @daniele1,

maybe using the time library may help you.

I usually use this script to keep a tag to 1 for few seconds:

import time

system.tag.write("[default]Test/Tag", 1)
time.sleep(1) 
system.tag.write("[default]Test/Tag", 0)

the parameter of time.sleep is the amount of seconds the script will wait before continuing the script.
So if you want for example 100ms, you can write:

time.sleep(0.1)

Hope it helps :slight_smile:

2 Likes

Just to add to this, sleep is not recommended for some applications as it locks the thread. There are other methods for adding delay.

@Federick732000’s answer is perfectly acceptable as long as you are aware of the caveats/risks associated with it :slight_smile:

1 Like

Ok, thanks.
I just saw this thread How to implement "Momentary Button" function in Perspective View that mentions that function and I was wondering what was the difference between the two solutions.
When writing/reading tags I always use writeBlocking and readBlocking (mostly because I didin’t understood how the Async method works :sweat_smile: ), so using time.sleep() shouldn’t make a difference I believe.

Using sleep in Perspective isn’t much of an issue since the gateway runs all scripts and it has no effect on the gui like it does in Vision. A Vision client would be locked up for the time that it takes to run the script. That being said, I wouldn’t make a habit of using sleep as there are better solutions most of the time. Also, if you have lots of scripts with sleeps, then the gateway threads might start to build up which could cause stability issues

The threading.Timer library is a standard Python library. It’s technically fine to use in any python script.
The difference between using time.sleep and threading.Timer is that time.sleep will delay the progression of your current script, whereas threading.Timer will spawn another thread and execute the given function after a delay, leaving your calling script free to continue on executing the rest of the code. For Perspective, time.sleep is simpler if you don’t need the function to execute asynchronously to your script

Also, tag.write and tag.read functions are from Ignition versions below v8.0 and shouldn’t be used in v8+ projects. You’re correct in using read/writeBlocking/Async etc.

2 Likes

That's not entirely true - while scripting in Perspective isn't directly tried to an 'event dispatch thread' as it is in Vision, it's still bad practice to "hold" a Perspective worker thread for longer than strictly necessary to perform your script. Using asynchronous threads or workers is definitely still best practice.

4 Likes

@daniele1

I tend to use the threading library whenever this situation arises. The timer.sleep function may have applications where it works, but it will always put more limitations on system functionality than the threading option.

On top of that, if I need to run another function during the ‘waiting’ portion of either of these options, the threading option will be only solution. The timer.sleep function will not run the rest of the script until after the sleep is complete whereas the threading timer works well for this.

Due to the differences and my desire to standardize whenever possible, I stick with the threading library.

2 Likes