Learn to Program Self-Driving Cars (and Help Duckies Commute) With Duckietown Robotics
Coding Tips
Explain to your students that they are going to program their robot to move according to a recorded set of instructions given to it through the buttons on the EV3 Brick. Use these parameters:
o Up Button bumped, the robot moves forward 30 cm
o Down Button bumped, the robot moves backward 30 cm
o Left Button bumped, and the robot turns 90 degrees left
o The right Button bumped, and the robot turns 90 degrees right
Recording One Action to Make the Robot Move
Explanation of the Program
1. Start the program.
2. Create a Variable Block called “Drive.”
3. Wait for a brick button to be bumped.
4. Play sound “Click 2.”
5. Record the numerical value of the pressed button in the variable “Drive.”
6. Wait for 2 seconds.
7. Play sound “G02.”
8. Read the number stored in variable “Drive” and send the value to a Switch.
9. Numeric switch:
a. If Drive = 0 (default case), do nothing.
b. If Drive = 1, curve turn the robot left.
c. If Drive = 3, curve turn the robot right.
d. If Drive = 4, move the robot straight forward for 2 rotations of the wheels.
e. If Drive = 5, move the robot straight backward for 2 rotations of the wheels.
10. Play sound “Game Over 2.”
Recording Multiple Actions to Make the Robot Move
The Array Operations Block is used to store a sequence of data. It is often described as a table consisting of a single row with multiple columns.
Explanation of the Solution
1. Start the program.
2. Create a Variable Block called “Drive.” Choose the “WriteNumeric Array” option.
3. Create a Loop. The example code is set to run 5 times.
4. Wait for a brick button to be bumped.
5. Play sound “Click.”
6. Read the Variable Block “Drive.” Choose the “Read Numeric Array” option.
7. Use the Array Operations Block. Choose “Write at Index - Numeric.”
a. Wire-in the "Drive" Variable Block.
b. Wire the Loop index from the front of the Loop into the index location on the Array Operations Block.
c. Wire the value from the wait for EV3 button block into the value location on the Array Operations Block.
8. Write the output of the Array Operations Block into the Variable Block “Drive.”
9. Wait for 2 seconds.
10. Play sound “Go.”
11. Create a second Loop. The example code is set to run 5 times, the same number of times as the first Loop.
12. Read the Variable Block “Drive.” Choose the “Read Numeric Array” option.
13. Use the Array Operations Block. Choose “Read at Index - Numeric” option.
14. Numeric switch:
a. If Drive = 0 (default case), do nothing.
b. If Drive = 1, curve turn the robot left.
c. If Drive = 3, curve turn the robot right.
d. If Drive = 4, move the robot straight forward for 2 rotations of the wheels.
e. If Drive = 5, move the robot straight backward for 2 rotations of the wheels.
15. Play sound “Game Over 2.”
Tabs “1” and “2”