Next: , Previous: , Up: The Alarm System   [Contents]


2.6.5 Adding the Siren and Timer

Next we add a siren to be sounded only when a user takes too long to enter the pincode after a sensor trigger makes the LED go red. The siren interface and component are simple:

interface ISiren {
  in void turnOn();
  in void turnOff();
  behaviour { on turnOn, turnOff: {}
  }
}

component Siren { provides ISiren iSiren; }

We’ll put the siren in its own file Siren.dzn and include that file in the Controller. In the next step we turn on the siren whenever Alarming, and turn it off whenever returning to the Armed state.

import LED.dzn;
import Sensor.dzn;
import Siren.dzn;

interface IController {
  in void validPincode();
  behaviour { on validPincode: {} }
}

component Controller {
  provides IController iController;
  requires ILED iLed;
  requires ISensor iSensor;
  requires ISiren iSiren;

  behaviour {
    enum State { Unarmed, Armed, Alarming };
    State state = State.Unarmed;
    [state.Unarmed] {
      on iController.validPincode(): {
        iSensor.turnOn();
        state = State.Armed;
        iLed.setYellow();
      }
      on iSensor.triggered(): {}
    }
    [state.Armed] {
      on iController.validPincode(): {
        iSensor.turnOff();
        state = State.Unarmed;
        iLed.setGreen();
      }
      on iSensor.triggered(): {
        iSensor.turnOff();
        state = State.Alarming;
        iLed.setRed();
        iSiren.turnOn();
      }
    }
    [state.Alarming] {
      on iController.validPincode(): {
        iSensor.turnOn();
        state = State.Armed;
        iLed.setYellow();
        iSiren.turnOff();
      }
      on iSensor.triggered(): illegal;
    }
  }
}

As an exercise, modify the above model to turn on the siren only on a 30-second timeout after the system has been triggered into the Alarming state. You will need to import the Timer component we created earlier and configure it correctly. The fragment of model code below shows only the key changes needed to the Controller event handling; if you copy and paste this partial solution into Dezyne you will still have to debug and finish it.

      on iSensor.triggered(): {
        iSensor.turnOff();
        state = State.Alarming;
        iLed.setRed();
        iTimer.start($30000$);
      }
      on iTimer.timeout(): illegal;
    }
    [state.Alarming] {
      on iTimer.timeout(): {
        iSiren.turnOn();
      }
      on iController.validPincode(): {
        iTimer.cancel();
        iSensor.turnOn();
        state = State.Armed;
        iLed.setYellow();
        iSiren.turnOff();
      }

Next: , Previous: , Up: The Alarm System   [Contents]