Theory:
So far the robot has only gone forward (and possibly backward), but how do you get it to turn? There isn't a wheel you can angle like on a bike or car. Instead, it uses differential steering like a tank. If only one wheel rotates, that side of the robot will move while the other side stays in place. This causes the robot to turn, but it will turn even faster if one wheel rotates forward while the other one rotates backward.
Make It Turn:
Remember when we first started using the servos, for the robot to drive forward, one wheel had to rotate clockwise while the other rotated counter-clockwise because the servos are pointed in opposite directions. So if the robot is going to turn, both must rotate clockwise (or counter-clockwise) so that one side of the robot will drive forward and the other in reverse. All we need to do is change the 135° to 45° and the robot will start to turn.
Having the robot just turn isn't very interesting, so let's have it drive forward, then turn. Start with the Drive code and add a pause. Then clone those three blocks like the Drive and Stop code. This time, don't set the second set of servo commands to stop, instead set them both to 45° to make it turn. That will make it drive and turn, but let's add one more piece that will be useful for the Independent Challenge. Go to Control and grab a repeat block. Drag this to the top of main loop and then put all the other code inside the repeat. It defaults to repeating 5 times. Of course once it finishes the repeat, it hits the bottom of the loop and starts over, so it won't do much good yet. Here's the AdruBlock sketch.
When we look at the code, there are a few new things.
#include <Servo.h>
Servo servo_pin_11;
int _ABVAR_1_;
Servo servo_pin_10;
void setup()
{
servo_pin_11.attach(11);
servo_pin_10.attach(10);
}
void loop()
{
for (_ABVAR_1_=0; _ABVAR_1_< ( 5 ); ++_ABVAR_1_ )
{
servo_pin_10.write( 45 );
servo_pin_11.write( 135 );
delay( 1000 );
servo_pin_10.write( 45 );
servo_pin_11.write( 45 );
delay( 1000 );
}
}
The setup looks the same, but notice there is an extra line above the setup. Where we normally declare the names of the servos, there is also int _ABVAR_1_;. This tells the robot to set aside an area of memory for a variable called _ABVAR_1_ and that this variable will be an integer (int). That means the value it stores will always be a whole number and, because of the amount of memory set aside, it will be between -32,768 and 32,767.
The next change in is the loop. A new command is used: for (_ABVAR_1_=0; _ABVAR_1_< ( 5 ); ++_ABVAR_1_ ). This is how it translated the repeat command. What it is telling the robot is to do the code below in the { } for a certain number of times. To know how many times to repeat the code, we need to look at what's after the for. First, it take the variable we created before (_ABVAR_1_) and sets it equal to 0. Then it says to keep doing the for loop as long as _ABVAR_1_ is less than 5. This is the "test condition". Finally it requires that the variable be increased by 1 (++) each time before it checks the test condition. The use of ++ to mean increase by one might not seem obvious but it turns out it happens a lot in programming and rather than write "_ABVAR_1_ = _ABVAR_1_ + 1" each time, the programmers created a shortcut. You can also use -- to decrease by one. One last thing to note, the indentation of the code in the for loop was not auto-generated and was done to make it easier to read.
Independent Challenge: Square
Now that your robot can drive forward and turn, make it drive in a perfect square with 1 meter sides. You'll need to change the delay for the drive forward section and the delay for the turn section. How many times should it repeat those? When it finishes repeating it the correct number of time, it needs to stop near where it started, pointing in the same direction.