programming maxbotix and ping sensor

hi everyone!!
i have a maxbotix ultrasonic sensor but after surfing internet for quite some time i couldnt find enough tutorials for maxbotix as compared to ping sensor. basically i am building an autonomous robot firstly can any one guide me that the sig pin of ping sensor how can i able to use the same functionality of sig pin from maxbotix sensor?which pin should i replace it in maxbotix?thus the coding remain same?
i have tried to build a code using pwm but i dnt know my motors are not reacting to my commands might there be a problem with the code i am a beginner please do help me !!!
here is the code

int motor1Pin1 = 9;                             // pin 2 on L293D
int motor1Pin2 = 10;                             // pin 7 on L293D
int enable1Pin = 6;                             // pin 1 on L293D
int motor2Pin1 = 12;                             // pin 10 on L293D
int motor2Pin2 = 11;                             // pin  15 on L293D
int enable2Pin = 7;                            // pin 9 on L293D
int configPin = 13;

void setup() {
 pinMode(motor1Pin1, OUTPUT);
 pinMode(motor1Pin2, OUTPUT);
 pinMode(enable1Pin, OUTPUT);
 pinMode(motor2Pin1, OUTPUT);
 pinMode(motor2Pin2, OUTPUT);
 pinMode(enable2Pin, OUTPUT);
 // set enablePins high so that motor can turn on:
 digitalWrite(enable1Pin, HIGH);
 digitalWrite(enable2Pin, HIGH);
 Serial.begin(9600);             //serial baud rate 9600
pinMode(configPin,OUTPUT);       //make Calibration pin output
}

float sensor(){                     //looping of program
digitalWrite(configPin,HIGH);    //raise the reset pin high
delay(120);                      //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
Serial.print("Sensed a-d value:"); //print a-d text
Serial.println(sensorvalue);       //print a-d value
Serial.print("Inch value=");      //print inch text
Serial.println(inchvalue);        //print inch value
delay(1000);                      //optional delay 1 second
digitalWrite(configPin,LOW);      //turn off Calibration ring and sensor
delay(1000);                      //delay 1 second
}


