Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.

Log in or register to post/reply in the forum.

Continuous for time period command

elisseo Aug 16, 2021 04:07 AM

Hey all

I am currently working with the CR1000X logger. I am looking to implement the functionality to only set a variable true if it has been continuously true for 8 minutes. I have used a scan interval of every minute and increment a counter every time the condition is true or reset if not. then when the counter is greater than 8 set the condition true. however this is very long and i have to implement it so many times so it is making the code very long (thousands of lines), i was wondering whether there was a simpler way to do this, such as a specific command or function? see below the current way I have done it/

If (Strain(1) < StressL3_LB OR Strain(1) >= StressL3_HB) Then
Count1(2) = Count1(2) + 1
Count1(2) = 0
If Count1(2) >= 8 Then 'For 8 minutes
Gauge_AlarmValue(1) = -4 * (Gauge1_Switch >= 0 AND Gauge1_Switch <= 3) 'Set alarm
Count1(2) = 0 'Reset counter for alarm trigger

Any help would be appreciated, 


Nico Aug 24, 2021 08:42 AM

You could prob use an internal DataTable() to add up the boolean value for each gauge by putting the determination about it hitting the rails into the DisableVar of a Totalize() command. Would need a bit of thinking and playing to get it right and probably needs an expression for the SourceVar to get the value counting down again.. but yeah.

One question though.. you're only measuring that gauge every minute, how do you catch when the condition isn't there when you try to measure it? Is the change that slow?
The alarm should sound 8 min past the moment it got last triggered as well?

Sam Aug 29, 2021 04:23 AM

You noted that the code is very long and implemented many times. In those situations, packaging up the code into a Subroutine or Function helps improve code management, simplify reuse, and shorten the overall program.

A counter is not a bad approach, but requires that you are always aware of the relationship between the scan rate and the counter value.

The following has not been tested, but conceptually shows how you can use variables to hold state that is passed in and out of a subroutine that could be used over and over again throughout the program.


Const StressL3_LB = 10.1
Const StressL3_HB = 100.2
Const AlarmPeriod = 8*60 'seconds

Public Strain(3)
Public TimeStart(3)
Public TimeElapse(3)
Public Alarm(3)

Sub ContinuousForTimePeriod(TimeStart As Long,TimeElapse As Long,Period As Long, Alarm As Boolean,ResetTimeStart As Boolean)
  If TimeStart <= 0 Then
    TimeStart = Public.Timestamp(1,1)

  TimeElapse = Public.Timestamp(1,1) - TimeStart

  If TimeElapse >= Period Then
    Alarm = TRUE
    TimeStart = Public.TimeStamp(1,1)

  If ResetTimeStart Then
    TimeStart = Public.Timestamp(1,1)

  TimeElapse = Public.Timestamp(1,1) - TimeStart

  Scan (1,Min,0,0)

    Call ContinuousForTimePeriod(TimeStart(1),TimeElapse(1),AlarmPeriod,Alarm(1),((Strain(1)<stressl3_lb) or="" (strain(1)="">=StressL3_HB)))
    Call ContinuousForTimePeriod(TimeStart(2),TimeElapse(2),AlarmPeriod,Alarm(2),((Strain(2)<stressl3_lb) or="" (strain(2)="">=StressL3_HB)))
    Call ContinuousForTimePeriod(TimeStart(3),TimeElapse(3),AlarmPeriod,Alarm(3),((Strain(3)<stressl3_lb) or="" (strain(3)="">=StressL3_HB)))



elisseo Aug 30, 2021 11:02 PM

This post is under review.

pokeeffe Aug 31, 2021 02:37 AM

8min running average of a psuedo-boolean (0/1)? So 1 = continuous and <1 is not?

Log in or register to post/reply in the forum.