Let's Make Robots!

Phase 2 - Install and test motor shield

For this phase of the project, I am using the SainSmart Motor Shield, which is a clone of the Adafruit Motor Shield (V1). It is very easy to set up and use and even has a URL printed on the PCB for the Adafruit Motor Shield Library Download. That link is an awesome resource on both this shield and the motor library, so the software install will not be documented here. I have purchased from Adafruit in the past and for the project, I tried to order from them again, but they are no longer supporting the USPS, which is the cheapest, fastest shipping method in the US that is both trackable and guaranteed 3 day delivery anywhere in the US. When I asked them what was up with dropping it, they said they no longer use USPS to my state. Wot?? I'm literally one state away! Anyway...

I will be powering the Arudino & Motor Shield separately, given that by project's end, there will be multiple sensors, motors & servos on board. I am using the stock battery tray to power the motors with rechargeable AA batteries and have toyed with the idea of upgrading to a 9.6V hobby grade rechareable battery with siginificantly larger mAh, but that will have to wait for a future phase. There was only one catch to using the separate power supplies with the Motor Shield, as noted further below.

Again, all my project photos are HERE so that you can see more pictures and see them larger.

Wiring was easy and straight forward.

The only one tiny oddity of the Maisto and/or SainSmart Motor Shield is that +/- orientation of the front motor is different from the +/- of the rear motor & external power supply. What I mean by that is you can see in the foreground of the picture on the power terminal all by itself. VCC is on the left and GND is on the right. Since I knew that terminal's power orientation, I tried to apply it to the other motor terminals also. On the rear motor (background right in the picture), it worked out, but on the front motor, it had to be switched. You could, of course, just write the code the opposite way (forward/reverse), but I just used the same test code below for each motor termial one by one as I plugged them in and rant the code to test. That way I only changed the motor # assignment in the code, which is simpler. As stated in Phase 1, the grey and purple wires are for the steering motor.

http://i1277.photobucket.com/albums/y485/BeepBoopBuzz/IMG_5827_zps74cf50ab.jpg

http://i1277.photobucket.com/albums/y485/BeepBoopBuzz/IMG_5827_zps74cf50ab.jpg

 

#include <AFMotor.h>

AF_DCMotor motor(2); // create motor #2, this is the number i changed for each motor

void setup() {

  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Motor test!");

  motor.setSpeed(200);     // set the speed to 200/255

}

void loop() {

  Serial.print("tick");

  motor.run(FORWARD);      // turn it on going forward

  delay(3000); // 3 second run time

  Serial.print("tock");

  motor.run(BACKWARD);     // the other way

  delay(3000);

  Serial.print("tack");

  motor.run(RELEASE);      // stopped

  delay(3000);

}

Every motor works as intended now! Time to close this chassi back up and tidy up the wires - still thinking I will use this small enclosure space for later.

The ball and socket joints are kind of tricky to get back into place unless you have an extra pair of hands. Mind the motor wires entering from the bottom! You don't want to crunch/cut them when screwing the enclosure back.

Next I used a hand drill to make a pilot hole in the top of the chassis so I could pull the wires, then simply widened the hole appropriately with a knife. Before I pulled the wires through, I separated and taped them in the appropriate pairing (didn't tape the battery wires so I knew which red/black pair was which, because there's 3 after all). Then I pulled the wires through for the Microcontroller to sit atop this chassis until I get an enclosure designed and built.

http://i1277.photobucket.com/albums/y485/BeepBoopBuzz/IMG_5836_zps9d41c1cc.jpg

http://i1277.photobucket.com/albums/y485/BeepBoopBuzz/IMG_5836_zps9d41c1cc.jpg

Now for the part that wasn't documented clearly in the tutorials: how to run separate power to both the Arduino and Motor Shield. Sure, the tutorials mention a jumper, but don't get very specific for a newbie and I like pictures... can't you tell? I hooked up the Motor Shield's power terminal to the stock battery and the Arduino to my computer, but clearly the stock battery power wasn't being used, as the motors didn't have enough power to turn.

