Help with ping robot code

Hello all, I found this code on the net to use with my 4 wheel drive robot and ping sensor but I cannot figure out why it is not working properly. When it runs, I can't tell if it uses the ping sensor at all or not because it just goes back and forth and side to side, over and over... I am pretty sure it is because it is running the whole code and running the "define" commands and not just defining them but I really don't know how to fix that because like I have said before, I understand the physical electronics side of arduino and robotics quite well, I really struggle with code though...

#include <NewPing.h>

//Tell the Arduino where the sensor is hooked up
NewPing sonar(12, 13);

int enableA = 11;
int pinA1 = 6;
int pinA2 = 5;

int enableB = 10;
int pinB1 = 4;
int pinB2 = 3;

long inches;

void setup() {
pinMode(enableA, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);

pinMode(enableB, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinB2, OUTPUT);
}

void loop() {

//Run the motors at slightly less than full power
analogWrite(enableA, 200);
analogWrite(enableB, 200);

//Ping the sensor and determine the distance in inches
inches = sonar.ping_in();

//If the robot detects an obstacle less than four inches away, it will back up, then turn left; if no obstacle is detected, it will go forward
if (inches < 4) {
analogWrite(enableA, 255);
analogWrite(enableB, 255);
backward(600);
coast(200);
turnLeft(600);
coast(200);}
else {
forward(1);

}
}

//Define high-level H-bridge commands

void enableMotors()
{
motorAOn();
motorBOn();
}

void disableMotors()
{
motorAOff();
motorBOff();
}

void forward(int time)
{
motorAForward();
motorBForward();
delay(time);
}

void backward(int time)
{
motorABackward();
motorBBackward();
delay(time);
}

void turnLeft(int time)
{
motorABackward();
motorBForward();
delay(time);
}

void turnRight(int time)
{
motorAForward();
motorBBackward();
delay(time);
}

void coast(int time)
{
motorACoast();
motorBCoast();
delay(time);
}

void brake(int time)
{
motorABrake();
motorBBrake();
delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
digitalWrite(enableA, HIGH);
}

void motorBOn()
{
digitalWrite(enableB, HIGH);
}

//disable motors
void motorAOff()
{
digitalWrite(enableB, LOW);
}

void motorBOff()
{
digitalWrite(enableA, LOW);
}

//motor A controls
void motorAForward()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
}

void motorABackward()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, LOW);
}

void motorABrake()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, HIGH);
}

Thanks for the help! By the way, the H-Bridge and ping sensor are hooked up in a typical way (if there is such a thing)... The components are not in some advanced custom setup that would change their behavior.

I don't think the Problem is coming from the Ping. I think it's coming from the Logical flow of the program. If I understand your circuit correct, you can individually set the Speed of Motor a and B and you can individually turn each Motor on or off.

If you want, for example, to go backwards, you would usually Need both Motors to be on to back up straight. As I see the code, it first turns motorA on and motorB off, then immediately turns MotorA off and MotorB on, then waits 600.

When coding, it is always a good idea to take it step by step. I would suggest you write your LOOP function to do something simple like move Forward 100 and turn right. Repeat that four times and you should be back where you started. Then try it again with left turns. Then try it again moving backwards. Once all of that works, then start using the ping.

amarotica:
Hello all, I found this code on the net to use with my 4 wheel drive robot and ping sensor but I cannot figure out why it is not working properly. When it runs, I can't tell if it uses the ping sensor at all or not because it just goes back and forth and side to side, over and over...

I am pretty sure it is because it is running the whole code and running the "define" commands and not just defining them but I really don't know how to fix that because like I have said before, I understand the physical electronics side of arduino and robotics quite well, I really struggle with code though...

void loop() {

//Run the motors at slightly less than full power
analogWrite(enableA, 200);
analogWrite(enableB, 200);

//Ping the sensor and determine the distance in inches
inches = sonar.ping_in();

//If the robot detects an obstacle less than four inches away, it will back up, then turn left; if no obstacle is detected, it will go forward
if (inches < 4) {
analogWrite(enableA, 255);
analogWrite(enableB, 255);
backward(600);
coast(200);
turnLeft(600);
coast(200);}
else {
forward(1);




Thanks for the help! By the way, the H-Bridge and ping sensor are hooked up in a typical way (if there is such a thing)... The components are not in some advanced custom setup that would change their behavior.

So your rover is moving ahead, then the ping detects an obstacle. I'd say that 4 inches sound a bit close, for reliable sonar operation, but assuming it works, you then flip the motors into reverse direction at full power for 0.6 seconds, before coasting for 0.2 sec. You then turn left for 0.6 + 0.2 seconds.

Have you verified that this is enough to actually move your rover? Having four wheels and two motors, I suppose you use skid steering, which can be pretty hard on motors if this thing has any weight.

I'd try fiddling with the delays, in order to clearer see what the robot is doing, and also try to increase the sonar ping distance to at least 12 inches, unless you have verified that it operates ok on that short range.

A good start could be to include in the setup() function code for verifying in turn the different motor control functions, such as forward(), backward(), turnleft() and so on, if you have not already done so.