Greetings,
Im currently working on a project to make an RC car autonomous by integrating a Arduino Uno with a Seeed Motorshield v1.2, two DC motors, a servo, and a PING sensor. One dc motor controls foward and backwards motion of the car. Another controls left and right of the car. The ping sensor is mounted in the front atop of the servo. I would like the servo to sweep 180 degrees when the ping sensor says its x distance away from an object so that car can avoid hitting something and then drive around it. Im having a bit of trouble trying to debug my issue. The issue being I cannot get the servo to work along with the two DC motors. I have gotten code to get the servo and ping sensor to work together. And I have also gotten code to work for the Ping sensor and the two DC motors to work together. Both scenarios have worked with all the components hooked up. When I try to mesh the two codes, only the servo and ping sensor work.
I have tried to pinpoint the problem and it seems when I write servo.attach(5) in the code that has the PING sensor and two dc motors, the motors stop working all together. If someone could explain to me why that line of code causes the motors to stop working that would be great. I have been playing with this and getting no where for about 15 hours…Could it be I need a common ground or something? The code is listed below
[code]#include <Servo.h>
Servo servo;
const int pingPin = 6;
const int dangerThresh = 10; //threshold for obstacles (in)
int pinI1=8;//define I1 interface, D8
int pinI2=11;//define I2 interface , D11
int pinI3=12;//define I3 interface , D12
int pinI4=13;//define I4 interface , D13
int speedpinA=9;//enable motor A
int speedpinB=10;//enable motor B
int spead = 250;//define the spead of motor
int distanceFwd;
long duration; //time it takes to recieve PING))) signal
long cm;
void setup()
{
Serial.begin(9600);
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);
Serial.println(“MotorTest”);
}
void loop()
{
// Send out PING))) signal pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//Get duration it takes to receive echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//Convert duration into distance
cm = duration / 29 / 2;
Serial.print(cm);
Serial.print("cm, ");
Serial.println();
if (cm < 10)
{
Serial.println(“backward”);
backward();
delay(1000);
Serial.println(“stop”);
stop();
}
else
{
Serial.println(“forward”);
forward();
delay(1000);
Serial.println(“stop”);
stop();
}
delay(100);
}
void forward()
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);
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,LOW);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
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);
}[/code]