When you're ready to add the motor controller to your robot, you'll want to wire it as shown in the picture below. Remember, when connecting to Pin #, you want to connect to only the blue pin next to the number.
Once that's done, it's time to program it. This code will make the motors drive one way at different speeds (which will cause the robot to turn) for 3 seconds, then it will reverse for 3 seconds. One thing to note, if you're typing this, everything after the // is there for you. You don't have to type it. The robot will ignore it... But it may help you understand what your code does later when you look back.
void setup() {
// Motor_1 control pin initiate
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT); // Speed control
// Motor_2 control pin initiate
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(10, OUTPUT); // Speed control
}
void loop() {
analogWrite(9, 230); // set the motor_1 speed to 230 (out of 255)
digitalWrite(4, HIGH); // set the motor_1 direction to forward
digitalWrite(5, HIGH); // set the motor_1 enabled
analogWrite(10, 50); // set the motor_2 speed to 50
digitalWrite(6, HIGH); // set the motor_2 direction to forward
digitalWrite(7, HIGH); // set the motor_2 enabled
delay(3000); // wait for a 3 seconds
analogWrite(9, 150); // set the motor_1 speed to 150
digitalWrite(4, LOW); // set the motor_1 direction to reverse
digitalWrite(5, HIGH); // set the motor_1 enabled
analogWrite(10, 150); // set the motor_2 speed to 150
digitalWrite(6, LOW); // set the motor_2 direction to reverse
digitalWrite(7, HIGH); // set the motor_2 enabled
delay(3000); // wait for a 3 seconds
}
The next challenge is to get it to drive in a straight line. What do you need to change to make that happen?
Independent Challenge: 1 Meter
Next you need to make your robot stop. You can do that in one of two ways. You can either set the speed to 0 (by changing the speed line to "analogWrite(9, 0);") or by disabling the motor by setting the enable pin to LOW (e.g. "digitalWrite(5, LOW);"). To show that you are in control of what your robot does, make it drive exactly 1 meter and stop.