There are several different ways to make your robot drive for a certain distance, then stop. We'll examine one below. Can you think of others?
We start with our drive code from before and add a delay of 1000 milliseconds to the bottom of the code. Next we'll need two more Servo blocks to tell the servos to stop and a delay block to prevent it from looping back to the top too soon. We could grab three more blocks or we can right click on the top Servo block, which brings up a menu, and select clone. This will create a copy of every block from that point down. Look for the cloned blocks in the upper left corner of the programming area. Grab the top block and drag it down to connect with the delay block already in the loop. When you drag the top block, all the blocks below it will come along. Now set the angle to 90° for the two new servo blocks and increase the delay to a large number like 999999999. This long delay will effectively end the sketch. If you wait long enough, the robot will start driving again, but it take ~11.5 days, so you're not in a rush.
Below you can see that I didn't use 90° in my code. As we discussed before, each servo is slightly different. Mine stop around 93°. If you use 90° and the servos are still turning, you can adjust the number to make them stop. It's unlikely that they will be the same, so notice which servo is turning and only adjust that one.
Using ArduBlock, you can not stop a servo. You can send it a command that won't make it turn, but you are still sending it a command. Usually this causes the servo to continue to make noise and attempt to get to the position you assigned it. Using code however, you can completely shut it off. In the Drive sketch above, we attach the servos specific pins in the setup. Then we write a position for them to go to in the loop.
#include <Servo.h>
Servo servo_pin_10;
Servo servo_pin_11;
void setup()
{
servo_pin_10.attach(10);
servo_pin_11.attach(11);
}
void loop()
{
servo_pin_10.write( 45 );
servo_pin_11.write( 135 );
delay( 1000 );
servo_pin_10.detach();
servo_pin_11.detach();
delay( 999999999 );
}
This should start to look familiar. Notice that we still have the setup and loop sections and the #include <Servo.h> at the top. Play around with the code to see what happens if you adjust the different variables. What happens if you change the angle the servos are supposed to go to? What happens if you change the length of the first delay?
Independent Challenge: 1 Meter
Your robot drives and stops. Nice work. To show that you are in control of what your robot does, make it drive exactly 1 meter and stop.