Example 5.

Time-Based Tasks

 

Example 4 introduced the use of the controller clock and the associated system variables.  We now build upon these instruments to implement a more realistic application.  The example below is a little longer but each segment is build from the simpler statements discussed in the previous examples.

 

First consider the following program.

 

 

title     "example 5a. simple irrigation"

output    oPump;

parameter pHourStart;

parameter pHourEnd;

parameter pMinsOn;

parameter pMinsOff;

var       vMinsElapsed;

var       vMinsCycle;

 

if( (_hour<pHourStart) || (_hour>pHourEnd) )

 {

  oPump=0;

  }

 else

  {

   vElapsedMins=( 60*(_hour-pHourStart) ) + _minute;

   vCycleMins=vElapsedMins % (pOnMins+pOffMins);

   oPump=(vCycleMins<pOnMins);

   }

 

 

You may notice that this program uses two variables, namely “vMinsElapsed” and “vMinsCycle.”  Recall that variables are simply memory locations that hold intermediate values.  The program declares the output “oPump.”  Although, there is no explicit input, the program makes use of the clock.  Four parameters (pHourStart, pHourEnd, pMinsOn, and pMinsOff) determine the behavior of the program. 

 

The program implements a simple time-based irrigation scheme.  Starting at “pHourStart” hour (a value between 0 and 23), the output pump (oPump) is turned on for pMinsOn minutes.  Then it is switched off for pMinsOff minutes.  When the hour reaches pHourEnd, the pump is turned off.  For instance, if you want the irrigation pump to be on from 4PM to 6PM for 5 minutes and then off for 10 minutes, you would set the parameters as below:

 

pHourStart 16

pHourEnd   18

pMinsOn     5

pMinsOff   10

 

Let us inspect the program in a little more detail.  The program is build around a single condition.  If the current hour is outside the range pHourStart and pHourStop, the pump is turned off.  Otherwise, we first compute the intermediate value, vMinsElapsed.  This variable is the number of minutes since the irrigation started at hour pHourStart.  Note how it is computed.  The value (_hour-pHourStart) is the number of hours since the irrigation scheme started.  Multiplying this by 60 gives the number of minutes in this many hours.  To this, we add the current minutes.

 

The next variable, vMinsCycle, is the number of minutes since the end of the last on/off cycle.  An on/off cycle takes (pMinsOn+pMinsOff) number of minutes.  If we divide the total minutes elapsed since the beginning of the irrigation cycle by this value, we would find the number of cycles executed so far.  However, we are interested not in the number of cycles, but the remainder of this division.  The remainder operator (‘%’) is just like the division operator (‘/’).  The remainder is the number of minutes in to the current on/off cycle.  Once this value is computed, it is a simple manner to decide wheterh the pump should be on or off.  If the minutes in the current cycle are less than pMinsOn, then the last conditional statement turns the pump on.

This may be a good place to say a few things about style.  In this example, you may have noticed that we start the output with a lower case ‘o’, variables with a lower case ‘v’, and parameters with a lower case ‘p’.  This improves the readability of the program, since while looking at any portion of the code, you immediately know if the symbol represents an input, output, variable, or parameter.

 

Since we implemented the start and stop times, and the on and off times as parameters, you may change these from the iNetGrow program, or a web page without having to change the code and download it to the controller.

 

Also see : Programming, Program Examples, Script Syntax

 

 


© Rigel Corporation iNetGrow 2003-2006.  All rights reserved.