How to use and/or create a Timer
PRESENTATION
Prerequisites
- Have done the tutorial Start to use Sim4Sys with a simple model (blackbox),
- Have a look at Countdown page.
Skills that you will acquire
In this tutorial, you will learn two ways to model a timer:
- via the VB’s HMI Element “Countdown”
- entirely inside Designer Papyrus
At the end, we want a common behavior:
- Start a timer for several seconds,
- Be able to pause & resume this timer and see its state modification,
- Be informed of the end of this timer,
- Stop this timer at any time and see its state modification.
Duration
1h30
USE COUNTDOWN HMI ELEMENT
In Designer Papyrus
Create a new model TimerModel, with a Project TimerProject and a service TimerSystem.
In the State Machine:
- rename "FirstState" as "Initialized" (this is the starting state, where the countdown can be set up),
- add the state "Countdown_VB" (a specific state that may transmit the behavior of the countdown to VB).
You will need four requested flows:
- requests_start_countdown(value: Integer),
- requests_stop_countdown(),
- requests_pause_countdown(),
- requests_resume_countdown().
And four flows To ENV:
- starts_countdown(value: Integer),
- stops_countdown(),
- pauses_countdown(),
- resumes_countdown().
And one flow From ENV:
- countdown_ended().
In Standard Use, create four UseCases and five UserStories:
- Start a countdown
- User starts a countdown
- End of countdown
- Stop a countdown
- User stops a countdown
- Put countdown in pause
- User puts a countdown in pause
- Resume a countdown
- Users resumes a countdown
In VB
Drag & drop a Countdown, put “Visibility” to hidden.
Create mappings: Four model to simu mapping instances
- starts_countdown mapping
- Check the triggered behaviours “Start the countdown” & select the variable “value” as “Remaining time”
- Check “Visibility” and select visible
- stops_countdown mapping
- Check the triggered behaviours “Stop the countdown”
- Check “Style” and put “fill:red;”
- pauses_countdown mapping
- Check the triggered behaviours “Pause the countdown”
- Check “Style” and put “fill:grey;”
- resumes_countdown mapping
- Check the triggered behaviours “Resume the countdown”
- Check “Style” and put “fill:black;”
One simu to model mapping instances
- (Countdown ended) countdown_ended mapping
- Select the Event “Countdown ended”
In the timeline, add five “Timeline Actions” as triggers, with four simu to model mapping instances:
- (On trigger) requests_start_countdown mapping
- Set the “value” to 5 (seconds)
- (On trigger) requests_pause_countdown mapping
- (On trigger) requests_resume_countdown mapping
- (On trigger) requests_stop_countdown mapping

Run
CREATE YOUR OWN TIMER INSIDE A MODEL
In Designer Papyrus
Update the State Machine:
- add another State “Countdown_Model” (this state represents the logic and data model of the countdown),
- add two sub-states “Started” (the countdown is actively running) & “Paused”(the countdown is temporarily halted).
Add one flow From Engineer, to get Virtual Bench’s timestep:
- deltaT (value: Integer)
The value getting from VB is in ms!
To convert this received value in seconds: add a Simulation Phase, a UseCase “Convert delta from ms to s” and a UserStory “Conversion” In the Sequence Diagram, add the Engineer Life Line, draw the Message “deltaT” from Engineer to TimerSystem (in State Initialized), then add an activity “convert()” an create a new Decimal variable named “deltaT_in_s”, copy/paste the followed code:
deltaT_in_s = deltaT_value/1000.0;
Don’t forget the “.0” after “/1000”, overwise deltaT_in_s will be an Integer
You should have this Sequence Diagram at the end:

You will need four requested flows:
- requests_start_timer(value: Decimal)
- requests_stop_timer()
- requests_pause_timer()
- requests_resume_timer()
Four feedbacks:
- display_timer (value: Decimal)
- paused_timer()
- stopped_timer()
And one flow From ENV:
- clock()
In Standard Use, create four UseCases & six UserStories:
- Start a timer
- User starts a timer
- New Decimal variable named “timer”
- Activity: register_timer_value()
- Body: timer = requests_start_timer_value;
- Decrementation of the timer
- GC: timer > 0
- Activity: decrease_timer()
- Body: timer -= deltaT_in_s;
- End of timer
- GC: timer <= 0
- Activity: reset_timer()
- Body: timer = 0.0;
- User starts a timer
- Stop a timer
- User stops a timer
- Activity: reset_timer()
- Body: timer = 0.0;
- Activity: reset_timer()
- User stops a timer
- Put timer in pause
- User puts a timer in pause
- Resume a timer
- Users resumes a timer
At the end, you should have this State Machine:

In VB
Drag & drop an Hour Display, put “Mode” to “Only seconds” & “Visibility” to hidden.
Create mappings: Three model to simu mapping instances
- display_timer mapping
- Check “Seconds” (not “Second”) & select the variable “value”
- Set “Color” to #000000
- Check “Visibility” and select visible
- paused_timer mapping
- Set “Color” to #949494
- stopped_timer mapping
- Check “Seconds” (not “Second”) & set it to 0
- Set “Color” to #ff0000
In the timeline, add 5 “Timeline Actions” as triggers, with 4 Simu to model mapping instances:
- (On trigger) requests_start_timer mapping
- Set the “value” to 5 (seconds)
- (On trigger) requests_pause_timer mapping
- (On trigger) requests_resume_timer mapping
- (On trigger) requests_stop_timer mapping

You can finally run the simulation.