You can use functions or subroutines to reduce the number of times you have to rewrite code. By putting the code in its own "void [name]" you can then send the program to that piece of code by calling "name();" Below you can see where we did this both in ArduBlock and the IDE. The subroutine and subroutine commands blocks are both under Control. By adding a subroutine to the main loop (and changing the name to forward or turn), that subroutine is called so the robot will jump to that part of the code elsewhere on your ArduBlock canvas. There you can tell the robot what to do using the servo and delay commands you've used before.
#include <Servo.h>
Servo servo_pin_10;
Servo servo_pin_11;
void forward();
void turn();
void setup()
{
servo_pin_10.attach(10);
servo_pin_11.attach(11);
}
void loop()
{
forward();
turn();
forward();
forward();
turn();
forward();
}
void forward()
{
servo_pin_10.write( 45 );
servo_pin_11.write( 45 );
delay( 200 );
}
void turn()
{
servo_pin_10.write( 45 );
servo_pin_11.write( 135 );
delay( 500 );
}
Independent Challenge: Z-Maze
Can you use subroutines to get your robot to drive through a simple maze that the class creates? Have the maze start with the robot needing to go one unit forward, turn left, drive two units forward, turn right, and drive one unit forward to get out of the maze. Then see how complex the maze can be and still have the robot navigate through it.