Introduction: Arduino Powered RC Tank

  This is my first instructable so please bear with me. My name is Stanley Deakins and I have been playing with electronics for quite some time, though it was years ago. Due to my recent employment I have rekindle that passion. I have gotten my hands on a few electronic kits but I wanted to do something more on my own.. something a little more challenging.

  Fabricating something to function differently from the manufactured use has always fascinated me. Seeing how the holiday season has came to a close I decided to try to hop on some clearance toys. The first one I bought was a RC truck. Messed with if for a few days and decided that I wanted to go with another platform. The next one I went with was the Popular Mechanics RC tank. This was a much more manageable platform and the basis for this instructable. 

Step 1: Parts List and Tool List

All the parts in this instructable can be purchased at your local Radioshack. I've made it convenient by listing the sku number as well as the price. All of the items on this list can be reused for other projects. Nothing Is permanently attach. No glue or tape.

Part List

                 Part - Sku - Price
Arduino Uno Rev.3 - 2760128 - $34.99
Seeedstudio Motor Shield - 2760242 - $19.99
Seeedstudio Proto Shield - 2760244 - $9.99
Popular Mechanic RC tank - 2800041 - $27.99
9.6v RC connector - 2300445 - $5.29
9.6v RC Battery Pack - 2301171 - $29.99
Parallax Ping Sensor - 2760136 - $32.99

Tool List

Solder
Soldering iron
Zip-ties
20 gauge solid wire
Screw driver set
Wire stripper/cutter




Step 2: Building the Tank

Basically assembly the wheels and add the treads and your ready to move onto the next step. You will not need the parts pictured in the second photo.

Step 3: Securing Battery Pack and Arduino

   I find the process of prototyping very enjoyable. I have these photos only as a reference. Please secure the battery pack and Arduino in any way you deem fit. Use a pair of wire cutter to remove any plastic you may need to on the platform. Not wanting to make anything in the project too permanent I avoid using glue or anything sticky. I instead used 20 gauge solid wire to attach the battery pack to the platform. Then used zip-ties to attach the Arduino to the battery pack. It is very secure.. doesn't budge.

   I wanted to be able to take my proto shield off and my motor shield so the Uno is the only thing secured to the platform. Also be mindful of which direction you are placing the battery pack and the Arduino.  Once everything is secure cut any zip-ties and make sure nothing is hitting or rubbing on the tank treads. 

Step 4: Building the Proto Shield

   The seeedstudio proto shield is great minus a very exceptions. There are non through shield headers. So be mindful when building it.. You wont need the usb port installed on the board. Mine is about as basic as it gets though i do like my leds. Once basic assembly is done add a three pin header to the front of the shield.

   I had to cut the contacts on the board in order to get mine centered. I then ran a red wire from the 5v through hole to the corresponding header. The black wire from the negative and the green wire from the 7 pin in the same fashion. Make sure that you have the correct wires ran to the correct part of the header then solder the connections.


*The header on the bottom of the shield is for a project down the road and will not be covered in this instructable. Please use pictures as reference.

Step 5: Testing the Ping Sensor

Once your proto shield is finished you can attach it to the arduino. After install the shield place the Ping sensor into the header from previous step.  You should now attach your USB cable to the Arduino and upload the following sketch:

/* Ping))) Sensor

   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.

   The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping

   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe

   This example code is in the public domain.

*/

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}



Once sketch is uploaded run the serial monitor and verify that everything is connected and you are getting data from the sensor. 

Step 6: Connecting the Motor Shield

Now that we are sure that the ping sensor is working properly we need to add power to the motor shield. After cutting the ends off of the battery connector strip the wire back about a half an inch. Located on one side of the board you will find a single set of screw terminals. Insert natter connector into proper terminal. Mind polarity. 

  Once the battery connector is connected its time to add the motors to the shield. Cut and disregard the connection ends on the motor wires. Make sure to distinguish which motor is which. Strip the wires about a quarter inch again. Printed on the bottom of the shield is where you can find the polarity of the terminals. Blue is negative and Red is positive. Add one motor to M1 and the second to 
M2. Minding polarity once again.

Step 7: Putting It All Together

OK now all that is left is plugging it all together and uploading to sketch.. I'm not going to go over the sketch too much. Its basically just a modification of the Seeedstudio motor shield demo code. I'm still toying around with the code. Is not perfect but it will give you a good start. Feel free to modify it anyway you would like. 

Upload the following sketch:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Arduino powered RC tank  By: Stanley Deakins                                                                  
//                                                                                                          
//This is a basic sketch used to power an RC tank using a Arduino and a seeedstudio ping sensor. It uses a       
//Parallax ping sensor to navigate. Please feel free to modify this sketch anyway you feel like. This is simply 
//just enough to get you started on the project. Thank and enjoy.                                                
//https://www.instructables.com/id/E2Q88MBHC9MJAFY/                                                                                                               
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Built atop Ping example code by Tom Igoe
// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =127;//define the spead of motor

void setup()
{
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
}

void forward()
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);
}
void backward()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);
}
void left()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);
}
void right()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
     digitalWrite(pinI1,HIGH);
}
void stop()//
{
     digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
     digitalWrite(speedpinB,LOW);
     delay(1000);

}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);

  //
  //if objects are less than 12 inches away
  //the robot reverses and turns to the right
  //for 2 seconds
  //

  if (inches < 8){

    //brake drive motor and pause 1/10 second
  stop();

    //
    //setting motors backwards
    //

  backward();
  delay(700);
  stop();

    //
    //setting motors to turn
    //

  left();
  delay(1100);
  stop();
  }

  //
  //when nothing is within 8"
  //the robot simply drives forwards
  //

  else{

    //
    //Setting drive motor
    //

  forward();
  }
   delay(100);
  }
  long microsecondsToInches(long microseconds)
  {
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
  }

Step 8: Future Plans

I'm planning on installing a servo for the ping sensor. I'm also planning on installing a camera as well as a Bluetooth module. I want to be able to control it with my android phone and see what it sees. I hope you have enjoyed this instructable. It was a fun project and helped me learn a good deal about coding the arduino. Please check back in the future to see what else I have in store for this little robot.