# Time Off Timer

Viewing 12 posts - 1 through 12 (of 12 total)
• Author
Posts
• #8182
Rich Ex
Participant

Hello friends ,
I am trying to add a delay within a formula and things are not working right .

I have a graphic in Webstation that is linked to a channel and the graphic changes when the channel changes from 0 to 1 . So far no problem .
The thing is the real pulse on the channel is short , a few mS , so I want the graphic to stay ‘on’ for 10 seconds .
In my code I have tried using Task.Delay , this did not work , I then tried using the datetime.now as a timer , this worked but stopped the whole Webstation from updating .
Heres my code

double FindStatus(int boardcnl,int transittimecnl) // get ip cnl & delay cnl
{
double boardbit = Val(boardcnl);// setup variables
double boardcnlstat = Stat(boardcnl);// setup variables
double transittimeSecs = Val(transittimecnl);// setup variables
DateTime tnow = DateTime.Now; // get current time for timer
DateTime target = DateTime.Now.AddSeconds(transittimeSecs); // timer target
int aa = 0; // setup variables

if (boardcnlstat == 1)
if (boardbit == 0 )
{aa=1;
return aa;
}
if (boardbit == 1 )
{aa=2;
Val(aa); // update the cnl
while (tnow < target) // DELAY target reached ?
{ tnow = DateTime.Now; }
return aa;
}
else
{
aa=0;
return aa;
}
}

Any help please would be much appreciated .
Thank
Rich

#8187
Mikhail
Moderator

Hello Rich,

I think, you need an extra channel of the calculated discrete type. A formula should check when the source input channel was changed from 0 to 1, then set the 2nd channel to 1 and remember a timestamp. After that compare current time with the timestamp to determine when to change the 2nd channel back to 0.

Formulas must be calculated as fast as possible. Avoid delays, tasks and thread in formulas.

#8188
manjey73
Participant

Will the TP timer be enough for you, similar to how it is implemented in a PLC?

Ticks

Tp

Calling the formula Tp (Val(414), 10000) number of the monitored channel 414, time in milliseconds.
When 1 appears in channel 414, the timer output will turn on, after 10 seconds it will turn off for this example. The timer starts working on the signal edge, no need to hold 1 in the controlled channel.

The Ticks formula is used in all the timers I’ve done. The formulas must be added to the Formula Reference.

• This reply was modified 1 year, 10 months ago by manjey73.
#8190
manjey73
Participant

But this does not solve the problem of saving the pulse to the channel database when writing once per minute.

#8192
Rich Ex
Participant

Thank you Mikhail , thank you Manjey .

Yes I see that I should not add the delay in the formula . I will try what you said about making another channel and using a timestamp for comparison .

As always thank you.

Rich

#8636
Participant

Dear Mr.Mikhail,
I want to use AutoControl Module on Raspberry Pi4. So Pls help me to get the Raspberry Pi4 Key for purchasing order.
Thanks and best regards
P/S: On this Raspberry Pi4 was installed the fee Extra Commponent Module. It is working perfectly.
From Hoang Han

#8649
Mikhail
Moderator

Hello,

This topic is named “Time Off Timer”. Please create a new topic with an appropriate name for discussing Auto Control Module.

#11284
gabeirinhas
Participant

Hello,

Sorry for my ignorance,

I am trying to implement the formula Tp, in the formula table,

When copying the content that it indicates in the same one,

int[] TpNum = new int[1];
long[] TpST = new long[1];
bool[] TpFlag = new bool[1];
public double Tp(double TpIn, long TpPT)
{
long ET = 0L;
bool q = Val(CnlNum) > 0;
bool tp_in = TpIn > 0;

int res = Array.IndexOf(TpNum, CnlNum);
if (res == -1)
{
res = TpNum.Length;
Array.Resize(ref TpNum, res+1);
Array.Resize(ref TpST, res+1);
Array.Resize(ref TpFlag, res+1);
TpNum[res] = CnlNum;
}

if (!TpFlag[res])
{
if (tp_in)
{
TpFlag[res] = true;
TpST[res] = Ticks();
if (ET < TpPT) q = true;
}
}
else
{
if (q)
{
ET = Ticks() – TpST[res];
if (ET >= TpPT) q = false;
}
else
{
if(!tp_in)
{
TpFlag[res] = false;
ET = 0L;
}
}
}
return Convert.ToDouble(q);
}

The server stops working correctly,

Could you tell me how I could implement such a formula correctly,

I attach images of the process I perform

Thank you, best regards

• This reply was modified 1 month, 3 weeks ago by gabeirinhas.
#11286
manjey73
Participant

There should also be a Tiсks formula in the reference book, which all Timers refer to.

Also, the engine of this forum may spoil some characters when copying. See in the Server log which line in the EngineCS file the error refers to.

• This reply was modified 1 month, 3 weeks ago by manjey73.
#11289
gabeirinhas
Participant

I have added the ticks formula to the formula table and it works correctly,

I get the following error,

I hope you can help me, thank you

• This reply was modified 1 month, 3 weeks ago by gabeirinhas.
#11291
manjey73
Participant

`ET = Ticks() - TpST[res];` probably the forum engine replaced the minus with a dash

Check this line and the position in the file is referenced by an error

#11292
gabeirinhas
Participant

Hello,

You were right,

It is working correctly,

Thank you very much

Viewing 12 posts - 1 through 12 (of 12 total)
• You must be logged in to reply to this topic.