Control movement , sound and light

11,581
Author: Yisong Xu, Si Xu

On the computer keyboard, press the ↑ key and mBot moves forward; press SPACEBAR and mBot onboard LED lights green; press the number 1 key, and the mBot onboard buzzer sounds.

Hardware requirements:

mBot

Implementation:

Online debugging (serial/Bluetooth connection)

Example program download:

Keyboard Control.sb2

download

Example programs
kids programming language

 

Script description

kids programming language To use a keyboard to control the movement, sound and light effects of mBot, you need to use Block  in the “event” area. You can switch the “space bar” to arrow keys, spacebar, letter keys, number keys and other keyboard keys.
In this example the ↑ key is used to control the movement of mBot. When you press the ↑ key, mBot moves forward.
kids programming language

The forward-moving rotation speed is -255~255. An active number represents moving forward, and a negative one moving back. 0 speed represents keeping still. The bigger the number is, the quicker it moves. 255 indicates 100% power of the motor. In this exmaple the speed is set first to as and then to 0 in 1 second, so as to let mBot move and then stop in 1 second.

For the glowing program, please refer to Example 1 Blink.
For the description of the glowing program, please refer to Example 6 Play a piece of music.

 

Knowledge points
What if the rotation speed for moving forward is beyond 255?
If the speed is set beyond 255, it will be automatically set as 255 which is the maximum speed.

 

Extended tasks
Rewrite the program. Press only one button to let mBot move, make light and sound at the same time.

 

Attched -mBotV1.1 Product Drawing

kids programming language