Read The Arduino Inventor's Guide Online
Authors: Unknown
LISTING 8-3:
Simplified version of the
loop()
using the custom function
setMotorA()
void
loop
()
{
//set direction to clockwise
setMotorA(100);
delay
(1000);
//set direction to counterclockwise
setMotorA(-255);
delay
(1000);
//stop
setMotorA(0);
delay
(1000);
}
This code sets a
setMotorA()
value and a delay to make each change in speed and direction. Now you have the beginnings of your Drawbot! Next, you’ll wire the second motor.
The DrawBot needs a second motor so it can zip around on two wheels.
Figure 8-10
shows how the second motor will be wired. Plug Motor B in on the left side of the breakout board just below the connections for the first motor, with the red wire connected to B02 and the black wire connected to B01. Next, add the signal control lines to the H-bridge breakout board, just below the STBY pin on the right side. Connect the PWMB pin on the H-bridge to Arduino pin 10 for speed control, and connect the BIN1 and BIN2 pins to Arduino pins 8 and 9, respectively, for direction control.
FIGURE 8-10:
Wiring diagram for the motor driver and two motors
Now, you’ll need to add code to control the second motor, as shown in
Listing 8-4
.
LISTING 8-4:
Adding constants and
pinMode()
functions for Motor B
const byte AIN1 = 13;
const byte AIN2 = 12;
const byte PWMA = 11;
➊
const byte
BIN1 = 8;
const byte
BIN2 = 9;
const byte
PWMB = 10;
void setup()
{
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
➋
pinMode
(BIN1,
OUTPUT
);
pinMode
(BIN2,
OUTPUT
);
pinMode
(PWMB,
OUTPUT
);
}
This code adds the three additional constants
➊
for the signal control pins for Motor B, and sets each of these pins as
OUTPUT
➋
in the
setup()
.
Next, you’ll again write a custom function to control Motor B. This code is so similar to the
setMotorA()
function that you can save yourself some typing by highlighting the code for
setMotorA()
, copying it (
CTRL
-C), pasting it (
CTRL
-V) below the
setMotorA()
function, and changing the
A
s to
B
s. This is a technique that programmers use a lot, and it can save you a lot of time. You just need to make sure
you’re careful to change all the
A
s to
B
s in this second custom function (
Listing 8-5
), or the code won’t work.
LISTING 8-5:
Custom function for Motor B
void
setMotorB(
int
motorSpeed)
{
if
(motorSpeed > 0)
{
digitalWrite
(BIN1,
HIGH
);
digitalWrite
(BIN2,
LOW
);
}
else if
(motorSpeed < 0)
{
digitalWrite
(BIN1,
LOW
);
digitalWrite
(BIN2,
HIGH
);
}
else
{
digitalWrite
(BIN1,
HIGH
);
digitalWrite
(BIN2,
HIGH
);
}
analogWrite
(PWMB,
abs
(motorSpeed));
}
The sketch will now need
motorSpeed
values for both
setMotorA()
and
setMotorB()
. Let’s add those to test the motors out together.
To make your Drawbot drive forward, you’ll need the right motor to spin clockwise and the left motor to spin counterclockwise. This may seem counterintuitive, but take a look at a robot base from the side.
Figure 8-11
shows a robot frame from both sides with arrows indicating the forward direction.
FIGURE 8-11:
Side views of the robot from the right and left sides. To move forward, the right wheel must spin clockwise and the left wheel must spin counterclockwise.
On the right side of the robot, the wheel needs to spin clockwise for the robot to move forward, but on the left side of the robot, the wheel needs to spin counterclockwise. Pay attention to the direction in which each axle is spinning. If you need to, attach a piece of masking tape to the spinning end of the motor so that you can see the axle’s direction.
Now, to make the robot go backward, you just reverse those directions. After adding the custom function code for
setMotorB()
to your sketch, adjust your
loop()
to look like
Listing 8-6
, and then upload this code and watch your motors spin!
LISTING 8-6:
New
loop()
code to test both motors
void
loop
()
{
//drive forward medium speed for one second
setMotorA(100);
setMotorB(-100);
delay
(1000)
//drive backward quickly for one second
setMotorA(-255);
setMotorB(255);
delay
(1000);
//stop for one second
setMotorA(0);
setMotorB(0);
delay
(1000);
}
You should see that Motor A (right side) is spinning clockwise and Motor B (left side) is spinning counterclockwise, and then after 1 second they flip. If you find that the motors are spinning in the same direction, swap the red and black wire connections on
one
of
the motors.
With just a few lines of code, you can make your Drawbot move forward, turn right, turn left, move backward, and jiggle around!
Now it’s time to build a frame or a
chassis
for your Drawbot. Because the code you wrote is all in the
loop()
part of the sketch, your motors will continue to spin, stop, spin, and stop. To stop the motors from spinning while you’re building the chassis for your Drawbot, temporarily disconnect the USB cable from your computer.
If you’re using the SIK with the breadboard holder and Arduino baseplate, you’ll need to make the chassis of the Drawbot at least as large as the baseplate itself. The baseplate measures 6 inches by 4.25 inches. Use a piece of cardboard or thin plywood to make your chassis. For our design, we made the chassis a rectangular 6 × 8-inch cutout, as shown in
Figure 8-12
. You can download a PDF of this template from
https://www.nostarch.com/arduinoinventor/
.
FIGURE 8-12:
Drawbot chassis, bottom view (not full size)
Using tape or a hot glue gun, attach the motors to the underside of the chassis, oriented as shown in
Figure 8-13
, with the motor hub near the back and the longer end of the motor body toward the front. (Hot glue is a great semipermanent method for attaching things because you can simply scrape it away with a craft knife and remove the part if you want to reuse it later.) You may need to temporarily disconnect the motors from your breadboard circuit while you’re attaching them to the chassis, so just remember to reconnect them to the circuit after you’ve glued them down. Refer back to
Figure 8-10
if you need help rewiring it.
FIGURE 8-13:
Attaching the motors to the chassis with hot glue
We have a hole in our base template that’s designed for a pen or marker. A Drawbot needs to be able to draw, after all! To give the pen more stability, we glued two smaller pieces of cardboard together to create a taller pen holder. Glue these pieces down right on top of the hole, as shown in
Figure 8-14
.
FIGURE 8-14:
Gluing the pen holder onto the chassis
Finally, attach the wheels to the motors. You’ll notice that the motor axles have two flat edges (
Figure 8-15
). Make sure you line these up with the flat edges on the axle holes of the wheels. The fit may be a little tight. Hold on to the entire motor while pushing the wheel on so that you don’t accidentally rip the motor off your chassis.
FIGURE 8-15:
Profile of motor axle. Line up the flat edges with the flats of the opening on the wheel.