Press button to play

11,419
Author: Yisong Xu, Si Xu

When the onboard button is pressed, the buzzer sounds; when it is loosened, the buzzer does not sound.

Hardware requirements:

mBot/mCore Control Panel

Implementation:

Upload the program to Arduino

Example program download:

Press button to play.sb2

download

Example programs
mblock


Script description

mblock Define the on-board button state (for onboard button see the attached photo)
mblock Define the tone and rhythms of the buzzer.
mblock When the program is running, judge whether the sound playing task is executed according to the status of the onboard button.
The block indicates that if the event is triggered, the block script within is executed; if the event  is not triggered, the block script within is not executed. In this example when the onboard button is pressed, the script included within is executed.
mblock Repeat it to ensure each pressing makes sound.


Knowledge points
Point 1 Condition judge the logic of Blockmblock
In our life, people need to choose or carry out different tasks according to conditions, such as wearing T shirts in hot weather or padded clothes in cold weather. Here the air temperature is a condition. Whether you wear a T shirt or padded clothes depends on the judgment of the air temperature. For a robot, it turns before a barrier or moves forward if there is no barrier in front. Whether there is a barrier is a condition and whether to turn or move forward is the executed result after judgment.
In mblock,if Condition  is true, execute the block script within. In this example, the condition is “whether the onboard button is pressed”; when it is true, the onboard buzzer sounds a tone and when it is not true, nothing is done.

Point 2 What if I do not use mblock
As the status of the onboard button is judged all the time when the program is running, you need to use the “repeat” block. If you do not use “repeat”, the program, when started, will do judgement once quickly and then it will cease to run, and then pressing the button will not control the buzzer.


Attached
-mCore main control board buzzer and onboard button diagram
mblock

 

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

—— More examples