How to Create a Custom Totalizer

Hi Ben,

I’m not sure where you’re getting that 0.5 second error assumption. But then, I wouldn’t use a timer for this either. Here’s how I would do it (just one option, hoping others will share their ideas):

(OptoScript):

while (1 == 1) // loop forever

  // Get Day & Seconds
  nSecondsSinceMidnight = GetSecondsSinceMidnight();
  nDay = GetDay();

  // See if the day changed since last we looked
  if (nDay <> nLastDay) then

    // Clear total
    fTotalMeasuredSoFarToday = 0;

  endif

  // See if the seconds changed since we last looked
  if (nSecondsSinceMidnight <> nLastSeconds) then 

    // Update total
    fTotalMeasuredSoFarToday = fTotalMeasuredSoFarToday + (fCurrentReading / 60.0);

  endif

  // Update last day, seconds
  nLastSeconds = nSecondsSinceMidnight;
  nLastDay = nDay;

  // Give other task a chance to run
  DelayMsec(1);
wend

If you’re not sure why I have that delay of 1 millisecond in there, check out form 1776 (essentially you’re making sure this chart/task doesn’t hog the CPU).

Here’s how it would look in non-OptoScript blocks:

One side note since I’m showing OptoScript vs. Action/Condition blocks here: Notice in the “Update total” Action block, it takes and extra step (and extra variable: fCurrentReadingDividedBy60):

vs. the OptoScript equivalent:

fTotalMeasuredSoFarToday = fTotalMeasuredSoFarToday + (fCurrentReading / 60.0);

In spite of my OptoScript bias (as a recovering programmer), I must admit the nice thing about Action/Condition blocks as a flowchart: it’s easy to see at a glance what the logic/flow does. (Maybe even without glasses.)

Here’s this chart in case you’d like to import it in to your PAC Control 9.3 basic or better strategy:
TotalizerChart9.3Basic.zip (1.92 KB)

Hope that helps!
-OptoMary