Uno R3 and L298N w 2 DC Motors..Could someone look at my CODE and help.

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

:slight_smile: 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)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What is you question, what does the code do that it shouldn't?
What is meant by "But it seems to go through the turnAround LOOP over and over."

More importantly what should it do?

Thanks... Tom.. :slight_smile:

TomGeorge:
Hi,

What is you question, what does the code do that it shouldn't?
What is meant by "But it seems to go through the turnAround LOOP over and over."

More importantly what should it do?

Thanks... Tom.. :slight_smile:

Hi Tom.. I figuered it would go back to Void Loop .. "the beginning" and move forward until it encountered another obstacle.. it seem to go right to Void turnAround and stays there.. i appreciate your advice.. i attempted to remove this post . i will do more testing.. and see of i can solve this. i thought it might be something that i was overlooking. that may have been obviouse.