Functions
Last updated
Was this helpful?
Last updated
Was this helpful?
In addition to the programming blocks we have already discussed, Scratch also includes a powerful feature called "My Blocks" that allows you to create custom functions that can be reused throughout your program.
By using the "My Blocks" feature, you can create a set of programming blocks that perform a specific task. For example, you could create a block that makes the robot move forward a certain distance, or a block that turns the robot a specific angle.
Once you have created a custom block, you can reuse it throughout your program, rather than having to write the same code repeatedly. This makes your code easier to read and can also save you time when programming.
To create a custom block in Scratch, simply click on the "Make a Block" button in the "My Blocks" category of the block palette. You can then give your block a name and add any necessary code. Once you have created the block, you can drag it into your program and use it like any other block.
Overall, using custom blocks in Scratch can make your programs more modular and easier to read. By creating reusable blocks for common tasks, you can save time and make your code more efficient.
In our example we want to create a block that represents a Forward and a stop motion command for the robot, using the commands we learned earlier.
Now that we have our functions, we can use them in our earlier example to move the robot based on the conditions.
The full example is included below. After connecting and pressing the space bar, the robot will move based on the MODI’s current distance reading.
Now that we have a basic understanding of Scratch and the MODI sensor, we can start introducing coding commands to control the Mantis Scout robot.
The Mantis Scout robot comes with a set of programming blocks in Scratch that allow you to control the robot's movements. The main block for controlling the robot's motors is called the "move" block. You can find this block in the "Robots" category in the Scratch block palette.
The "move" block has several options for controlling the robot's movements, including forward, backward, left, and right. You can also control the speed of the robot by adjusting the value in the block. By using the "move" block in combination with the distance readings from the MODI sensor, you can program the robot to move a certain distance or to stop when it reaches a certain point.
In addition to the "move" block, there are also blocks that allow you to control each motor individually. These blocks can be found in the "Mantis" category in the Scratch block palette as well. By using these blocks, you can make the robot turn or move in more complex ways.
Overall, by combining the distance readings from the MODI sensor with the programming blocks in Scratch, you can create a wide range of movements and behaviors for the Mantis Scout robot.
Commands can be sent to individual motors. The motor dropdown values can be set to Motor (1) or Motor (2). The speed value can range from -100 to 100. -100 would represent a backwards direction, whereas 100 would represent a forward direction for that motor.
The duration value (in milliseconds) controls how long to run the command. Applying a 0 duration sets and indefinite time.
Commands can also be sent to both motors simultaneously using this block
This example would stop the motor because a value of 0 is sent
This command has both motors run for 500ms at the provided speed. Motor’s 1 and 2 in this example will move in opposite directions.