Hi.. this is my first post. Ill try to be specific. SO..I built this "obstacle avoiding robot base" I thought i had it pretty good. But it seems to go through the turnAround LOOP over and over. any pointers would be appreciated. PS I made a lot of comments on code to help with my understanding and anyone else who may look at it.
I am using:
L298N DUAL BRIDGE DC motor driver.. (Velleman VMA409)
a
An Arduino R3 UNO
2 DC 3V Motors
and a Velleman VMA306 Ultra Sonic Sensor
Everything is working as it should. the motors have plenty of power and the UNO R3 board is seperately powered. The servo works goodetc.
I believe i am Missing something in the Code. IF anyone wants to take a look and give me a couple pointers or mark it up..that would be Great.. Happy Turkey Day!
#include <Servo.h>
#include <NewPing.h>
#define TRIG_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 300 //sets MAx Distance of sensor ping read
#define COLL_DIST 30 //set the distance at which stops and reverses to avoid collision
#define TURN_DIST COLL_DIST+20 //Sets distance at which robot veers away from a collision object
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); //set up sensor libray to use correct pins
Servo myservo; //creates Servo object to control servo ..."gives servo a name"
int leftDistance, rightDistance; //distance on either side..gives name for those calculations.
int curDist =0; //starting value for current distance..set for 0
// Motor A
int enA = 9; //Analog PWM pin.
int in1 = 8;
int in2 = 7;
// Motor B
int enB = 3; //analog PWM pin
int in3 = 5;
int in4 = 4;
void setup()
{
myservo.attach(13); // indicates which pin the Servo signal line is attached
myservo.write(90); // tell servo to postion itself for forward facing..you may have to adjust this depending on which direction you are facing your servo to begin with.
delay(1000); //delay for 1 second
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
myservo.write(90);
delay(90);
curDist = readPing();
if (curDist < COLL_DIST) {
changePath();} //if current dist. is blocked..change pat..directs robot to goto changePath loop in program
moveForward(); // directs robot to move forward if there is no collision object in the way..by directing robot to goto moveForward loop in program which has coding to make motors go forward
delay(500); //this tell robot to go forward for 1/2 second and check again
}
// this is the changePath loop ----------------
//--------------------------------------------
void changePath() {
moveStop(); //directs program to Stop loop which has codeing to stop the motors
myservo.write(36); // turns sensor to the right
delay(500); // waits 1/2 second towhile sensor sends and reads ping signal
rightDistance = readPing(); //send signal and tells robot to remember right distance from reading the ping signal
delay(500);
myservo.write(144); //checks the left..turns servo to the left
delay(700);
leftDistance = readPing();
delay(500);
myservo.write(90);
delay(500);
compareDistance();
}
// end changePath loop--------------------------------------------
//------------------------------------------------------------------
// Start compareDistance loop----------------------------------
//-------------------------------------------------------------------
void compareDistance()
{
if (leftDistance > rightDistance)
{
turnLeft(); //directs robot to turnLeft loop..which contains cod to turn robot left
}
else if (rightDistance > leftDistance)
{
turnRight();
}
else
{
turnAround();
}
}
// end compareDistance loop-----------------------------------
//--------------------------------------------------------------
// start readPing math loop----------------------------
//---------------------------------------------------
int readPing() {
delay(500);
unsigned int uS = sonar.ping();
int cm = uS / US_ROUNDTRIP_CM;
return cm;
}
// stop read PING math loop-----------------------
// start STOP loop------------------------------
void moveStop()
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// end stop motor loop--------------------
//---------------------------------------
void moveForward() {
// Turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set speed to 255 out of possible range 0~255
analogWrite(enA, 255);
// Turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Set speed to 255 out of possible range 0~255
analogWrite(enB, 255);
delay(50);
}
//end moveForward loop---------------------------------
//------------------------------------------------------
// Start moveBackward loop ----------------------------
//------------------------------------------------------
void moveBackward() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(5);
}
//end moveBackward loop -----------------------------------
//----------------------------------------------------------
//Start turnRight loop -----------------------
//----------------------------------------------------------
void turnRight() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(900);
}
// end turn right-------------------------
//start turnLeft loop----------------------------
void turnLeft() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(900);
}
//end turnleft loop--------------------------
// start turn around loop
void turnAround() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(1700);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set speed to 255 out of possible range 0~255
analogWrite(enA, 255);
// Turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Set speed to 255 out of possible range 0~255
analogWrite(enB, 255);
delay(20);
}
L249Hbridgemotorobstacleavoid.ino (5.35 KB)