We know that the S3 can be a very maneuverable robot. The robot’s shape, drive wheel location, and configuration give programmers the ability to finely control its movement. Since we have just finished covering turns and arcs, let’s put your new understanding to the test by programming your robot to create some simple shapes.
Using BlocklyProp to Make a Block or Square
If you tried out your demo programs, you will recall that demonstration program #7 scribbled a figure eight, paused, and then made a path in the shape of a square. We will show you how to create the square and figure eight programs in BlocklyProp, and adding some additional challenges along the way.
- Start a new project and piece together a BlocklyProp program that looks like this one - all the blocks should be familiar.
You have used the drive, wait, rotate, and stop driving blocks already. For these, and any other previously used blocks, we will no longer be adding instructions on where to find them in the BlocklyProp workspace. If you cannot remember where to find them, look around! That’s the best way to learn where everything is.
- Save, compile, load to EEPROM, and run the program.
How did work? If there was a problem, check your blocks. You will notice that after the last side of the square is run, the robot turns. This 90° turn is not required to “close” the square, but it will return the S3 to its original place and orientation.
There is an old expression that experienced programmers use: “Work hard at being lazy!” This means that our code should always try to accomplish as much as possible, in as little code (or blocks) as possible. So, how might you make the code you created above better and more efficient? This program drives forward for 2 seconds and turns 90° - 4 times. That repetition should get you thinking about loops. Remember that program loops allow you to do a process repeatedly, without writing the command again and again. Edit your code by adding one block and deleting nine:
You should have added the loop (x times) block and deleted 3 drive, 3 wait, and 3 rotate blocks. Working hard to be lazy – reducing program space resulted in simpler and more elegant code.
The programming language running these blocks is C. Even without knowing how to read or write in C, you can easily see the effect that this optimizing has on your program's size and complexity.
- Click on the Code button at the top right-hand corner of the page for the long program, and then look again for the shortened one. You may have to rebuild each program to test this out. What do you notice?
Did You Know?
Geometry is a branch of mathematics concerned with the properties and relations of points, lines, surfaces, and solids. Geometry helps us describe and measure basic shapes - squares, triangles and circles. Each is created by combining lines and/or curves.
- A square is four equal length lines connected end to end forming four right angles.
- A triangle consists of three connected line segments. Unlike a square, the angles in a triangle can be of various measurements and are not always right angles. Triangle types are named based on the angles that form the triangle.
- A circle has no straight lines and no angles. All the points that form a circle are equal distance from a central location. Our figure eight is two connected circles.
Shapes are formed by describing the relationship between the components that form the shape, not with fixed dimensions or size. A square is a square, regardless of its size. Similarly, a computer program defines the relationship between the steps or pieces of a process. A program describes a process by relationships. It is a code for how to do something - a pattern.
Try Making a Triangle
The exact same blocks you have been using, but with slightly different parameters, can create a triangle.
- Construct the following program and give it a try.
To make this triangle program you changed one setting: the rotation angle changed to 120° (this will form a 60° angle inside our triangle). Note that you repeated your code for only three sides instead of four. The last rotation gets your S3 back to its starting point. It doesn’t take much work or imagination at all to see how this can be simplified. Just like before, you will add one block and delete many.
The loop (x times) block repeats the drive, wait, and rotate blocks 3 times, finishing again with the stop driving block.
Everyone Loves a Figure Eight
Did you ever notice that a figure eight turned sideways is the symbol for infinity: 8 or ∞. As promised, let’s drive a figure 8 pattern or, if you prefer, to infinity.
- Build this blockly code for your S3 - save, compile, load to EEPROM, and run.
How did it work? Setting the wait time is done by trial and error to set the drive distance of each of the circles of the 8 (or ∞) symbol. Because we are using a less precise way to control the distance travelled, that is, by using time - your mileage may vary.
- Write a BlocklyProp program for a square. This time, use drive speed and drive a turn blocks found in the ACTIONS > MOTORS categories. Use each block 4 times.
- Simplify your program by using the loop block found in the CONTROL category and set to loop 4 times.
- Write programs for a triangle using the same blocks. First, without using the loop block, and then again using the block.
- Write a program to trace out a figure 8 using the rotate block, but this time travel for half a circle clockwise, half a circle counterclockwise - returning to where you began and completing the figure eight. Could a loop be used here?
- Try the same thing using the rotate block 4 times, and then look for ways to simplify the program. Did you find any?