Skip to main content

2 posts tagged with "timers"

View All Tags

Timers (Continued)

Timers do not have to count down constantly; they can be started and stopped.

Assigning a non-zero value to a timer (i.e., in the Variable Set/Math block) variable always enables the countdown. There is also a somewhat redundant Timer Start block, which does the same job. To pause the timer without resetting its value, use the Timer Stop block.

In this project, pressing the MD button starts the timer. If you keep the button pressed, the timer will count to zero. If you release the button while the timer is still counting, the timer will be stopped, and its current value will be printed to the console.

Timers

This application introduces timers. When a timer is preloaded with a value greater than zero, it counts down at the rate of one count per second. Once the timer reaches zero, it generates an event.

In this application, a timer event causes the timer to be reloaded with the same value, causing it to count down again, and so on.

Times are defined on the Timers page of the Features tab:

timer

Adding a timer automatically creates a variable of the same name.

The type of such timer-linked variables is a dword, which is a 32-bit integer value. In Variables and Arithmetic, we already explained that variable types other than the four types explicitly available to the user exist in "certain parts" of the system. This is one such case. You cannot directly declare a variable of the dword type, but you can create a timer, which will be assigned the dword type automatically.

timer_variable

You can use timer variables anywhere in the application, just as you would use plain variables. The only difference is that when a timer runs, its value automatically decrements with each passing second until it hits zero. Once a timer reaches zero, the On Timer event is generated. This application relies on this event to reload the timer.

Your application may have multiple timers and multiple On Timer event blocks. The properties sheet of the On Timer block allows you to select which timer this block belongs to. However, It is impossible to have multiple On Timer blocks for the same timer.

timer_block

This kind of timer is often referred to as a one-shot timer. It is thus called because the timer stops once the countdown reaches zero. AppBlocks also has periodic timers -- introduced later in this Tutorial -- that keep auto-reloading with the same initial value.

Back to the application now. So, every time the timer expires, it is reloaded by the value stored in the timer_reload_value variable... but where does that variable get its value? The answer: Each variable has a default value property. You can edit default values on the Variables page of the Features tab:

timer_variable_default_value