Arduino Uno
Arduino Wireless SD shield with RN-XV
Arduino Motor shield
I recently purchased from sparkfun.com.... I was wanting to make an internet controlled tank bot using the motor and tank tread from the Popular Mechanics RC Tank...
My current roving tank bot with out internet control or the Wireless SD shield looks like this:
#include <Servo.h>
int signal=7; //ping signal
int distance;
int bumper_R = 5;
int bumper_L = 6;
int bumper_R_val;
int bumper_L_val;
int leftDistance, rightDistance;
Servo panMotor; //ping sensor servo
unsigned long pulseduration=0;
void setup(){
pinMode(signal, OUTPUT);
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
pinMode(8, OUTPUT); //Initiates Brake Channel B pin
panMotor.attach(10,.5,2.3);
panMotor.write(90);
pinMode(bumper_R, INPUT);
digitalWrite(bumper_R, HIGH);
pinMode(bumper_L, INPUT);
digitalWrite(bumper_L, HIGH);
Serial.begin(9600);
}
void measureDistance()
{
// set pin as output so we can send a pulse
pinMode(signal, OUTPUT);
// set output to LOW
digitalWrite(signal, LOW);
delayMicroseconds(1);
// now send the 5uS pulse out to activate Ping)))
digitalWrite(signal, HIGH);
delayMicroseconds(1);
digitalWrite(signal, LOW);
// now we need to change the digital pin
// to input to read the incoming pulse
pinMode(signal, INPUT);
// finally, measure the length of the incoming pulse
pulseduration=pulseIn(signal, HIGH);
}
void loop()
{
// get the raw measurement data from Ping)))
measureDistance();
// divide the pulse length by half
pulseduration=pulseduration/2;
bumper_R_val = digitalRead(bumper_R);
bumper_L_val = digitalRead(bumper_L);
// now convert to centimetres. We're metric here people...
distance = int(pulseduration/29);
int distanceFwd = distance;
// Display on serial monitor
Serial.print("Distance - ");
Serial.print(distance);
Serial.println(" cm");
// when characters arrive over the serial port...
if (bumper_R_val == 0){
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the Brake for Channel B
delay(100);
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //back up
delay(1250);
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //turn left
delay(1000);
}
else if (bumper_L_val == 0){
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the Brake for Channel B
delay(100);
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //back up
delay(1250);
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //turn right
delay(1000);
}
else if (bumper_L_val == 0 || bumper_R_val == 0){
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the Brake for Channel B
delay(100);
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //back up
delay(1250);
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //turn right
delay(1750);
}
else if (distance >= 20 || distance <= 0) {
// wait a bit for the entire message to arrive
delay(100);
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
else {
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the Brake for Channel B
panMotor.write(-20);
delay(350);
measureDistance();
measureDistance();
pulseduration=pulseduration/2;
distance = int(pulseduration/29);
rightDistance = distance ; //scan to the right
delay(400);
panMotor.write(190);
delay(430);
measureDistance();
measureDistance();
pulseduration=pulseduration/2;
distance = int(pulseduration/29);
leftDistance = distance; //scan to the left
delay(400);
panMotor.write(90); //return to center
delay(300);
compareDistance();
}
delay(100);
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //turn left
delay(1000);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //turn right
delay(1000);
}
else //if they are equally obstructed
{
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed //Do 180
delay(1250);
}
}