Introduction
One of your tasks on the mission is to secure the planet for mankind - learn how to speed up your programs to get our robot to patrol the planet surface.
Tools
No tools specified.
-
-
Make sure your robot is setup in the same way as the previous sections!
-
-
-
For the rest of this lesson, we need to be able to make the robot turn accurately.
-
Build the simple test program in the picture - it should make your robot spin on the spot for 1 second, and then stop.
-
There are 1000 milliseconds in 1 second - adjust the number until your robot turns by exactly 90 degrees. Don't forget to get the challenge checked off when you've done it!
-
-
-
Now you can turn by 90 degrees, write a program that makes your robot move in a square!
-
It should like something like the example in the picture but your 'delay' times will be different.
-
If you think about it, you only need to reverse 1 motor and then set it to forwards again to change direction - one motor can be going forward all the time.
-
Also, you don't necessarily need to stop after turning - just change the motor going in reverse to go forward again!
-
-
-
Making that last program took a while - and the robot was just doing the same things over and over again.
-
Say we wanted to drive in a square 10 times - that would take ages to program!
-
Driving in a square was doing the same thing 4 times:
-
Drive Forward
-
Delay
-
Turn
-
Delay
-
We can use the loop to get the computer to repeat these steps for us!
-
-
-
So far, we have just been using the setup part of the program.
-
Any code inside the setup gets run only once, as soon as the run button on the robot is pressed.
-
Once the setup has finished, any code in the loop is run again and again until the robot is turned off!
-
-
-
Change your program so that you only have some lines of code in the loop, that make the robot:
-
Drive forwards
-
Turn 90 degrees
-
Don't forget, you still need the invent.begin(); line in the setup!
-
Check the picture for a hint if you need it - the program in the picture will drive forwards, then turn 90 degrees, but it will only do it once!
-
-
-
Now your robot can drive in a square using a loop, let's change the code so it will drive in a figure of 8.
-
Have a look at the picture if you don't know what a figure of 8 is.
-
Try to split the shape up into 2 sections, and put it in the loop''' to reduce the length of your program.
-
Make sure your robot drives in the figure of 8 properly, and ends up where it started !
-