void forward() // This function moves the wheels forward
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void backward() // This function moves the wheels backward
{
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}
void  haltMotors() 
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
}
void  turnRight(){// This function turns the robot right.
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void loop() {
long duration, inches; // Declare the variable, duration and inches.
duration = pulseIn(configPin, HIGH);
inches = sensor(); // Set the inches variable to the float returned by the ping() function.

while (inches >{ // While the robot is 8 inches away from an object.
inches = sensor();
forward(); // Move the robot forward.
}
haltMotors(); // Stop the motors for 2 seconds, before changing direction.
delay(2000);

while (inches < 10){ // Until the robot is 10 inches away from the object, go backward.
inches = sensor();
backward(); // Move the robot backward.
}
turnRight();
delay(5000); 

haltMotors();
delay(2000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

Moderatore edit: I took some time and trouble to edit this post and added some CODE TAGS

A couple of things 1) DO NOT POST THE SAME QUESTION IN DIFFERENT PARTS OF THE FORUM. It wastes time. Duplicate deleted. 2) Read this

float sensor(){                     //looping of program
digitalWrite(configPin,HIGH);    //raise the reset pin high
delay(120);                      //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
Serial.print("Sensed a-d value:"); //print a-d text
Serial.println(sensorvalue);       //print a-d value
Serial.print("Inch value=");      //print inch text
Serial.println(inchvalue);        //print inch value
delay(1000);                      //optional delay 1 second
digitalWrite(configPin,LOW);      //turn off Calibration ring and sensor
delay(1000);                      //delay 1 second
}

This function is defined as returning a float. Where is the return statement that returns that float?

Meaningful function names almost always include a noun and a verb, like digitalRead() and analogWrite(). This function has a lousy name.

but i dnt know my motors are not reacting to my commands

If you ship all of your hardware to me, I’ll be able to tell you whether your motors are reacting to your commands, or not.

Surely, you can see whether they are, or are not.

Simulate the output you expect from the poorly named function, sensor(), instead of relying on that output. That way, you’ll know if it the function output that is wrong, or the action you take based on that output.

while (inches >{ // While the robot is 8 inches away from an object.
inches = sensor();
forward(); // Move the robot forward.
}

What the hell is this? While inches greater than what? This crap won’t even compile.

The sensor() function does not properly return a value, so you can hardly assign that value to a variable in any meaningful way.

thank you for your reply, does the name sensor will not work ok will put a return to it,like in case of parallex sensor they use ping() where i have used sensor(). sorry mistakenly i removed >8 from the loop. my motors are responding well my ultrasonic sensor is also responding to my commands but when i am combining them together thats where the motors are just running forward but they are not responding the way i want them to from the ultrasonic sensor... so i shall put a return in the following code and and what shall i name this loop?

[float sensor(){                     //looping of program
digitalWrite(configPin,HIGH);    //raise the reset pin high
delay(120);                      //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
Serial.print("Sensed a-d value:"); //print a-d text
Serial.println(sensorvalue);       //print a-d value
Serial.print("Inch value=");      //print inch text
Serial.println(inchvalue);        //print inch value
delay(1000);                      //optional delay 1 second
digitalWrite(configPin,LOW);      //turn off Calibration ring and sensor
delay(1000);                      //delay 1 second
}]
[while (inches >8{ // While the robot is 8 inches away from an object.
inches = sensor();
forward(); // Move the robot forward.
}]

now is it ok?

now is it ok?

No. You have not made sensors() return a value, and yet you still expect that to happen magically.

Magic is for Harry Potter movies. No magic happens on the Arduino.

No. You have not made sensors() return a value, and yet you still expect that to happen magically.

hahaha!!! thanks bro:)

int motor1Pin1 = 9;                             // pin 2 on L293D
int motor1Pin2 = 10;                             // pin 7 on L293D
int enable1Pin = 6;                             // pin 1 on L293D
int motor2Pin1 = 12;                             // pin 10 on L293D
int motor2Pin2 = 11;                             // pin  15 on L293D
int enable2Pin = 7;                            // pin 9 on L293D
int configPin = 13;

void setup() {
 pinMode(motor1Pin1, OUTPUT);
 pinMode(motor1Pin2, OUTPUT);
 pinMode(enable1Pin, OUTPUT);
 pinMode(motor2Pin1, OUTPUT);
 pinMode(motor2Pin2, OUTPUT);
 pinMode(enable2Pin, OUTPUT);
 // set enablePins high so that motor can turn on:
 digitalWrite(enable1Pin, HIGH);
 digitalWrite(enable2Pin, HIGH);
 Serial.begin(9600);             //serial baud rate 9600
pinMode(configPin,OUTPUT);       //make Calibration pin output
}

float sensor(){                     //looping of program
digitalWrite(configPin,HIGH);    //raise the reset pin high
delay(120);                      //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
Serial.print("Sensed a-d value:"); //print a-d text
Serial.println(sensorvalue);       //print a-d value
Serial.print("Inch value=");      //print inch text
Serial.println(inchvalue);        //print inch value
delay(1000);                      //optional delay 1 second
digitalWrite(configPin,LOW);      //turn off Calibration ring and sensor
delay(1000);                      //delay 1 second
return inchvalue;
}


void forward() // This function moves the wheels forward
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void backward() // This function moves the wheels backward
{
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}
void  haltMotors() 
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
}
void  turnRight(){// This function turns the robot right.
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void loop() {
long duration, inches; // Declare the variable, duration and inches.
duration = pulseIn(configPin, HIGH);
inches = sensor(); // Set the inches variable to the float returned by the ping() function.

while (inches >8{ // While the robot is 8 inches away from an object.
inches = sensor();
forward(); // Move the robot forward.
}
haltMotors(); // Stop the motors for 2 seconds, before changing direction.
delay(2000);

while (inches < 10){ // Until the robot is 10 inches away from the object, go backward.
inches = sensor();
backward(); // Move the robot backward.
}
turnRight();
delay(5000); 

haltMotors();
delay(2000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

now is every thing ok? and one more help please can u tell me what microsecondsToInches command does?is it necessary for me to include it in my program?thank u so much

while (inches >8{

The compiler already caught that one, so why are you asking if it is OK?

The IDE has a really useful tool called auto format. Please use it before posting.

now is every thing ok?

No. Suppose you are walking down a hall, and people are moving in the same direction, in the opposite direction, and across the path in front of you. You need to avoid hitting anyone, by turning towards the most open direction.

Would you really determine that by keeping your eyes close most of the time?

That is what the delay() calls in sensors() is doing. You keep charging ahead, or turning, while blind most of the time. You can only perform a course correction at most every two seconds.

I hope you don't drive the same way.

Also:

float sensor(){                     //looping of program
float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
return inchvalue;
}

You aren't looping anything here. Ditch the dumb comment.

The function returns a float.

void loop() {
long duration, inches; // Declare the variable, duration and inches.
duration = pulseIn(configPin, HIGH);
inches = sensor(); // Set the inches variable to the float returned by the ping() function.

You are not calling a ping() function. The inches variable is not a float, so why does the function return a float, when what you want is a long? Or, why do you truncate the float when you store it in a long?

while (inches >8{ // While the robot is 8 inches away from an object.
inches = sensor();
forward(); // Move the robot forward.
}

This is still crap. The while statement needs an open paren AND a close paren. The body of the statement is enclosed in curly braces.

Learn to put every { on a new line, so mistakes like this jump out at you. Learn to compile code before wasting time posting it here, unless the problem is that you don't understand what the compiler is telling you. In which case, you also need to provide the error messages AND say that that is what you need help with.

No. Suppose you are walking down a hall, and people are moving in the same direction, in the opposite direction, and across the path in front of you. You need to avoid hitting anyone, by turning towards the most open direction.

sorry mate but i am new to arduino and programming stuff!!!
and being handed this sort of project… i have done some basic C++ programming and that was also 3yrs back…
so i am left with only one option to post my issues on this forum so i can compile and learn how to code.
ok i have implemented on your suggestions and compiled this code and have verified it it isnt giving me any error

int motor1Pin1 = 9;                             // pin 2 on L293D
int motor1Pin2 = 10;                             // pin 7 on L293D
int enable1Pin = 6;                             // pin 1 on L293D
int motor2Pin1 = 12;                             // pin 10 on L293D
int motor2Pin2 = 11;                             // pin  15 on L293D
int enable2Pin = 7;                            // pin 9 on L293D
int configPin = 13;

void setup() {
 pinMode(motor1Pin1, OUTPUT);
 pinMode(motor1Pin2, OUTPUT);
 pinMode(enable1Pin, OUTPUT);
 pinMode(motor2Pin1, OUTPUT);
 pinMode(motor2Pin2, OUTPUT);
 pinMode(enable2Pin, OUTPUT);
 // set enablePins high so that motor can turn on:
 digitalWrite(enable1Pin, HIGH);
 digitalWrite(enable2Pin, HIGH);
 Serial.begin(9600);             //serial baud rate 9600
pinMode(configPin,OUTPUT);       //make Calibration pin output
}

float sensor(){                     //looping of program
digitalWrite(configPin,HIGH);    //raise the reset pin high
delay(120);                      //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
Serial.print("Sensed a-d value:"); //print a-d text
Serial.println(sensorvalue);       //print a-d value
Serial.print("Inch value=");      //print inch text
Serial.println(inchvalue);        //print inch value
delay(1000);                      //optional delay 1 second
digitalWrite(configPin,LOW);      //turn off Calibration ring and sensor
delay(1000);                      //delay 1 second
return float();
}


void forward() // This function moves the wheels forward
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void backward() // This function moves the wheels backward
{
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}
void  haltMotors() 
{
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
}
void  turnRight(){// This function turns the robot right.
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}
void loop() {
long duration;
float  inches; // Declare the variable, duration and inches.
duration = pulseIn(configPin, HIGH);
inches = sensor(); // Set the inches variable to the float returned by the sensor() function.

while (inches >8)
{ // While the robot is 8 inches away from an object.
inches = sensor();
forward(); // Move the robot forward.
}
haltMotors(); // Stop the motors for 2 seconds, before changing direction.
delay(2000);

while (inches < 10){ // Until the robot is 10 inches away from the object, go backward.
inches = sensor();
backward(); // Move the robot backward.
}
turnRight();
delay(5000); 

haltMotors();
delay(2000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

so is it ok now?

and i will change the delay time for sure in sensor() loop.

so is it ok now?

I’d say no, but what happens when you test it?

float sensor(){                     //looping of program
digitalWrite(configPin,HIGH);    //raise the reset pin high
delay(120);                      //start of calibration ring
float sensorvalue = analogRead(0); //get analog sensor value from pin 0
float inchvalue = (254.0/1024.0) *2.0* sensorvalue; //convert to inches
Serial.print("Sensed a-d value:"); //print a-d text
Serial.println(sensorvalue);       //print a-d value
Serial.print("Inch value=");      //print inch text
Serial.println(inchvalue);        //print inch value
delay(1000);                      //optional delay 1 second
digitalWrite(configPin,LOW);      //turn off Calibration ring and sensor
delay(1000);                      //delay 1 second
return float();
}

I assume you actually want to return more than a null. I presume you meant to return inchvalue.

return float();

What is this supposed to be doing? What you had there was right.

You can't have any delay() in sensor(). It needs to be reading as fast as possible.

long duration;
duration = pulseIn(configPin, HIGH);

This can, and should be, one line of code.

float  inches; // Declare the variable, duration and inches.
inches = sensor(); // Set the inches variable to the float returned by the sensor() function.

This can, and should be, one line of code.

What does the second comment contribute? Anyone with a IQ above 4 can see that that is what the code is doing.

Similarly, the other comments contribute nothing, and are no longer even correct.

What is connected to the config pin? What is learned from waiting for it to go LOW then HIGH again?

It really looks to me like you have no idea how to read the distance sensor that you have. Before trying to use the data from the sensor, write another sketch to just get data from the distance sensor, and print the distance to the serial monitor. Do not come back to this sketch until you can post code that does return readings that correlate to distance.

It really looks to me like you have no idea how to read the distance sensor that you have. Before trying to use the data from the sensor, write another sketch to just get data from the distance sensor, and print the distance to the serial monitor. Do not come back to this sketch until you can post code that does return readings that correlate to distance.

ya i have tried it individually and is giving me correct reading the problem is when i am combining it with motors. here is the individual code for sensor

const int pwPin = 7; 
//variables needed to store values
long pulse, inches, cm;
void setup() {
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
}
void loop() {
  pinMode(pwPin, INPUT);
    //Used to read in the pulse that is being sent by the MaxSonar device.
  //Pulse Width representation with a scale factor of 147 uS per Inch.

  pulse = pulseIn(pwPin, HIGH);
  //147uS per inch
  inches = pulse/147;
  //change inches to centimetres
  cm = inches * 2.54;
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(500);
}

here is the individual code for sensor

The code for reading the sensor in the combined code looks NOTHING like that code. If that code works, why didn't you copy it exactly into the other sketch?

The code for reading the sensor in the combined code looks NOTHING like that code. If that code works, why didn't you copy it exactly into the other sketch?

but using the code

const int pwPin = 7; 
//variables needed to store values
long pulse, inches, cm;
void setup() {
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
}
void loop() {
  pinMode(pwPin, INPUT);
    //Used to read in the pulse that is being sent by the MaxSonar device.
  //Pulse Width representation with a scale factor of 147 uS per Inch.

  pulse = pulseIn(pwPin, HIGH);
  //147uS per inch
  inches = pulse/147;
  //change inches to centimetres
  cm = inches * 2.54;
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(500);
}

i am getting an issue that how should i arrange it so i can retrieve value from here and can pass commands to my motors? and secondly about the previous code that code is also showing me values when i print them but the problem is of motors they are not responding the way i want them to ofcourse they are due to my incorrect code so what should i do either go on with the previous code in which u have pointed blunders or should i work on this code?

i am getting an issue that how should i arrange it so i can retrieve value from here and can pass commands to my motors?

Create a function, just like you did before (except with a better name). Return a value (inches) from that function, rather than using a global variable.

You have two incompatible sets of code that purport to read data from the sensor. Only one can possibly be correct. It is that code, whichever one it is, that is the basis for a robust application.

thank you so much i will work on the code on thursday and will show you then thank you so much for helping me