After much googling, it turns out that you need to remove the jumper cap pictured below to utilize separate power sources, otherwise even if both power sources are hooked up, it only draws from the Arduino's power source... or if there is no Arduino power source connected, the Arduino runs off of the Motor Shield power source. But, I wanted separate power for each. Remove that little blue cap I'm pinching in the photo below. After it's gone, two male pins will be accessible. For this project, I don't utilize them any further.

http://i1277.photobucket.com/albums/y485/BeepBoopBuzz/IMG_5832_zps8f08052b.jpg

http://i1277.photobucket.com/albums/y485/BeepBoopBuzz/IMG_5832_zps8f08052b.jpg

Now that everything is powered up, it was time to run a motor party sketch to see everything working in conjunction... and everything worked perfectly!

#include <AFMotor.h>

AF_DCMotor frontMotor(1); //Declaration of Front Motor for M1 of Motor Shield

AF_DCMotor rearMotor(3);  //Declaration of Rear Motor for M3 of Motor Shield

AF_DCMotor steerMotor(2);  //Declaration of Steer Motor for M2 of Motor Shield

 

int runSpeed = 255, turnSpeed = 255; //Declared speed for the Motor

void setup() 

{

  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Motor test!");

 frontMotor.setSpeed(runSpeed);

  rearMotor.setSpeed(runSpeed);

  steerMotor.setSpeed(turnSpeed);

  frontMotor.run(RELEASE);

  rearMotor.run(RELEASE);

  steerMotor.run(RELEASE); 

}

void loop() 

{

  AllForward(); // All wheels forward

  delay(3000);

  AllStop();

  delay(3000);

  GoBack();

    delay(3000);

    AllStop();

  delay(3000);

turnRight();

  delay(3000);

  AllStop();

  delay(3000);

turnLeft();

  delay(3000);

AllStop();

  delay(3000);

}

 

void AllStop() 

{

  frontMotor.run(RELEASE);

  rearMotor.run(RELEASE); // Stop Left Servo

}

void AllForward() 

{

  Serial.println("Going forward");  

  frontMotor.run(FORWARD);

  frontMotor.setSpeed(runSpeed);

  rearMotor.run(FORWARD);

  rearMotor.setSpeed(runSpeed); // Motor 2 goes forward

}

void turnRight() 

{

  steerMotor.run(FORWARD);

  steerMotor.setSpeed(turnSpeed);

  frontMotor.run(BACKWARD);

  frontMotor.setSpeed(runSpeed);

  rearMotor.run(BACKWARD);

  rearMotor.setSpeed(runSpeed);

  delay(2000);

  Serial.println("Motors going Right");

  steerMotor.run(BACKWARD);

  steerMotor.setSpeed(turnSpeed);

  frontMotor.run(FORWARD);

  frontMotor.setSpeed(runSpeed);

  rearMotor.run(FORWARD);

  rearMotor.setSpeed(runSpeed); 

  delay(2000);

  steerMotor.run(RELEASE);

}

void GoBack(){

  Serial.println("Backward"); // Makes the robot go back

  frontMotor.run(BACKWARD);

  frontMotor.setSpeed(runSpeed);

  rearMotor.run(BACKWARD);

  rearMotor.setSpeed(runSpeed); // Motor 1 goes back

  delay(800); 

}

void turnLeft() 

 steerMotor.run(BACKWARD);

  steerMotor.setSpeed(turnSpeed);

  frontMotor.run(BACKWARD);

  frontMotor.setSpeed(runSpeed);

  rearMotor.run(BACKWARD);

  rearMotor.setSpeed(runSpeed); 

  delay(2000);

  Serial.println("Motors going Left");

  steerMotor.run(FORWARD);

  steerMotor.setSpeed(turnSpeed);

  frontMotor.run(FORWARD);

  frontMotor.setSpeed(runSpeed);

  rearMotor.run(FORWARD);

  rearMotor.setSpeed(runSpeed);   

  delay(2000);

  steerMotor.run(RELEASE);

}

After seeing everything run independently, I am happy with the progress so far.
http://i1277.photobucket.com/albums/y485/BeepBoopBuzz/IMG_5838_zps9a2fba80.jpg
Here's a link to the video of it driving on set commands.
End Phase 2