The On-board Button and the Timer
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?
- Can you write a program that turns the LEDs on, and then times how long it takes someone to press the on-board button, displaying that time in the mBlock environment?
- Can you write a program that counts how many times the spacebar is pressed in 5 seconds, and displays that number in the mBlock environment?
- Can you write a program that has the mBot moving at variable speeds for 10 seconds?
Construct Your Dreams!