Using broadcast events
Broadcast events are messages sent from one part of a program to one or more other parts. In robotics they can be used to handle unexpected situations. For example if an emergency stop button is pressed, a broadcast event can be sent to various parts of the robot control code to stop all operations. This lecture explains how to create and how to use broadcast events.
Figure 1 - Program about sending broadcast message
Learn how you can send a broadcast message
First in this task you will discover how you can send broadcast messages, so how you can send message from one part to other part. You can find blocks for send broadcast messages in the 'Control' category. There is a 'broadcast [message]' block and a 'broadcast [message] and wait' block for it. You can type the message in the block's parameter, if you click on 'new'. You will see a pop-up window called 'Message name' (Figure 2). Type your message here. Now, if you take the 'broadcast' block into a program it will send a message to a block where you react to the message.
Figure 2 - A window where you can type the broadcast message
Learn how you can react to the broadcast message
Now, If you have already learned how you can send broadcast message let's discover how you can react to the broadcast message in this task. You can react to a broadcast message with a 'when I receive [message]' block (Figure 3). In the block's parameter you can set the message that is in the 'broadcast' block. When a block get a broadcast message the blocks will execute that have attached to the 'when I receive' block. It is the way to react to the broadcast message.
Figure 3 - 'when I receive' block that can react to the message
Write a program that can send a broadcast message
In this task you will write a program that can send a broadcast message if the robot crashes with an obstacle. For that you need to create a broadcast message with the 'broadcast' block. The message will be 'obstacle'. The program should drive the motor forward and if the robot crashes with an obstacle, so the Touch sensor is being pressed you should create a broadcast message (Figure 4).
Figure 4 - Send a broadcast message in a program
Upgrade the program with react to the brodcast message
In this task you will upgrade the program. You will have a broadcast message if your robot crashes with an obstacle. In this task you will react to the broadcast message. For that use the 'when I receive' block. First the Brick's LED should flashing. Second the Brick should show a sad face. Third the Brick should play a 'beep' (Figure 5). Now, you can see how easy to use the broadcast events.
Figure 5 - A program that react to the broadcast message
Download the program|
The program is capable to react to a broadcast message.
Download Ozeki SNAP code
- My first 'hello robot' program
- Saving and loading my project, writing sequential programs
- Using loops to repeat tasks
- Understanding events
- Understanding conditions (and using sensors)
- Using variables and operators
- Using nested loops and conditions
- Making blocks
- Making blocks with input and return value