Hi there, I am working on an obstacle avoiding car. I am modifying some code I found online. I have gotten the car working with the motor shield V2 that I have but I would like to program the servo that moves the ultrasonic sensor to constantly sweep back and forth so that obstacles can be detected more reliably. As it currently is, the servo only looks left to right when an obstacle is encountered.
I have tried to add a few new lines of code to get the servo to constantly look left to right but haven't been able to do so without breaking the rest of the code. I've just gone ahead and pasted all of the code below. So basically, I'd just like to know how I can add on to this so that the servo is constantly moving without causing the rest of the code to not work. I can provide more code and or information if needed. Thank you so much!
#include <Adafruit_MotorShield.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN 9
#define ECHO_PIN 8
#define MAX_DISTANCE 200
#define MAX_SPEED 100 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);
Servo myservo;
boolean goesForward=false;
int distance = 100;
int speedSet = 0;
void setup() {
myservo.attach(10);
myservo.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
Serial.begin(9600);
Serial.println("Riverbot DC Motor test!");
//start the motor shield
if (!AFMS.begin()) {
Serial.println("Could not find Motor Shield. Check wiring");
while (1);
}
Serial.println("Motor Shield found");
}
void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);
if(distance<=20)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL)
{
turnRight();
moveStop();
}else
{
turnLeft();
moveStop();
}
}else
{
moveForward();
}
distance = readPing();
}
int lookRight()
{
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}
int lookLeft()
{
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}
void moveStop() {
Motor1->run(RELEASE);
Motor2->run(RELEASE);
Motor3->run(RELEASE);
Motor4->run(RELEASE);
}
void moveForward() {
if(!goesForward)
{
goesForward=true;
Motor1->run(FORWARD);
Motor2->run(FORWARD);
Motor3->run(FORWARD);
Motor4->run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
Motor1->setSpeed(speedSet);
Motor2->setSpeed(speedSet);
Motor3->setSpeed(speedSet);
Motor4->setSpeed(speedSet);
delay(5);
}
}
}
void moveBackward() {
goesForward=false;
Motor1->run(BACKWARD);
Motor2->run(BACKWARD);
Motor3->run(BACKWARD);
Motor4->run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
Motor1->setSpeed(speedSet);
Motor2->setSpeed(speedSet);
Motor3->setSpeed(speedSet);
Motor4->setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
Motor1->run(FORWARD);
Motor2->run(FORWARD);
Motor3->run(BACKWARD);
Motor4->run(BACKWARD);
delay(500);
Motor1->run(FORWARD);
Motor2->run(FORWARD);
Motor3->run(FORWARD);
Motor4->run(FORWARD);
}
void turnLeft() {
Motor1->run(BACKWARD);
Motor2->run(BACKWARD);
Motor3->run(FORWARD);
Motor4->run(FORWARD);
delay(500);
Motor1->run(FORWARD);
Motor2->run(FORWARD);
Motor3->run(FORWARD);
Motor4->run(FORWARD);
}