In this tutorial, I would like to introduce the on-board button and the timer. The on-board button has 2 related programming blocks in mBlock. The first one is a header block:
This can be used as a trigger for some code to be run. NOTE: this block can only be used when running programs from the mBlock environment.
The second block is a Boolean block. A Boolean block is one which can be used in a conditional (like “if”) and will return either true or false:
The drop-down menus for both blocks are either “pressed” or “released”.
There are two timers in mBlock – one runs within the mBlock environment, and the other runs on the board. Therefore which timer you use should depend on where you intend to run the program. The mBlock environment timer blocks can be found in the “Sensing” scripts:
The robot timer blocks can be found in the “Robots” scripts. We can use the “reset timer” block to set the timer to zero:
Once the timer is reset to zero, it will begin counting immediately. You can later access how much time has elapsed since you reset the timer by using this block:
In this simple program, pushing the on-board button resets the timer and the proceeding loop is run until the timer exceed 10 seconds.
In this example, the effect is that the robot will run forward for a speed of 10 seconds. You may be thinking “Why not just turn the motors on at speed 100, wait for 10 seconds, and then turn them off?”. And you would be right. In this case, it would have the same effect. But what if you wanted to have a variable speed?
Construct Your Dreams!