Load accumulated duration

Discussion in 'Pascal Logic Code Examples' started by Darren, Sep 12, 2005.

  1. Darren

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    Sometimes it is necessary to know the total duration for which a load has been switched on. A timer can be used to determine how long it has been since the load was last switched on, but to cope with the load going on and off, a little more code is required.

    In the code below, the accumulated time for which the "Kitchen 1" light has been on is displayed on the screen. Timer number 1 records how long it has been since the load was last switched on. The AccumulatedTime variable stores how long the load was on previously. The sum of these two is the total on time. Note that when the Timer is not running, its value will be -1 which will make the total one second lower than it should be. If you can't live with this, you need to check whether the timer is running or not, as shown in the code below.

    Code:
    { var section }
    AccumulatedTime : integer;
    
    { initialisation section }
    AccumulatedTime := 0;
    
    { module }
    once GetLightingState("Kitchen 1") = ON then
      TimerStart(1);
    
    once GetLightingState("Kitchen 1") = OFF then
    begin
      AccumulatedTime := AccumulatedTime + TimerTime(1);
      TimerStop(1);
    end;
    
    { display accumulated time on screen }
    ClearScreen;
    TextPos(300, 100);
    if TimerRunning(1) then
      DrawText(AccumulatedTime + TimerTime(1))
    else
      DrawText(AccumulatedTime);
    
    Note that if you are using a PAC, the timers only go up to 9 hours, so the code may been to be written differently.
     
    Darren, Sep 12, 2005
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.