midterm-rc car with ultrasonic sensor

I made an obstacle avoiding rc car.

The sensor is mounted on the servo. For some reason when the servo is attached in the setup, the dc motors stop running, but the servo works and prints information to the console as it should.

 

here is simple version my code without the servo running-

int pinSpeaker=8;

float sinVal;
int toneVal;
unsigned long echo = 0;
int ultraSoundSignal = 7; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
#include <Servo.h> // Enables the Servo library
int pingPin = 7 ; // Ping sensor is connected to port A0
int centerDist, leftDist, rightDist, backDist; // Define variables center, left, right and back distance
long duration, inches, cm; // Define variables for Ping sensor

#include “MotorDriver.h”

#include “Servo.h”
Servo PingServo;
void setup()
{
Serial.begin(9600);

pinMode(pinSpeaker,OUTPUT);
pinMode(ultraSoundSignal,OUTPUT);
motordriver.init();
motordriver.setSpeed(110,MOTORA);
motordriver.setDirection(200,MOTORB);
// PingServo.attach(10); // Servo is attached to pin 10 in the motor shield
PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
}

unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
// please note that pulseIn has a 1sec timeout, which may
// not be desirable. Depending on your sensor specs, you
// can likely bound the time like this — marcmerlin
// echo = pulseIn(ultraSoundSignal, HIGH, 38000)
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
return ultrasoundValue;
}

void loop()
{

int x = 0;
x = ping();
Serial.println(x);
delay(250); //delay 1/4 seconds.

if (x >= 15){
motordriver.rotateWithID(MOTOR_CLOCKWISE, MOTORA);
}
else{
tone (8,100,76);
delay(105);
motordriver.rotateWithID(MOTOR_ANTICLOCKWISE, MOTORA);
motordriver.rotateWithID(MOTOR_CLOCKWISE, MOTORB);
delay(4000);

 

————————-

and here is the code that isn’t running with the servo-
#include “MotorDriver.h”
#include “Servo.h”// Enables the Servo library
Servo PingServo;

int minSafeDist = 11 ; // Minimum distance for ping sensor to know when to turn
int pingPin = 7 ;
int centerDist, leftDist, rightDist, backDist; // Define variables center, left, right and back distance
long duration, inches, cm; // Define variables for Ping sensor
void setup() {
Serial.begin(9600); // Enables Serial monitor for debugging purposes
Serial.println(“Serial test!”); // Test the Serial communication
PingServo.attach(3); // Servo is attached to pin 10 in the motor shield
PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
motordriver.init();

motordriver.setSpeed(140,MOTORA);
motordriver.setDirection(200,MOTORB);
}

void loop()
{
LookAhead();
Serial.print(inches);
Serial.println(” inches”); // Prints a line in the serial monitor
if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/
{
motordriver.goForward();
delay(110); // Wait 0.11 seconds
}else // If not:

{
motordriver.stop(MOTORA);
motordriver.stop(MOTORB);
LookAround(); // Check your surroundings for best route
if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right
{
motordriver.rotateWithID(MOTOR_CLOCKWISE, MOTORA);
motordriver.rotateWithID(MOTOR_CLOCKWISE, MOTORB);
}else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left
{
motordriver.rotateWithID(MOTOR_CLOCKWISE, MOTORA);
motordriver.rotateWithID(MOTOR_ANTICLOCKWISE, MOTORB);
}else if (leftDist&&rightDist<minSafeDist) // If the left and right distance is smaller than the min safe distance (11 inch) go back
{
motordriver.rotateWithID(MOTOR_ANTICLOCKWISE, MOTORA);
}
}
}

unsigned long ping() {
pinMode(pingPin, OUTPUT); // Make the Pingpin to output
digitalWrite(pingPin, LOW); //Send a low pulse
delayMicroseconds(2); // wait for two microseconds
digitalWrite(pingPin, HIGH); // Send a high pulse
delayMicroseconds(5); // wait for 5 micro seconds
digitalWrite(pingPin, LOW); // send a low pulse
pinMode(pingPin,INPUT); // switch the Pingpin to input
duration = pulseIn(pingPin, HIGH); //listen for echo

/*Convert micro seconds to Inches
————————————-*/

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
}

long microsecondsToInches(long microseconds) // converts time to a distance
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) // converts time to a distance
{
return microseconds / 29 / 2;
}

void LookAhead() {
PingServo.write(90);// angle to look forward
delay(175); // wait 0.175 seconds
ping();
}

void LookAround(){
PingServo.write(180); // 180° angle
delay(320); // wait 0.32 seconds
ping();
rightDist = inches; //get the right distance
PingServo.write(0); // look to the other side
delay(620); // wait 0.62 seconds
ping();
leftDist = inches; // get the left distance
PingServo.write(90); // 90° angle
delay(275); // wait 0.275 seconds

// Prints a line in the serial monitor
Serial.print(“RightDist: “);
Serial.println(rightDist);
Serial.print(“LeftDist: “);
Serial.println(leftDist);
Serial.print(“CenterDist: “);
Serial.println(centerDist);
}

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *